aspnetcore-redis-rate-limiting icon indicating copy to clipboard operation
aspnetcore-redis-rate-limiting copied to clipboard

Use dependency injection for IConnectionMultiplexer

Open cristipufu opened this issue 2 years ago • 4 comments

cristipufu avatar Nov 06 '22 14:11 cristipufu

I'm trying to figure out what needs to be done here, would it be possible to add some description?

GimmeDaKitty avatar Nov 27 '22 21:11 GimmeDaKitty

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

cristipufu avatar Nov 28 '22 09:11 cristipufu

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;
        }));
}

BenasBud avatar Mar 31 '23 13:03 BenasBud

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

infofromca avatar Oct 07 '24 14:10 infofromca