1
0
Fork 0
mirror of https://github.com/Jackett/Jackett synced 2025-03-09 05:16:55 +00:00

core: migrate Polly to v8 (#14896)

This commit is contained in:
Bogdan 2023-12-10 17:52:12 +02:00 committed by GitHub
parent c5088ca09d
commit 5bc872e8be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 20 deletions

View file

@ -14,6 +14,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using Polly;
using Polly.Retry;
using static Jackett.Common.Models.IndexerConfig.ConfigurationData;
namespace Jackett.Common.Indexers
@ -416,31 +417,41 @@ namespace Jackett.Common.Indexers
}
}
private AsyncPolicy<WebResult> RetryPolicy
private ResiliencePipeline<WebResult> RetryStrategy
{
get
{
// Configure the retry policy
int attemptNumber = 1;
var retryPolicy = Policy
.HandleResult<WebResult>(r => (int)r.Status >= 500)
.Or<Exception>()
.WaitAndRetryAsync(
NumberOfRetryAttempts,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt) / 4),
onRetry: (exception, timeSpan, context) =>
var retryPipeline = new ResiliencePipelineBuilder<WebResult>()
.AddRetry(new RetryStrategyOptions<WebResult>
{
ShouldHandle = args => args.Outcome switch
{
if (exception.Result == null)
{ Result: { HasHttpServerError: true } } => PredicateResult.True(),
{ Result: { Status: System.Net.HttpStatusCode.RequestTimeout } } => PredicateResult.True(),
{ Exception: { } } => PredicateResult.True(),
_ => PredicateResult.False()
},
Delay = TimeSpan.FromSeconds(2),
MaxRetryAttempts = NumberOfRetryAttempts,
BackoffType = DelayBackoffType.Exponential,
UseJitter = true,
OnRetry = args =>
{
if (args.Outcome.Exception != null)
{
logger.Warn($"Request to {Name} failed with exception '{exception.Exception.Message}'. Retrying in {timeSpan.TotalSeconds}s... (Attempt {attemptNumber} of {NumberOfRetryAttempts}).");
logger.Warn("Request to {0} failed with exception '{1}'. Retrying in {2}s.", Name, args.Outcome.Exception.Message, args.RetryDelay.TotalSeconds);
}
else
{
logger.Warn($"Request to {Name} failed with status {exception.Result.Status}. Retrying in {timeSpan.TotalSeconds}s... (Attempt {attemptNumber} of {NumberOfRetryAttempts}).");
logger.Warn("Request to {0} failed with status {1}. Retrying in {2}s.", Name, args.Outcome.Result?.Status, args.RetryDelay.TotalSeconds);
}
attemptNumber++;
});
return retryPolicy;
return default;
}
})
.Build();
return retryPipeline;
}
}
@ -531,9 +542,9 @@ namespace Jackett.Common.Indexers
string referer = null, IEnumerable<KeyValuePair<string, string>> data = null,
Dictionary<string, string> headers = null, string rawbody = null, bool? emulateBrowser = null)
{
return await RetryPolicy.ExecuteAsync(async () =>
await RequestWithCookiesAsync(url, cookieOverride, method, referer, data, headers, rawbody, emulateBrowser)
);
return await RetryStrategy
.ExecuteAsync(async _ => await RequestWithCookiesAsync(url, cookieOverride, method, referer, data, headers, rawbody, emulateBrowser))
.ConfigureAwait(false);
}
protected virtual async Task<WebResult> RequestWithCookiesAsync(

View file

@ -24,7 +24,7 @@
<PackageReference Include="MimeMapping" Version="1.0.1.50" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.1.2" />
<PackageReference Include="polly" Version="7.2.3" />
<PackageReference Include="Polly" Version="8.2.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="6.0.0" />

View file

@ -66,6 +66,10 @@ namespace Jackett.Common.Utils.Clients
set => _contentString = value;
}
public bool HasHttpError => (int)Status >= 400;
public bool HasHttpServerError => (int)Status >= 500;
public bool IsRedirect => Status == HttpStatusCode.Redirect ||
Status == HttpStatusCode.RedirectKeepVerb ||
Status == HttpStatusCode.RedirectMethod ||