Skip to content

ThrottlingTroll/ThrottlingTroll

Repository files navigation

ThrottlingTroll

Rate limiting/throttling middleware for ASP.NET Core and Azure Functions.

.NET Nuget

Nuget Nuget Nuget

Install from Nuget:

ASP.NET Core Azure Functions Azure Functions with ASP.NET Core Integration
dotnet add package ThrottlingTroll dotnet add package ThrottlingTroll.AzureFunctions dotnet add package ThrottlingTroll.AzureFunctionsAspNet

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.

  • 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.

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 Core Azure Functions Azure Functions with ASP.NET Core Integration
How to use with ASP.NET Core How to use with Azure Functions How to use with Azure Functions ASP.NET Core Integration

Samples

Full minimalistic sample using ASP.NET Core 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();

Sample projects that demonstrate all the above concepts:

ASP.NET Core Azure Functions
ThrottlingTrollSampleWeb ThrottlingTrollSampleFunction
ThrottlingTrollSampleAspNetFunction
ThrottlingTrollSampleDotNet6InProcDurableFunction

Contributing

Is very much welcomed.