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

Sergei shinkarevich #291

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

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

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