mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 10:38:15 +08:00
add ratelimit acceptance test
This commit is contained in:
@ -122,7 +122,7 @@ namespace Ocelot.Configuration.Creator
|
||||
Limit = fileReRoute.RateLimitOptions.Limit,
|
||||
Period = fileReRoute.RateLimitOptions.Period,
|
||||
PeriodTimespan = TimeSpan.FromSeconds(fileReRoute.RateLimitOptions.PeriodTimespan)
|
||||
});
|
||||
}, globalConfiguration.RateLimitOptions.HttpStatusCode);
|
||||
}
|
||||
var serviceProviderPort = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
|
||||
|
||||
|
@ -7,7 +7,6 @@ namespace Ocelot.Configuration.File
|
||||
{
|
||||
public class FileRateLimitOptions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId
|
||||
/// </summary>
|
||||
@ -29,6 +28,11 @@ namespace Ocelot.Configuration.File
|
||||
/// Disables X-Rate-Limit and Rety-After headers
|
||||
/// </summary>
|
||||
public bool DisableRateLimitHeaders { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP Status code returned when rate limiting occurs, by default value is set to 429 (Too Many Requests)
|
||||
/// </summary>
|
||||
public int HttpStatusCode { get; private set; } = 429;
|
||||
}
|
||||
|
||||
|
||||
|
@ -11,7 +11,7 @@ namespace Ocelot.Configuration
|
||||
public class RateLimitOptions
|
||||
{
|
||||
public RateLimitOptions(bool enbleRateLimiting, string clientIdHeader, List<string> clientWhitelist,bool disableRateLimitHeaders,
|
||||
string quotaExceededMessage, string rateLimitCounterPrefix, RateLimitRule rateLimitRule)
|
||||
string quotaExceededMessage, string rateLimitCounterPrefix, RateLimitRule rateLimitRule, int httpStatusCode)
|
||||
{
|
||||
EnableRateLimiting = enbleRateLimiting;
|
||||
ClientIdHeader = clientIdHeader;
|
||||
@ -20,6 +20,7 @@ namespace Ocelot.Configuration
|
||||
QuotaExceededMessage = quotaExceededMessage;
|
||||
RateLimitCounterPrefix = rateLimitCounterPrefix;
|
||||
RateLimitRule = rateLimitRule;
|
||||
HttpStatusCode = httpStatusCode;
|
||||
}
|
||||
|
||||
public RateLimitRule RateLimitRule { get; private set; }
|
||||
@ -29,12 +30,12 @@ namespace Ocelot.Configuration
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId
|
||||
/// </summary>
|
||||
public string ClientIdHeader { get; private set; } = "ClientId";
|
||||
public string ClientIdHeader { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP Status code returned when rate limiting occurs, by default value is set to 429 (Too Many Requests)
|
||||
/// </summary>
|
||||
public int HttpStatusCode { get; private set; } = 429;
|
||||
public int HttpStatusCode { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message.
|
||||
@ -46,7 +47,7 @@ namespace Ocelot.Configuration
|
||||
/// <summary>
|
||||
/// Gets or sets the counter prefix, used to compose the rate limit counter cache key
|
||||
/// </summary>
|
||||
public string RateLimitCounterPrefix { get; private set; } = "ocelot";
|
||||
public string RateLimitCounterPrefix { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Enables endpoint rate limiting based URL path and HTTP verb
|
||||
|
@ -0,0 +1,45 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ocelot.RateLimit
|
||||
{
|
||||
public class DistributedCacheRateLimitCounterHanlder : IRateLimitCounterHandler
|
||||
{
|
||||
private readonly IDistributedCache _memoryCache;
|
||||
|
||||
public DistributedCacheRateLimitCounterHanlder(IDistributedCache memoryCache)
|
||||
{
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
public void Set(string id, RateLimitCounter counter, TimeSpan expirationTime)
|
||||
{
|
||||
_memoryCache.SetString(id, JsonConvert.SerializeObject(counter), new DistributedCacheEntryOptions().SetAbsoluteExpiration(expirationTime));
|
||||
}
|
||||
|
||||
public bool Exists(string id)
|
||||
{
|
||||
var stored = _memoryCache.GetString(id);
|
||||
return !string.IsNullOrEmpty(stored);
|
||||
}
|
||||
|
||||
public RateLimitCounter? Get(string id)
|
||||
{
|
||||
var stored = _memoryCache.GetString(id);
|
||||
if (!string.IsNullOrEmpty(stored))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<RateLimitCounter>(stored);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Remove(string id)
|
||||
{
|
||||
_memoryCache.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user