Added logging to HttpRetryHandler to log priority-based throttling events.

This commit is contained in:
Tomek Melissa 2021-07-09 00:46:12 +02:00
Родитель 0a1ea98155
Коммит a775b50475
4 изменённых файлов: 35 добавлений и 13 удалений

Просмотреть файл

@ -83,6 +83,7 @@
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
@ -90,7 +91,7 @@
<Version>2.0.12</Version>
</PackageReference>
<PackageReference Include="Microsoft.Identity.Client">
<Version>4.31.0</Version>
<Version>4.34.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory">
<Version>5.2.9</Version>
@ -105,7 +106,7 @@
<Version>3.3.2</Version>
</PackageReference>
<PackageReference Include="System.Linq.Dynamic.Core">
<Version>1.2.9</Version>
<Version>1.2.10</Version>
</PackageReference>
<PackageReference Include="UrlCombine">
<Version>2.0.0</Version>

Просмотреть файл

@ -52,7 +52,7 @@ namespace RecurringIntegrationsScheduler.Common.Helpers
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
_httpClient = new HttpClient(new HttpRetryHandler(httpClientHandler, _settings.RetryCount, _settings.RetryDelay))
_httpClient = new HttpClient(new HttpRetryHandler(httpClientHandler, _settings))
{
Timeout = TimeSpan.FromMinutes(60) //Timeout for large uploads or downloads
};

Просмотреть файл

@ -1,6 +1,8 @@
/* Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. */
using log4net;
using RecurringIntegrationsScheduler.Common.JobSettings;
using System;
using System.Linq;
using System.Net.Http;
@ -11,14 +13,19 @@ namespace RecurringIntegrationsScheduler.Common.Helpers
{
public class HttpRetryHandler : DelegatingHandler
{
private readonly int maxRetries;
private int delayBetweenRetries;
private int _retryAfter;
private readonly Settings _settings;
/// <summary>
/// The log
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public HttpRetryHandler(HttpMessageHandler innerHandler, int retries = 3, int delay = 1)
public HttpRetryHandler(HttpMessageHandler innerHandler, Settings jobSettings)
: base(innerHandler)
{
maxRetries = retries;
delayBetweenRetries = delay;
_settings = jobSettings;
_retryAfter = _settings.RetryDelay;
log4net.Config.XmlConfigurator.Configure();
}
protected override async Task<HttpResponseMessage> SendAsync(
@ -26,7 +33,7 @@ namespace RecurringIntegrationsScheduler.Common.Helpers
CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
for (int i = 0; i < maxRetries; i++)
for (int i = 0; i < _settings.RetryCount; i++)
{
response = await base.SendAsync(request, cancellationToken);
if (response.IsSuccessStatusCode)
@ -35,15 +42,29 @@ namespace RecurringIntegrationsScheduler.Common.Helpers
}
if ((int)response.StatusCode == 429)
{
i--; //Explicit ask for retry. Try until successful
if (response.Headers.Contains("Retry-After"))
{
delayBetweenRetries = int.Parse(response.Headers.GetValues("Retry-After").FirstOrDefault());
_retryAfter = int.Parse(response.Headers.GetValues("Retry-After").FirstOrDefault());
if (_settings.LogVerbose || Log.IsDebugEnabled)
{
Log.Debug($@"Job: {_settings.JobKey}. HttpRetryHandler.Task is being called.
ReqeuestUri: {request.RequestUri}
Response Status Code: {response.StatusCode}
Priority-based throttling in action.
Requested delay (in seconds) between next request: {_retryAfter}");
}
}
}
if (delayBetweenRetries > 0 )
if (_retryAfter > 0 && _settings.RetryCount > 1)
{
Thread.Sleep(TimeSpan.FromSeconds(delayBetweenRetries));
if (_settings.LogVerbose || Log.IsDebugEnabled)
{
Log.Debug($@"Job: {_settings.JobKey}. HttpRetryHandler.Task is being called.
Delaying next request for {_retryAfter} seconds...");
}
Thread.Sleep(TimeSpan.FromSeconds(_retryAfter));
}
}
return response;

Просмотреть файл

@ -2,4 +2,4 @@
Licensed under the MIT License. */
using System.Reflection;
[assembly: AssemblyVersion("3.3.0.0")]
[assembly: AssemblyVersion("3.3.1.0")]