/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/core/extensions/snippets/ratelimit/http/Program.cs
41 строка
1 KB
David Pine
Upgrade dotnet 7 (#32345)
08 ноя 2022, 22:53
Не верифицирован
08 ноя 2022, 22:53
2aca282
Код
Авторство
О чём код?
var options = new TokenBucketRateLimiterOptions { TokenLimit = 8, QueueProcessingOrder = QueueProcessingOrder.OldestFirst, QueueLimit = 3, ReplenishmentPeriod = TimeSpan.FromMilliseconds(1), TokensPerPeriod = 2, AutoReplenishment = true }; // Create an HTTP client with the client-side rate limited handler. using HttpClient client = new( handler: new ClientSideRateLimitedHandler( limiter: new TokenBucketRateLimiter(options))); // Create 100 urls with a unique query string. var oneHundredUrls = Enumerable.Range(0, 100).Select( i => $"https://example.com?iteration={i:0#}"); // Flood the HTTP client with requests. var floodOneThroughFortyNineTask = Parallel.ForEachAsync( source: oneHundredUrls.Take(0..49), body: (url, cancellationToken) => GetAsync(client, url, cancellationToken)); var floodFiftyThroughOneHundredTask = Parallel.ForEachAsync( source: oneHundredUrls.Take(^50..), body: (url, cancellationToken) => GetAsync(client, url, cancellationToken)); await Task.WhenAll( floodOneThroughFortyNineTask, floodFiftyThroughOneHundredTask); static async ValueTask GetAsync( HttpClient client, string url, CancellationToken cancellationToken) { using var response = await client.GetAsync(url, cancellationToken); Console.WriteLine( $"URL: {url}, HTTP status code: {response.StatusCode} ({(int)response.StatusCode})"); }