Skip to content

ThrottlingTroll/ThrottlingTroll

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

460 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ThrottlingTroll

Rate limiting/throttling/circuit-breaking middleware for ASP.NET and Azure Functions.

.NET Nuget

Nuget Nuget Nuget

Install from Nuget:

.NET Azure Functions
dotnet add package ThrottlingTroll dotnet add package ThrottlingTroll.AzureFunctions

Features

Supported rate limiting algorithms

  • FixedWindow. No more than PermitLimit requests are allowed in IntervalInSeconds. Here is an illustration for the case of no more than 2 requests per each 8 seconds:

    The typical drawback of FixedWindow algorithm is that you'd get request rate bursts at the end of each window. So specifically to cope that we have

  • SlidingWindow. No more than PermitLimit requests are allowed in IntervalInSeconds, but that interval is split into NumOfBuckets. The main benefit of this algorithm over FixedWindow is that if a client constantly exceedes PermitLimit, it will never get any valid response and will always get 429 TooManyRequests. Here is an illustration for the case of no more than 2 requests per each 8 seconds with 2 buckets:

    In other words, with SlidingWindow your service gets a smoother request rate.

  • LeakyBucket. No more than PermitLimit failures are allowed in IntervalInSeconds, and the allowed requests get evenly distributed across the given time frame (a delay is added according to the request's position in the "queue"). Eliminates spikes whatsoever.

    image
  • Semaphore aka Concurrency Limiter. No more than PermitLimit requests are allowed to be executed concurrently. Here is an illustration for the case of no more than 3 concurrent requests:

    If you set Semaphore's PermitLimit to 1 and use RedisCounterStore, then ThrottlingTroll will act as a distributed lock. If you add an IdentityIdExtractor (identifying requests by e.g. a query string parameter), then it will turn into named distributed locks.

  • CircuitBreaker. No more than PermitLimit failures are allowed in IntervalInSeconds. Once the failure limit is exceeded, goes into Trial mode. In Trial mode one request per TrialIntervalInSeconds is allowed to pass through. Once that request succeeds, goes back to normal.

    [video is coming]

You can find it in our Wiki.

Most concepts and features are the same for all supported platforms. Things that are specific to each platform are highlighted in the relevant READMEs:

ASP.NET Azure Functions
How to use with ASP.NET How to use with Azure Functions

Samples

Full minimalistic sample using ASP.NET Minimal API:

using ThrottlingTroll;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello ThrottlingTroll!");

// Limiting to 1 request per 2 seconds
app.UseThrottlingTroll(options =>
{
    options.Config = new ThrottlingTrollConfig 
    {
        Rules =
        [
            new ThrottlingTrollRule
            {
                LimitMethod = new FixedWindowRateLimitMethod
                {
                    PermitLimit = 1,
                    IntervalInSeconds = 2
                }
            }
        ]
    };
});

app.Run();

Comprehensive sample projects that demonstrate all the above concepts are located in separate repos:

ASP.NET Core Azure Functions
ThrottlingTroll-AspDotNetCore-Samples ThrottlingTroll-AzureFunctions-Samples

Contributing

Is very much welcomed.

About

Rate limiting/throttling/circuit-breaking library for ASP.NET, Azure Functions and, in fact, any .NET app. Supports Redis and many other distributed counter stores.

Topics

Resources

License

Stars

129 stars

Watchers

2 watching

Forks

Packages

 
 
 

Contributors