System.Threading.RateLimiting 9.0.0-preview.2.24128.5

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of System.Threading.RateLimiting.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package System.Threading.RateLimiting --version 9.0.0-preview.2.24128.5
NuGet\Install-Package System.Threading.RateLimiting -Version 9.0.0-preview.2.24128.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="System.Threading.RateLimiting" Version="9.0.0-preview.2.24128.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.Threading.RateLimiting --version 9.0.0-preview.2.24128.5
#r "nuget: System.Threading.RateLimiting, 9.0.0-preview.2.24128.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.Threading.RateLimiting as a Cake Addin
#addin nuget:?package=System.Threading.RateLimiting&version=9.0.0-preview.2.24128.5&prerelease

// Install System.Threading.RateLimiting as a Cake Tool
#tool nuget:?package=System.Threading.RateLimiting&version=9.0.0-preview.2.24128.5&prerelease

About

Provides a set of types that enable application developers to control the rate of operations. This can be used to ensure that applications do not exceed certain limits when interacting with resources or services.

Key Features

  • Flexible rate-limiting primitives that can be applied to various scenarios.
  • Supports token bucket, fixed window, and sliding window strategies.

How to Use

This is an example of an HttpClient that does client side rate limiting.

Define a rate limiter.

internal sealed class ClientSideRateLimitedHandler : DelegatingHandler, IAsyncDisposable
{
    private readonly RateLimiter _rateLimiter;

    public ClientSideRateLimitedHandler(RateLimiter limiter)
        : base(new HttpClientHandler())
    {
        _rateLimiter = limiter;
    }

    // Override the SendAsync method to apply rate limiting.
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // Try to acquire a token from the rate limiter.
        using RateLimitLease lease = await _rateLimiter.AcquireAsync(permitCount: 1, cancellationToken);

        // If a token is acquired, proceed with sending the request.
        if (lease.IsAcquired)
        {
            return await base.SendAsync(request, cancellationToken);
        }

        // If no token could be acquired, simulate a 429 Too Many Requests response.
        var response = new HttpResponseMessage(HttpStatusCode.TooManyRequests);

        // Add a 'Retry-After' header if the rate limiter provides a retry delay.
        if (lease.TryGetMetadata(MetadataName.RetryAfter, out TimeSpan retryAfter))
        {
            response.Headers.Add("Retry-After", ((int)retryAfter.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
        }

        return response;
    }

    // Implement IAsyncDisposable to allow for asynchronous cleanup of resources.
    public async ValueTask DisposeAsync()
    {
        // Dispose of the rate limiter asynchronously.
        await _rateLimiter.DisposeAsync().ConfigureAwait(false);

        // Call the base Dispose method.
        Dispose(disposing: false);

        // Suppress finalization.
        GC.SuppressFinalize(this);
    }

    // Dispose pattern to clean up the rate limiter.
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (disposing)
        {
            // Synchronously dispose of the rate limiter if disposing is true.
            _rateLimiter.Dispose();
        }
    }
}

Using the rate limiter.

using System.Globalization;
using System.Net;
using System.Threading.RateLimiting;

// Initialize the rate limiter options.
// TokenLimit: Maximum number of tokens that can be acquired at once.
// QueueProcessingOrder: The order in which queued requests will be processed.
// QueueLimit: Maximum number of queued requests.
// ReplenishmentPeriod: How often tokens are replenished.
// TokensPerPeriod: Number of tokens added each period.
// AutoReplenishment: If true, tokens are replenished automatically in the background.
var options = new TokenBucketRateLimiterOptions
{
    TokenLimit = 4,
    QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
    QueueLimit = 2,
    ReplenishmentPeriod = TimeSpan.FromMilliseconds(1),
    TokensPerPeriod = 2,
    AutoReplenishment = true
};

// Create a new instance of the TokenBucketRateLimiter with the defined options.
TokenBucketRateLimiter tokenBucketRateLimiter = new TokenBucketRateLimiter(options);

// A custom HttpMessageHandler that limits the rate of outgoing HTTP requests.
ClientSideRateLimitedHandler clientsideRateLimitedHandler = new ClientSideRateLimitedHandler(tokenBucketRateLimiter);

// Create an HttpClient that uses the rate-limited handler.
using HttpClient client = new HttpClient(clientsideRateLimitedHandler);

// Generate a list of dummy URLs for testing the rate limiter.
var oneHundredUrls = Enumerable.Range(0, 100).Select(i => $"https://example.com?iteration={i:00}");

// Issue concurrent HTTP GET requests using the HttpClient.
// The rate limiter will control how many requests are sent based on the defined limits.
await Parallel.ForEachAsync(oneHundredUrls.Take(0..100), async (url, cancellationToken) =>
{
    using HttpResponseMessage response = await client.GetAsync(url, cancellationToken);
    Console.WriteLine($"URL: {url}, HTTP status code: {response.StatusCode} ({(int)response.StatusCode})");
});

Main Types

The main types provided by this library are:

  • System.Threading.RateLimiting.RateLimiter
  • System.Threading.RateLimiting.ConcurrencyLimiter
  • System.Threading.RateLimiting.FixedWindowRateLimiter
  • System.Threading.RateLimiting.ReplenishingRateLimiter
  • System.Threading.RateLimiting.SlidingWindowRateLimiter
  • System.Threading.RateLimiting.TokenBucketRateLimiter
  • System.Threading.RateLimiting.PartitionedRateLimiter<TResource>

Additional Documentation

Feedback & Contributing

System.Threading.RateLimiting is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (33)

Showing the top 5 NuGet packages that depend on System.Threading.RateLimiting:

Package Downloads
Microsoft.AspNetCore.ConcurrencyLimiter The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

ASP.NET Core middleware for queuing incoming HTTP requests, to avoid threadpool starvation. This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/d4eca39c3fc1944b2c6431bf6b22036bdb176c0d

Polly.RateLimiting The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Polly.RateLimiting is a .NET resilience and transient-fault-handling library that allows developers to express resilience strategies using a Rate Limiter in a fluent and thread-safe manner.

RedisRateLimiting

Redis extensions for rate limiting

Microsoft.AspNetCore.RateLimiting The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

ASP.NET Core middleware for enforcing rate limiting in an application This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/b12b77b241f0a093d53508c3cb2084860bd5339d

Phoesion.Glow.SDK.Firefly.Abstractions The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Phoesion Glow SDK Abstractions

GitHub repositories (7)

Showing the top 5 popular GitHub repositories that depend on System.Threading.RateLimiting:

Repository Stars
App-vNext/Polly
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.
microsoft/dotnet-podcasts
.NET reference application shown at .NET Conf featuring ASP.NET Core, Blazor, .NET MAUI, Microservices, Orleans, Playwright, and more!
smartstore/Smartstore
A modular, scalable and ultra-fast open-source all-in-one eCommerce platform built on ASP.NET Core 7
mehdihadeli/food-delivery-microservices
🍔 A practical food delivery microservices, built with .Net 7, MassTransit, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
Version Downloads Last updated
9.0.0-preview.3.24172.9 123 4/11/2024
9.0.0-preview.2.24128.5 102 3/12/2024
9.0.0-preview.1.24080.9 337 2/13/2024
8.0.0 1,249,798 11/14/2023
8.0.0-rc.2.23479.6 734 10/10/2023
8.0.0-rc.1.23419.4 824 9/12/2023
8.0.0-preview.7.23375.6 930 8/8/2023
8.0.0-preview.6.23329.7 1,127 7/11/2023
8.0.0-preview.5.23280.8 1,708 6/13/2023
8.0.0-preview.4.23259.5 565 5/16/2023
8.0.0-preview.3.23174.8 410 4/11/2023
8.0.0-preview.2.23128.3 444 3/14/2023
8.0.0-preview.1.23110.8 723 2/21/2023
7.0.1 388,468 9/12/2023
7.0.0 1,916,583 11/7/2022
7.0.0-rc.2.22472.3 149,608 10/11/2022
7.0.0-rc.1.22426.10 50,727 9/14/2022
7.0.0-preview.7.22375.6 25,182 8/9/2022
7.0.0-preview.6.22324.4 28,576 7/12/2022
7.0.0-preview.5.22301.12 11,471 6/14/2022
7.0.0-preview.4.22229.4 12,034 5/10/2022
7.0.0-preview.3.22175.4 2,395 4/13/2022
7.0.0-preview.2.22152.2 268 3/14/2022
7.0.0-preview.1.22076.8 3,166 2/17/2022