mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:32:50 +08:00
refactor ratelimit config
This commit is contained in:
parent
e1d5ef3aae
commit
9b06afc781
@ -1,6 +1,6 @@
|
||||
{
|
||||
"projects": [ "src", "test" ],
|
||||
"sdk": {
|
||||
"version": "1.0.0-preview2-003131"
|
||||
"version": "1.0.0-preview2-003133"
|
||||
}
|
||||
}
|
||||
|
@ -114,14 +114,14 @@ namespace Ocelot.Configuration.Creator
|
||||
RateLimitOptions rateLimitOption = null;
|
||||
if (enableRateLimiting)
|
||||
{
|
||||
rateLimitOption = new RateLimitOptions(enableRateLimiting, fileReRoute.RateLimitOptions.ClientIdHeader,
|
||||
fileReRoute.RateLimitOptions.ClientWhitelist,fileReRoute.RateLimitOptions.DisableRateLimitHeaders,
|
||||
fileReRoute.RateLimitOptions.QuotaExceededMessage,fileReRoute.RateLimitOptions.RateLimitCounterPrefix,
|
||||
rateLimitOption = new RateLimitOptions(enableRateLimiting, globalConfiguration.RateLimitOptions.ClientIdHeader,
|
||||
fileReRoute.RateLimitOptions.ClientWhitelist, globalConfiguration.RateLimitOptions.DisableRateLimitHeaders,
|
||||
globalConfiguration.RateLimitOptions.QuotaExceededMessage, globalConfiguration.RateLimitOptions.RateLimitCounterPrefix,
|
||||
new RateLimitRule()
|
||||
{
|
||||
Limit = fileReRoute.RateLimitOptions.RateLimitRule.Limit,
|
||||
Period = fileReRoute.RateLimitOptions.RateLimitRule.Period,
|
||||
PeriodTimespan = TimeSpan.FromSeconds(fileReRoute.RateLimitOptions.RateLimitRule.PeriodTimespan)
|
||||
Limit = fileReRoute.RateLimitOptions.Limit,
|
||||
Period = fileReRoute.RateLimitOptions.Period,
|
||||
PeriodTimespan = TimeSpan.FromSeconds(fileReRoute.RateLimitOptions.PeriodTimespan)
|
||||
});
|
||||
}
|
||||
var serviceProviderPort = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
|
||||
|
@ -5,8 +5,12 @@
|
||||
public FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider();
|
||||
RateLimitOptions = new FileRateLimitOptions();
|
||||
}
|
||||
public string RequestIdKey { get; set; }
|
||||
|
||||
public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;}
|
||||
|
||||
public FileRateLimitOptions RateLimitOptions { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -7,26 +7,12 @@ namespace Ocelot.Configuration.File
|
||||
{
|
||||
public class FileRateLimitOptions
|
||||
{
|
||||
public FileRateLimitOptions()
|
||||
{
|
||||
RateLimitRule = new FileRateLimitRule();
|
||||
ClientWhitelist = new List<string>();
|
||||
}
|
||||
|
||||
public FileRateLimitRule RateLimitRule { get; set; }
|
||||
|
||||
public List<string> ClientWhitelist { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId
|
||||
/// </summary>
|
||||
public string ClientIdHeader { get; set; } = "ClientId";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the policy prefix, used to compose the client policy cache key
|
||||
/// </summary>
|
||||
public string ClientPolicyPrefix { get; set; } = "crlp";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message.
|
||||
/// If none specified the default will be:
|
||||
@ -39,28 +25,11 @@ namespace Ocelot.Configuration.File
|
||||
/// </summary>
|
||||
public string RateLimitCounterPrefix { get; set; } = "ocelot";
|
||||
|
||||
/// <summary>
|
||||
/// Enables endpoint rate limiting based URL path and HTTP verb
|
||||
/// </summary>
|
||||
public bool EnableRateLimiting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Disables X-Rate-Limit and Rety-After headers
|
||||
/// </summary>
|
||||
public bool DisableRateLimitHeaders { get; set; }
|
||||
}
|
||||
|
||||
public class FileRateLimitRule
|
||||
{
|
||||
/// <summary>
|
||||
/// Rate limit period as in 1s, 1m, 1h
|
||||
/// </summary>
|
||||
public string Period { get; set; }
|
||||
|
||||
public int PeriodTimespan { get; set; }
|
||||
/// <summary>
|
||||
/// Maximum number of requests that a client can make in a defined period
|
||||
/// </summary>
|
||||
public long Limit { get; set; }
|
||||
}
|
||||
}
|
||||
|
34
src/Ocelot/Configuration/File/FileRateLimitRule.cs
Normal file
34
src/Ocelot/Configuration/File/FileRateLimitRule.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ocelot.Configuration.File
|
||||
{
|
||||
|
||||
public class FileRateLimitRule
|
||||
{
|
||||
public FileRateLimitRule()
|
||||
{
|
||||
ClientWhitelist = new List<string>();
|
||||
}
|
||||
|
||||
public List<string> ClientWhitelist { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Enables endpoint rate limiting based URL path and HTTP verb
|
||||
/// </summary>
|
||||
public bool EnableRateLimiting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Rate limit period as in 1s, 1m, 1h
|
||||
/// </summary>
|
||||
public string Period { get; set; }
|
||||
|
||||
public int PeriodTimespan { get; set; }
|
||||
/// <summary>
|
||||
/// Maximum number of requests that a client can make in a defined period
|
||||
/// </summary>
|
||||
public long Limit { get; set; }
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ namespace Ocelot.Configuration.File
|
||||
AuthenticationOptions = new FileAuthenticationOptions();
|
||||
FileCacheOptions = new FileCacheOptions();
|
||||
QoSOptions = new FileQoSOptions();
|
||||
RateLimitOptions = new FileRateLimitOptions();
|
||||
RateLimitOptions = new FileRateLimitRule();
|
||||
}
|
||||
|
||||
public string DownstreamPathTemplate { get; set; }
|
||||
@ -33,6 +33,6 @@ namespace Ocelot.Configuration.File
|
||||
public int DownstreamPort { get; set; }
|
||||
public FileQoSOptions QoSOptions { get; set; }
|
||||
public string LoadBalancer {get;set;}
|
||||
public FileRateLimitOptions RateLimitOptions { get; set; }
|
||||
public FileRateLimitRule RateLimitOptions { get; set; }
|
||||
}
|
||||
}
|
@ -22,7 +22,6 @@ namespace Ocelot.Configuration
|
||||
RateLimitRule = rateLimitRule;
|
||||
}
|
||||
|
||||
|
||||
public RateLimitRule RateLimitRule { get; private set; }
|
||||
|
||||
public List<string> ClientWhitelist { get; private set; }
|
||||
|
@ -1 +1 @@
|
||||
{"ReRoutes":[{"DownstreamPathTemplate":"41879/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null,"RateLimitOptions":{"RateLimitRule":{"Period":null,"PeriodTimespan":0,"Limit":0},"ClientWhitelist":[],"ClientIdHeader":"ClientId","ClientPolicyPrefix":"crlp","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","EnableRateLimiting":false,"DisableRateLimitHeaders":false}}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Host":null,"Port":0}}}
|
||||
{"ReRoutes":[{"DownstreamPathTemplate":"41879/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null,"RateLimitOptions":{"ClientWhitelist":[],"EnableRateLimiting":false,"Period":null,"PeriodTimespan":0,"Limit":0}}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Host":null,"Port":0},"RateLimitOptions":{"ClientIdHeader":"ClientId","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","DisableRateLimitHeaders":false}}}
|
@ -26,7 +26,6 @@ namespace Ocelot.UnitTests.RateLimit
|
||||
public class ClientRateLimitMiddlewareTests
|
||||
{
|
||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||
private readonly Mock<IRateLimitCounterHandler> _counterHanlder;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
|
Loading…
x
Reference in New Issue
Block a user