Skip to content

How to persist RateLimiter tokens? #489

Description

@DillionVVV

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions