System.Threading.RateLimiting 9.0.23

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.

Showing the top 20 packages that depend on System.Threading.RateLimiting.

Packages Downloads
Microsoft.AspNetCore.ConcurrencyLimiter
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/fa4d80b76c2431a825be026f6bbabca63e1f42ef
20
Microsoft.AspNetCore.ConcurrencyLimiter
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/d62be99ba6e73feb46d7b64a6b4ce8610dc9040a
18
Microsoft.AspNetCore.ConcurrencyLimiter
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/63d1187a01b82719c2891cecc74ee3d51ce892a8
16
Microsoft.AspNetCore.ConcurrencyLimiter
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/abc2c7226ff616316cd1e05d76e6c36b49ce06a1
16
Microsoft.AspNetCore.ConcurrencyLimiter
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/de35e2b0a0d8d5d1e307907983a6838da1092898
16
Microsoft.AspNetCore.ConcurrencyLimiter
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/763b4ef31ca4df6dae07c7ee8f39ea259b6980fa
16
Microsoft.AspNetCore.ConcurrencyLimiter
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/8486d31e24f30e3fa1809a95699a0adc16f448d7
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/af22effae4069a5dfb9b0735859de48820104f5b
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/febee99db845fd8766a13bdb391a07c3ee90b4ba
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/cccab50fba1c7335fdb48a735246374b5ddd7e69
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/5ae8106f83d4d23cd0a2d2474c1b15e4d5dfc9eb
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/b225d30209cf027c80b741d04efe1c54f2431b3c
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/b1beadfe0ad3d02eb5207681a5328793c4b3822c
15
Microsoft.Data.SqlClient
The current data provider for SQL Server and Azure SQL databases. This has replaced System.Data.SqlClient. These classes provide access to SQL and encapsulate database-specific protocols, including tabular data stream (TDS). Commonly Used Types: Microsoft.Data.SqlClient.SqlConnection Microsoft.Data.SqlClient.SqlException Microsoft.Data.SqlClient.SqlParameter Microsoft.Data.SqlClient.SqlDataReader Microsoft.Data.SqlClient.SqlCommand Microsoft.Data.SqlClient.SqlTransaction Microsoft.Data.SqlClient.SqlParameterCollection Microsoft.Data.SqlClient.SqlClientFactory When using NuGet 3.x this package requires at least version 3.4.
15
Microsoft.AspNetCore.ConcurrencyLimiter
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/b96167fbfe8bd45d94e4dcda42c7d09eb5745459
14
Microsoft.AspNetCore.ConcurrencyLimiter
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/954f61dd38b33caa2b736c73530bd5a294174437
14
Microsoft.AspNetCore.ConcurrencyLimiter
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/4442a188f9200a57635373dcd640893c0e8dcc78
14
Microsoft.AspNetCore.ConcurrencyLimiter
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/cb14812a5c6098bd1091f5b7fd471ce2be45e8dd
14
Microsoft.AspNetCore.ConcurrencyLimiter
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/c78f4a816c0c6a978a801a05a31dce9013e099e2
14
Microsoft.AspNetCore.ConcurrencyLimiter
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/d634f2bd1ad6e319f26ff0f1d7ada5539158a19f
14

https://go.microsoft.com/fwlink/?LinkID=799421

.NET Framework 4.6.2

.NET 8.0

  • No dependencies.

.NET 9.0

  • No dependencies.

.NET Standard 2.0

Version Downloads Last updated
11.0.0 1 11/19/2026
11.0.0-rc.3.26522.148 1 10/31/2026
11.0.0-rc.2.26504.105 2 10/19/2026
11.0.0-rc.1.26425.128 4 09/24/2026
11.0.0-preview.7.26381.103 4 08/20/2026
11.0.0-preview.6.26359.118 5 07/31/2026
11.0.0-preview.5.26302.115 5 07/31/2026
11.0.0-preview.4.26230.115 6 05/31/2026
11.0.0-preview.3.26207.106 5 05/31/2026
11.0.0-preview.2.26159.112 5 03/28/2026
11.0.0-preview.1.26104.118 9 03/05/2026
10.0.15 1 11/17/2026
10.0.14 1 11/18/2026
10.0.13 3 10/19/2026
10.0.12 5 09/24/2026
10.0.11 4 08/20/2026
10.0.10 6 07/31/2026
10.0.9 6 07/31/2026
10.0.8 6 05/31/2026
10.0.7 6 04/23/2026
10.0.6 6 04/23/2026
10.0.5 6 03/28/2026
10.0.4 4 03/28/2026
10.0.3 7 03/07/2026
10.0.2 3 01/27/2026
10.0.1 5 12/31/2025
10.0.0 3 12/10/2025
10.0.0-rc.2.25502.107 5 12/10/2025
10.0.0-rc.1.25451.107 5 12/10/2025
10.0.0-preview.7.25380.108 1 12/11/2025
10.0.0-preview.6.25358.103 0 12/11/2025
10.0.0-preview.5.25277.114 0 12/11/2025
10.0.0-preview.4.25258.110 0 12/11/2025
10.0.0-preview.3.25171.5 0 12/10/2025
10.0.0-preview.2.25163.2 0 12/10/2025
10.0.0-preview.1.25080.5 0 12/10/2025
9.0.23 1 11/19/2026
9.0.22 1 11/07/2026
9.0.21 3 10/19/2026
9.0.20 5 09/24/2026
9.0.19 4 08/20/2026
9.0.18 6 07/31/2026
9.0.17 6 07/31/2026
9.0.16 6 05/31/2026
9.0.15 6 04/23/2026
9.0.14 7 03/28/2026
9.0.13 9 03/06/2026
9.0.12 2 01/27/2026
9.0.11 3 01/01/2026
9.0.10 5 12/10/2025
9.0.9 4 12/26/2025
9.0.8 5 12/30/2025
9.0.7 4 12/30/2025
9.0.6 6 12/30/2025
9.0.5 6 12/30/2025
9.0.4 5 12/30/2025
9.0.3 4 12/26/2025
9.0.2 4 12/30/2025
9.0.1 4 12/30/2025
9.0.0 4 12/26/2025
9.0.0-rc.2.24473.5 2 12/10/2025
9.0.0-rc.1.24431.7 3 12/10/2025
9.0.0-preview.7.24405.7 12 12/10/2025
9.0.0-preview.6.24327.7 0 12/10/2025
9.0.0-preview.5.24306.7 0 12/11/2025
9.0.0-preview.4.24266.19 0 12/11/2025
9.0.0-preview.3.24172.9 0 12/10/2025
9.0.0-preview.2.24128.5 0 12/11/2025
9.0.0-preview.1.24080.9 0 12/10/2025
8.0.0 9 12/26/2025
8.0.0-rc.2.23479.6 10 12/10/2025
8.0.0-rc.1.23419.4 9 01/01/2026
8.0.0-preview.7.23375.6 13 12/10/2025
8.0.0-preview.6.23329.7 12 12/10/2025
8.0.0-preview.5.23280.8 11 12/10/2025
8.0.0-preview.4.23259.5 12 12/10/2025
8.0.0-preview.3.23174.8 13 12/10/2025
8.0.0-preview.2.23128.3 12 12/10/2025
8.0.0-preview.1.23110.8 13 01/01/2026
7.0.1 13 12/30/2025
7.0.0 10 12/26/2025
7.0.0-rc.2.22472.3 6 12/10/2025
7.0.0-rc.1.22426.10 10 12/10/2025
7.0.0-preview.7.22375.6 16 12/10/2025
7.0.0-preview.6.22324.4 11 12/10/2025
7.0.0-preview.5.22301.12 11 12/10/2025
7.0.0-preview.4.22229.4 12 12/10/2025
7.0.0-preview.3.22175.4 12 12/10/2025
7.0.0-preview.2.22152.2 10 12/10/2025
7.0.0-preview.1.22076.8 9 12/10/2025