System.Threading.RateLimiting 11.0.0-rc.1.26425.128

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
Polly.RateLimiting
Package Description
115
Polly.RateLimiting
Package Description
117
Polly.RateLimiting
Package Description
120
Polly.RateLimiting
Package Description
121
Polly.RateLimiting
Package Description
122
Polly.RateLimiting
Package Description
124
Polly.RateLimiting
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.
116
Polly.RateLimiting
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.
118
Polly.RateLimiting
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.
121
Polly.RateLimiting
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.
122
Polly.RateLimiting
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.
128
Polly.RateLimiting
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.
131
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages)
117
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages)
120
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages)
121
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages)
124
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages)
134
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages)
171

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

.NET 10.0

  • No dependencies.

.NET 11.0

  • No dependencies.

.NET Framework 4.6.2

.NET Standard 2.0

Version Downloads Last updated
11.0.0-rc.1.26425.128 8 09/10/2026
11.0.0-preview.7.26381.103 16 08/12/2026
11.0.0-preview.6.26359.118 22 07/17/2026
11.0.0-preview.5.26302.115 28 06/12/2026
11.0.0-preview.4.26230.115 46 05/13/2026
11.0.0-preview.3.26207.106 51 04/14/2026
11.0.0-preview.2.26159.112 42 03/12/2026
11.0.0-preview.1.26104.118 44 02/11/2026
10.0.12 8 09/08/2026
10.0.11 16 08/12/2026
10.0.10 23 07/17/2026
10.0.9 26 06/12/2026
10.0.8 33 05/13/2026
10.0.7 45 04/22/2026
10.0.6 42 04/14/2026
10.0.5 46 03/13/2026
10.0.4 42 03/12/2026
10.0.3 48 02/11/2026
10.0.2 58 01/13/2026
10.0.1 67 12/11/2025
10.0.0 81 11/11/2025
10.0.0-rc.2.25502.107 85 10/15/2025
10.0.0-rc.1.25451.107 93 09/10/2025
10.0.0-preview.7.25380.108 93 08/16/2025
10.0.0-preview.6.25358.103 113 07/16/2025
10.0.0-preview.5.25277.114 100 06/08/2025
10.0.0-preview.4.25258.110 111 05/16/2025
10.0.0-preview.3.25171.5 104 04/11/2025
10.0.0-preview.2.25163.2 126 04/02/2025
10.0.0-preview.1.25080.5 113 04/02/2025
9.0.20 7 09/12/2026
9.0.19 13 08/12/2026
9.0.18 20 07/18/2026
9.0.17 22 06/13/2026
9.0.16 40 05/13/2026
9.0.15 41 04/14/2026
9.0.14 44 03/12/2026
9.0.13 46 02/11/2026
9.0.12 49 01/13/2026
9.0.11 78 11/11/2025
9.0.10 94 10/15/2025
9.0.9 87 09/10/2025
9.0.8 107 08/05/2025
9.0.7 110 07/09/2025
9.0.6 102 06/12/2025
9.0.5 103 05/19/2025
9.0.4 127 04/11/2025
9.0.3 135 03/31/2025
9.0.2 121 03/31/2025
9.0.1 121 03/31/2025
9.0.0 119 11/14/2024
9.0.0-rc.2.24473.5 120 10/26/2024
9.0.0-rc.1.24431.7 115 10/26/2024
9.0.0-preview.7.24405.7 112 10/27/2024
9.0.0-preview.6.24327.7 114 10/31/2024
9.0.0-preview.5.24306.7 115 10/26/2024
9.0.0-preview.4.24266.19 109 10/30/2024
9.0.0-preview.3.24172.9 120 10/26/2024
9.0.0-preview.2.24128.5 110 10/26/2024
9.0.0-preview.1.24080.9 114 10/31/2024
8.0.0 133 10/26/2024
8.0.0-rc.2.23479.6 120 10/26/2024
8.0.0-rc.1.23419.4 121 10/30/2024
8.0.0-preview.7.23375.6 115 10/26/2024
8.0.0-preview.6.23329.7 129 10/26/2024
8.0.0-preview.5.23280.8 120 10/31/2024
8.0.0-preview.4.23259.5 120 10/26/2024
8.0.0-preview.3.23174.8 127 10/30/2024
8.0.0-preview.2.23128.3 118 10/31/2024
8.0.0-preview.1.23110.8 115 10/31/2024
7.0.1 114 10/26/2024
7.0.0 122 10/26/2024
7.0.0-rc.2.22472.3 107 10/26/2024
7.0.0-rc.1.22426.10 114 10/26/2024
7.0.0-preview.7.22375.6 122 10/26/2024
7.0.0-preview.6.22324.4 123 10/30/2024
7.0.0-preview.5.22301.12 119 10/26/2024
7.0.0-preview.4.22229.4 104 10/26/2024
7.0.0-preview.3.22175.4 115 10/26/2024
7.0.0-preview.2.22152.2 123 10/26/2024
7.0.0-preview.1.22076.8 122 10/30/2024