![]() |
VOOZH | about |
dotnet add package System.Threading.RateLimiting --version 10.0.9
NuGet\Install-Package System.Threading.RateLimiting -Version 10.0.9
<PackageReference Include="System.Threading.RateLimiting" Version="10.0.9" />
<PackageVersion Include="System.Threading.RateLimiting" Version="10.0.9" />Directory.Packages.props
<PackageReference Include="System.Threading.RateLimiting" />Project file
paket add System.Threading.RateLimiting --version 10.0.9
#r "nuget: System.Threading.RateLimiting, 10.0.9"
#:package System.Threading.RateLimiting@10.0.9
#addin nuget:?package=System.Threading.RateLimiting&version=10.0.9Install as a Cake Addin
#tool nuget:?package=System.Threading.RateLimiting&version=10.0.9Install as a Cake Tool
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.
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})");
});
The main types provided by this library are:
System.Threading.RateLimiting.RateLimiterSystem.Threading.RateLimiting.ConcurrencyLimiterSystem.Threading.RateLimiting.FixedWindowRateLimiterSystem.Threading.RateLimiting.ReplenishingRateLimiterSystem.Threading.RateLimiting.SlidingWindowRateLimiterSystem.Threading.RateLimiting.TokenBucketRateLimiterSystem.Threading.RateLimiting.PartitionedRateLimiter<TResource>System.Threading.RateLimiting is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on System.Threading.RateLimiting:
| Package | Downloads |
|---|---|
|
RabbitMQ.Client
The RabbitMQ .NET client is the official client library for C# (and, implicitly, other .NET languages) |
|
|
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. |
|
|
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/dotnet/tree/901ca941248413c79832d2fdbd709da0c4386353 |
|
|
Microsoft.AspNetCore.RateLimiting
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 |
|
|
RedisRateLimiting
Redis extensions for rate limiting |
Showing the top 15 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+.
|
|
|
dotnetcore/DotnetSpider
DotnetSpider, a .NET standard web crawling library. It is lightweight, efficient and fast high-level web crawling & scraping framework
|
|
|
microsoft/dotnet-podcasts
.NET reference application shown at .NET Conf featuring ASP.NET Core, Blazor, .NET MAUI, Microservices, Orleans, Playwright, and more!
|
|
|
Cleanuparr/Cleanuparr
Advanced download manager for the Servarr ecosystem
|
|
|
rabbitmq/rabbitmq-dotnet-client
RabbitMQ .NET client for .NET Standard 2.0+ and .NET 4.6.2+
|
|
|
RRQM/TouchSocket
TouchSocket is an integrated .NET networking framework that includes modules for socket, TCP, UDP, SSL, named pipes, HTTP, WebSocket, RPC, and more. It offers a one-stop solution for TCP packet issues and enables quick implementation of custom data message parsing using protocol templates.
|
|
|
fiddyschmitt/File-Tunnel
Tunnel TCP connections through a file
|
|
|
Sidekick-Poe/Sidekick
The main repository for the Sidekick project, a companion trade tool for Path of Exile and Path of Exile 2.
|
|
|
SnapHutaoRemasteringProject/Snap.Hutao.Remastered
实用的开源多功能原神工具箱 🧰 / Multifunctional Open-source Genshin Impact Toolkit 🧰
|
|
|
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
|
|
|
cristipufu/aspnetcore-redis-rate-limiting
Set up a Redis backplane for ASP.NET Core multi-node deployments
|
|
|
alnkesq/AppViewLite
A Bluesky appview focused on low resource consumption
|
|
| TradeOnSolutions/Steam-Desktop-Authenticator | |
|
ydb-platform/ydb-dotnet-sdk
YDB .NET SDK
|
|
|
wangdage12/Snap.Hutao
「新」稳定版胡桃工具箱
|
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.0-preview.5.26302.115 | 108 | 6/9/2026 |
| 11.0.0-preview.4.26230.115 | 282 | 5/12/2026 |
| 11.0.0-preview.3.26207.106 | 819 | 4/14/2026 |
| 11.0.0-preview.2.26159.112 | 458 | 3/10/2026 |
| 11.0.0-preview.1.26104.118 | 747 | 2/10/2026 |
| 10.0.9 | 27,980 | 6/9/2026 |
| 10.0.8 | 128,952 | 5/12/2026 |
| 10.0.7 | 203,926 | 4/21/2026 |
| 10.0.6 | 58,136 | 4/14/2026 |
| 10.0.5 | 570,733 | 3/12/2026 |
| 10.0.4 | 25,365 | 3/10/2026 |
| 10.0.3 | 406,916 | 2/10/2026 |
| 10.0.2 | 245,121 | 1/13/2026 |
| 10.0.1 | 343,948 | 12/9/2025 |
| 9.0.17 | 756 | 6/9/2026 |
| 9.0.16 | 15,467 | 5/12/2026 |
| 9.0.15 | 17,679 | 4/14/2026 |
| 9.0.14 | 32,880 | 3/10/2026 |
| 9.0.13 | 13,560 | 2/10/2026 |
| 9.0.12 | 19,982 | 1/13/2026 |