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
3 changed files with 35 additions and 20 deletions

View File

@@ -14,6 +14,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NLog; using NLog;
using Polly; using Polly;
using Polly.Retry;
using static Jackett.Common.Models.IndexerConfig.ConfigurationData; using static Jackett.Common.Models.IndexerConfig.ConfigurationData;
namespace Jackett.Common.Indexers namespace Jackett.Common.Indexers
@@ -416,31 +417,41 @@ namespace Jackett.Common.Indexers
} }
} }
private AsyncPolicy<WebResult> RetryPolicy private ResiliencePipeline<WebResult> RetryStrategy
{ {
get get
{ {
// Configure the retry policy var retryPipeline = new ResiliencePipelineBuilder<WebResult>()
int attemptNumber = 1; .AddRetry(new RetryStrategyOptions<WebResult>
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) =>
{ {
if (exception.Result == null) ShouldHandle = args => args.Outcome switch
{ {
logger.Warn($"Request to {Name} failed with exception '{exception.Exception.Message}'. Retrying in {timeSpan.TotalSeconds}s... (Attempt {attemptNumber} of {NumberOfRetryAttempts})."); { 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 {0} failed with exception '{1}'. Retrying in {2}s.", Name, args.Outcome.Exception.Message, args.RetryDelay.TotalSeconds);
} }
else 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 default;
return retryPolicy; }
})
.Build();
return retryPipeline;
} }
} }
@@ -531,9 +542,9 @@ namespace Jackett.Common.Indexers
string referer = null, IEnumerable<KeyValuePair<string, string>> data = null, string referer = null, IEnumerable<KeyValuePair<string, string>> data = null,
Dictionary<string, string> headers = null, string rawbody = null, bool? emulateBrowser = null) Dictionary<string, string> headers = null, string rawbody = null, bool? emulateBrowser = null)
{ {
return await RetryPolicy.ExecuteAsync(async () => return await RetryStrategy
await RequestWithCookiesAsync(url, cookieOverride, method, referer, data, headers, rawbody, emulateBrowser) .ExecuteAsync(async _ => await RequestWithCookiesAsync(url, cookieOverride, method, referer, data, headers, rawbody, emulateBrowser))
); .ConfigureAwait(false);
} }
protected virtual async Task<WebResult> RequestWithCookiesAsync( protected virtual async Task<WebResult> RequestWithCookiesAsync(

View File

@@ -24,7 +24,7 @@
<PackageReference Include="MimeMapping" Version="1.0.1.50" /> <PackageReference Include="MimeMapping" Version="1.0.1.50" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.1.2" /> <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="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" /> <PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="6.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; set => _contentString = value;
} }
public bool HasHttpError => (int)Status >= 400;
public bool HasHttpServerError => (int)Status >= 500;
public bool IsRedirect => Status == HttpStatusCode.Redirect || public bool IsRedirect => Status == HttpStatusCode.Redirect ||
Status == HttpStatusCode.RedirectKeepVerb || Status == HttpStatusCode.RedirectKeepVerb ||
Status == HttpStatusCode.RedirectMethod || Status == HttpStatusCode.RedirectMethod ||