From 74110bdff6b3072483b6c4e831d826854e205402 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Fri, 12 Jul 2019 21:57:24 -0500 Subject: [PATCH] use RetryAfter response header in retry policy --- .../ServiceCollectionExtensions.cs | 2 ++ .../Polly/ServiceCollectionExtensions.cs | 36 +++++++++++++++++++ .../Web/ServiceCollectionExtensions.cs | 24 +++++-------- 3 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 src/FilterLists.Agent/Infrastructure/Polly/ServiceCollectionExtensions.cs diff --git a/src/FilterLists.Agent/Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs index d2b33e021..e544abab1 100644 --- a/src/FilterLists.Agent/Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs @@ -4,6 +4,7 @@ using FilterLists.Agent.Infrastructure.Disk; using FilterLists.Agent.Infrastructure.FilterListsApi; using FilterLists.Agent.Infrastructure.GitHub; +using FilterLists.Agent.Infrastructure.Polly; using FilterLists.Agent.Infrastructure.Web; using MediatR; using Microsoft.ApplicationInsights; @@ -23,6 +24,7 @@ public static void ConfigureServices(this IServiceCollection services) services.AddLocalization(); services.AddTransient(); services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies()); + services.AddPollyPolicyRegistry(); services.AddDiskServices(); services.AddFilterListsApiServices(); services.AddGitHubServices(); diff --git a/src/FilterLists.Agent/Infrastructure/Polly/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Infrastructure/Polly/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..52f64a75e --- /dev/null +++ b/src/FilterLists.Agent/Infrastructure/Polly/ServiceCollectionExtensions.cs @@ -0,0 +1,36 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Polly; +using Polly.Registry; + +namespace FilterLists.Agent.Infrastructure.Polly +{ + public static class ServiceCollectionExtensions + { + public static void AddPollyPolicyRegistry(this IServiceCollection services) + { + var registry = services.AddPolicyRegistry(); + + IAsyncPolicy waitAndRetryTooManyRequests = Policy + .HandleResult(r => r.StatusCode == HttpStatusCode.TooManyRequests) + .WaitAndRetryAsync(3, + (retryCount, response, context) => + response.Result?.Headers.RetryAfter.Delta ?? TimeSpan.FromMilliseconds(120), + async (response, timespan, retryCount, context) => + { + await Task.Run(() => + { + var logger = services.BuildServiceProvider().GetService>>(); + logger.LogInformation("Retrying after 429 TooManyRequests", + response.Result.RequestMessage.RequestUri, + response.Result?.Headers.RetryAfter.Delta.ToString(), retryCount); + }); + }); + registry.Add(nameof(waitAndRetryTooManyRequests), waitAndRetryTooManyRequests); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/Infrastructure/Web/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Infrastructure/Web/ServiceCollectionExtensions.cs index 9e64657ca..860b826b0 100644 --- a/src/FilterLists.Agent/Infrastructure/Web/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Infrastructure/Web/ServiceCollectionExtensions.cs @@ -1,10 +1,7 @@ -using System; -using System.Net; -using System.Net.Http; +using System.Net.Http; using FilterLists.Agent.Core.Lists; using FilterLists.Agent.Core.Urls; using Microsoft.Extensions.DependencyInjection; -using Polly; namespace FilterLists.Agent.Infrastructure.Web { @@ -12,18 +9,13 @@ public static class ServiceCollectionExtensions { public static void AddWebServices(this IServiceCollection services) { - services.AddHttpClient().AddTransientHttpErrorPolicy(b => - b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests) - .WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3))); - services.AddHttpClient() - .ConfigureHttpMessageHandlerBuilder(b => - { - b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false}; - b.Build(); - }) - .AddTransientHttpErrorPolicy(b => - b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests) - .WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3))); + services.AddHttpClient() + .AddPolicyHandlerFromRegistry("waitAndRetryTooManyRequests"); + services.AddHttpClient().ConfigureHttpMessageHandlerBuilder(b => + { + b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false}; + b.Build(); + }).AddPolicyHandlerFromRegistry("waitAndRetryTooManyRequests"); } } } \ No newline at end of file