/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Identity/test/Identity.Test/RetryHandler.cs
61 строка
2 KB
Shreyas Jejurkar
chore : Remove unwanted `using` statement (#39176)
02 янв 2022, 17:39
Не верифицирован
02 янв 2022, 17:39
c85baf8
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Net.Http; using Xunit.Abstractions; namespace Microsoft.AspNetCore.Identity.Test; internal class RetryHandler : DelegatingHandler { private readonly ITestOutputHelper _output; private readonly int _maxRetries; private TimeSpan _waitIntervalBeforeRetry; public RetryHandler( HttpClientHandler httpClientHandler, ITestOutputHelper output, TimeSpan initialWaitTime, int maxAttempts) : base(httpClientHandler) { _waitIntervalBeforeRetry = initialWaitTime; _output = output; _maxRetries = maxAttempts; } protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { HttpResponseMessage result = null; var url = request.RequestUri; var method = request.Method; for (var i = 0; i < _maxRetries; i++) { try { _output.WriteLine($"Sending request '{method} - {url}' {i + 1} attempt."); result = await base.SendAsync(request, cancellationToken); if (result.IsSuccessStatusCode) { return result; } else { _output.WriteLine($"Request '{method} - {url}' failed with {result.StatusCode}."); } } catch (Exception e) { _output.WriteLine($"Request '{method} - {url}' failed with {e.ToString()}"); } finally { await Task.Delay(_waitIntervalBeforeRetry, cancellationToken); _waitIntervalBeforeRetry = _waitIntervalBeforeRetry * 2; } } // Try one last time to show the actual error. return await base.SendAsync(request, cancellationToken); } }