mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 11:58:15 +08:00
refactor ratelimit config
This commit is contained in:
@ -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; }
|
||||
|
Reference in New Issue
Block a user