Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to persist RateLimiter tokens? #489

Open
DillionVVV opened this issue Oct 2, 2024 · 1 comment
Open

How to persist RateLimiter tokens? #489

DillionVVV opened this issue Oct 2, 2024 · 1 comment

Comments

@DillionVVV
Copy link

I am using .Net RateLimiting and specifically the TokenBucketRateLimiter. Example:

services.AddRateLimiter(limiterOptions =>
{
    limiterOptions.AddPolicy(userPolicyName, context =>
    {
       string username = Utils.getUsername(context);
           return RateLimitPartition.GetTokenBucketLimiter
           (username , _ =>
               new TokenBucketRateLimiterOptions
               {
                   TokenLimit = myOptions.TokenLimit,
                   QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                   QueueLimit = 0,
                   ReplenishmentPeriod = TimeSpan.FromDays(myOptions.ReplenishmentPeriod),
                   TokensPerPeriod = myOptions.ReplenishmentPeriod,
                   AutoReplenishment = myOptions.AutoReplenishment
               });  
    });
});

This works, but doesn't survive service restarts (tokens get resetted every time the service restarts). From what I could find, it's using internally a memory store for tokens.

How can I change the store used for tokens? I'd prefer to use postgres for persistence if possible, but any kind of persistence will do.

@cristipufu
Copy link
Collaborator

You need to use a persistent store (eg: Redis). Check this out https://github.com/cristipufu/aspnetcore-redis-rate-limiting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants