private RateLimitProcessor <SimulationRequest> GetLeakyBucketProcessor(string storageType) { var leakyBucketRules = new LeakyBucketRateLimitRule <SimulationRequest>[] { new LeakyBucketRateLimitRule <SimulationRequest>(30, 10, TimeSpan.FromSeconds(1)) { Id = Guid.NewGuid().ToString(), 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 LeakyBucketAlgorithm <SimulationRequest>(leakyBucketRules)) .WithStorage(storage) .WithError(new RateLimitError() { Code = 429, }) .Build()); }
private RateLimitProcessor <SimulationRequest> GetLeakyBucketProcessor(string storageType, int limitNumber) { IRateLimitStorage storage = memoryStorage; if (storageType == "redis") { storage = redisStorage; } var leakyBucketRules = new LeakyBucketRateLimitRule <SimulationRequest>[] { new LeakyBucketRateLimitRule <SimulationRequest>(limitNumber, 100, TimeSpan.FromSeconds(1)) { Id = Guid.NewGuid().ToString(), LockSeconds = 1, ExtractTarget = (request) => { return(request.RequestResource); }, CheckRuleMatching = (request) => { return(true); }, } }; return(new RateLimitProcessor <SimulationRequest> .Builder() .WithAlgorithm(new LeakyBucketAlgorithm <SimulationRequest>(leakyBucketRules)) .WithStorage(storage) .WithError(new RateLimitError() { Code = 429, }) .Build()); }