mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
use RetryAfter response header in retry policy
This commit is contained in:
parent
133410991e
commit
74110bdff6
3 changed files with 46 additions and 16 deletions
|
|
@ -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<Parser>();
|
||||
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
|
||||
services.AddPollyPolicyRegistry();
|
||||
services.AddDiskServices();
|
||||
services.AddFilterListsApiServices();
|
||||
services.AddGitHubServices();
|
||||
|
|
|
|||
|
|
@ -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<HttpResponseMessage> waitAndRetryTooManyRequests = Policy
|
||||
.HandleResult<HttpResponseMessage>(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<ILogger<IPolicyRegistry<string>>>();
|
||||
logger.LogInformation("Retrying after 429 TooManyRequests",
|
||||
response.Result.RequestMessage.RequestUri,
|
||||
response.Result?.Headers.RetryAfter.Delta.ToString(), retryCount);
|
||||
});
|
||||
});
|
||||
registry.Add(nameof(waitAndRetryTooManyRequests), waitAndRetryTooManyRequests);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IListRepository, ListRepository>().AddTransientHttpErrorPolicy(b =>
|
||||
b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
.WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
|
||||
services.AddHttpClient<IUrlValidator, UrlValidator>()
|
||||
.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<IListRepository, ListRepository>()
|
||||
.AddPolicyHandlerFromRegistry("waitAndRetryTooManyRequests");
|
||||
services.AddHttpClient<IUrlValidator, UrlValidator>().ConfigureHttpMessageHandlerBuilder(b =>
|
||||
{
|
||||
b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
|
||||
b.Build();
|
||||
}).AddPolicyHandlerFromRegistry("waitAndRetryTooManyRequests");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue