Skip to content

Comments

Sergei shinkarevich#291

Closed
shblsh wants to merge 4 commits intocrexi-dev:masterfrom
shblsh:sergei-shinkarevich
Closed

Sergei shinkarevich#291
shblsh wants to merge 4 commits intocrexi-dev:masterfrom
shblsh:sergei-shinkarevich

Conversation

@shblsh
Copy link

@shblsh shblsh commented Mar 3, 2025

Usage

Register and configure Rate limiting services(rules)

Example:

builder.Services.
    AddFixedWindow(configure =>
    {
        configure.RuleConditions = new List<Func<AccessToken, bool>>
        {
            token => token.Region == Region.us,
            token => !string.IsNullOrEmpty(token.UserId)
        };

        configure.Limit = 10;
        configure.WindowSize = TimeSpan.FromMinutes(2);
    }).
    AddTimeBasedRateLimiting(configure =>
    {
        configure.RuleConditions = new List<Func<AccessToken, bool>>
        {
            token => token.Region == Region.us || token.Region == Region.eu,
            token => !string.IsNullOrEmpty(token.UserId)
        };

        configure.MinTimeBetweenRequests = TimeSpan.FromSeconds(10);
    });

Rules are applied based on conditions. If the condition is not specified then it applies to all requests

    configure.RuleConditions = new List<Func<AccessToken, bool>>
    {
        token => token.Region == Region.us,
        token => !string.IsNullOrEmpty(token.UserId)
    };

Set up the RateLimitMiddleware

Add the RateLimit middleware to your Configure method in the Startup class or directly into your IWebHostBuilder.

Example:

app.UseRateLimiting();

@shblsh shblsh closed this Jan 12, 2026
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

Successfully merging this pull request may close these issues.

1 participant