private RateLimitProcessor <SimulationRequest> GetSlidingWindowProcessor(string storageType) { var slidingWindowsRules = new SlidingWindowRateLimitRule <SimulationRequest>[] { new SlidingWindowRateLimitRule <SimulationRequest>(TimeSpan.FromSeconds(6), TimeSpan.FromSeconds(1)) { Id = Guid.NewGuid().ToString(), LimitNumber = 20, LockSeconds = 1, ExtractTarget = (request) => { return(request.RequestResource); }, CheckRuleMatching = (request) => { return(true); }, } }; IRateLimitStorage storage = new InProcessMemoryStorage(); if (storageType == "redis") { storage = new RedisStorage(StackExchange.Redis.ConnectionMultiplexer.Connect("127.0.0.1")); } return(new RateLimitProcessor <SimulationRequest> .Builder() .WithAlgorithm(new SlidingWindowAlgorithm <SimulationRequest>(slidingWindowsRules)) .WithStorage(storage) .WithError(new RateLimitError() { Code = 429, }) .Build()); }
private RateLimitProcessor <SimulationRequest> GetSlidingWindowProcessor(string storageType, int limitNumber) { IRateLimitStorage storage = memoryStorage; if (storageType == "redis") { storage = redisStorage; } var slidingWindowsRules = new SlidingWindowRateLimitRule <SimulationRequest>[] { new SlidingWindowRateLimitRule <SimulationRequest>(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(1)) { Id = Guid.NewGuid().ToString(), LimitNumber = limitNumber, LockSeconds = 1, ExtractTarget = (request) => { return(request.RequestResource); }, CheckRuleMatching = (request) => { return(true); }, } }; return(new RateLimitProcessor <SimulationRequest> .Builder() .WithAlgorithm(new SlidingWindowAlgorithm <SimulationRequest>(slidingWindowsRules)) .WithStorage(storage) .WithError(new RateLimitError() { Code = 429, }) .Build()); }