I wanted to use this library and did some testing. This was my code:
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
context.HttpContext.Response.WriteAsync("Too many requests. Please try again later.");
return new ValueTask();
};
options.GlobalLimiter = PartitionedRateLimiter.CreateChained(
PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
{
string clientId = GetClientFromContext(httpContext);
return RedisRateLimitPartition.GetFixedWindowRateLimiter(clientId, _ =>
new RedisFixedWindowRateLimiterOptions
{
ConnectionMultiplexerFactory = () => connectionMultiplexer,
PermitLimit = 2,
Window = TimeSpan.FromSeconds(1)
});
}),
PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
{
string clientId = GetClientFromContext(httpContext);
return RateLimitPartition.GetNoLimiter(clientId);
}),
PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
{
string clientId = GetClientFromContext(httpContext);
return RedisRateLimitPartition.GetConcurrencyRateLimiter(clientId, _ =>
new RedisConcurrencyRateLimiterOptions
{
ConnectionMultiplexerFactory = () => connectionMultiplexer,
PermitLimit = 5,
QueueLimit = 10
});
})
);
It worked as expected for a while, and at some point I started getting this error from Redis:
"ERR Error running script (call to f_a08ae7b80fbefc1d082f3c02f112bb4f38a59fa7): @user_script:8: WRONGTYPE Operation against a key holding the wrong kind of value"
After a while (next day) the error disappeared and now its working fine again.