diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index ccd0fe82c..f6916e00c 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -8,6 +8,7 @@ + diff --git a/src/NzbDrone.Core/Download/DownloadClientBase.cs b/src/NzbDrone.Core/Download/DownloadClientBase.cs index 193759191..28ab6d0ba 100644 --- a/src/NzbDrone.Core/Download/DownloadClientBase.cs +++ b/src/NzbDrone.Core/Download/DownloadClientBase.cs @@ -1,15 +1,19 @@ using System; using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; using FluentValidation.Results; using NLog; using NzbDrone.Common.Disk; +using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; using NzbDrone.Core.Indexers; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; +using Polly; +using Polly.Retry; namespace NzbDrone.Core.Download { @@ -21,6 +25,37 @@ namespace NzbDrone.Core.Download protected readonly IRemotePathMappingService _remotePathMappingService; protected readonly Logger _logger; + protected ResiliencePipeline RetryStrategy => new ResiliencePipelineBuilder() + .AddRetry(new RetryStrategyOptions + { + ShouldHandle = static args => args.Outcome switch + { + { Result.HasHttpServerError: true } => PredicateResult.True(), + { Result.StatusCode: HttpStatusCode.RequestTimeout } => PredicateResult.True(), + _ => PredicateResult.False() + }, + Delay = TimeSpan.FromSeconds(3), + MaxRetryAttempts = 2, + BackoffType = DelayBackoffType.Exponential, + UseJitter = true, + OnRetry = args => + { + var exception = args.Outcome.Exception; + + if (exception is not null) + { + _logger.Info(exception, "Request for {0} failed with exception '{1}'. Retrying in {2}s.", Definition.Name, exception.Message, args.RetryDelay.TotalSeconds); + } + else + { + _logger.Info("Request for {0} failed with status {1}. Retrying in {2}s.", Definition.Name, args.Outcome.Result?.StatusCode, args.RetryDelay.TotalSeconds); + } + + return default; + } + }) + .Build(); + public abstract string Name { get; } public Type ConfigContract => typeof(TSettings); @@ -54,10 +89,7 @@ namespace NzbDrone.Core.Download return GetType().Name; } - public abstract DownloadProtocol Protocol - { - get; - } + public abstract DownloadProtocol Protocol { get; } public abstract Task Download(RemoteBook remoteBook, IIndexer indexer); public abstract IEnumerable GetItems(); diff --git a/src/NzbDrone.Core/Download/TorrentClientBase.cs b/src/NzbDrone.Core/Download/TorrentClientBase.cs index a5ffd4b5e..0a4dc0212 100644 --- a/src/NzbDrone.Core/Download/TorrentClientBase.cs +++ b/src/NzbDrone.Core/Download/TorrentClientBase.cs @@ -133,7 +133,9 @@ namespace NzbDrone.Core.Download request.Headers.Accept = "application/x-bittorrent"; request.AllowAutoRedirect = false; - var response = await _httpClient.GetAsync(request); + var response = await RetryStrategy + .ExecuteAsync(static async (state, _) => await state._httpClient.GetAsync(state.request), (_httpClient, request)) + .ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.MovedPermanently || response.StatusCode == HttpStatusCode.Found || diff --git a/src/NzbDrone.Core/Download/UsenetClientBase.cs b/src/NzbDrone.Core/Download/UsenetClientBase.cs index b4bca217a..8ff43b5b0 100644 --- a/src/NzbDrone.Core/Download/UsenetClientBase.cs +++ b/src/NzbDrone.Core/Download/UsenetClientBase.cs @@ -47,7 +47,9 @@ namespace NzbDrone.Core.Download var request = indexer?.GetDownloadRequest(url) ?? new HttpRequest(url); request.RateLimitKey = remoteBook?.Release?.IndexerId.ToString(); - var response = await _httpClient.GetAsync(request); + var response = await RetryStrategy + .ExecuteAsync(static async (state, _) => await state._httpClient.GetAsync(state.request), (_httpClient, request)) + .ConfigureAwait(false); nzbData = response.ResponseData; diff --git a/src/NzbDrone.Core/Readarr.Core.csproj b/src/NzbDrone.Core/Readarr.Core.csproj index 852c2eb24..37a9dae33 100644 --- a/src/NzbDrone.Core/Readarr.Core.csproj +++ b/src/NzbDrone.Core/Readarr.Core.csproj @@ -5,6 +5,7 @@ +