aspnetcore-redis-rate-limiting
aspnetcore-redis-rate-limiting copied to clipboard
Use dependency injection for IConnectionMultiplexer
I'm trying to figure out what needs to be done here, would it be possible to add some description?
I don't really like the fact that we have to send the 'ConnectionMultiplexer' as an option to the rate limiters - wanted to somehow get it behind-the-hood from the Services Container.
However, I don't think there's much we can do for now - we're limited by the way the framework's rate limiting middleware works and is configured
If wanted you can use the HTTP context RequestServices more details here, or can use IPostConfigureOptions<RateLimiterOptions>.
Maybe this example will help someone:
public static RateLimiterOptions AddRedisFixedWindowLimiter(this RateLimiterOptions options, string policyName,
Action<RedisFixedWindowRateLimiterOptions, IServiceProvider> configure)
{
ArgumentNullException.ThrowIfNull(configure, nameof(configure));
var key = new PolicyNameKey(policyName);
return options.AddPolicy(policyName, (context) =>
RedisRateLimitPartition.GetFixedWindowRateLimiter(key, (_) =>
{
var windowsOptions = new RedisFixedWindowRateLimiterOptions();
configure(windowsOptions, context.RequestServices);
return windowsOptions;
}));
}
when you redesign this , please keep in mind that IConnectionMultiplexer can come from another application (system) , right now it is ok because there is a public property for IConnectionMultiplexer