diff --git a/src/NzbDrone.Core/Download/DownloadClientBase.cs b/src/NzbDrone.Core/Download/DownloadClientBase.cs index b9c5a1de060..1360213f8a0 100644 --- a/src/NzbDrone.Core/Download/DownloadClientBase.cs +++ b/src/NzbDrone.Core/Download/DownloadClientBase.cs @@ -1,9 +1,11 @@ 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.Organizer; @@ -11,6 +13,8 @@ using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; +using Polly; +using Polly.Retry; namespace NzbDrone.Core.Download { @@ -23,6 +27,37 @@ public abstract class DownloadClientBase : IDownloadClient 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); @@ -58,10 +93,7 @@ public override string ToString() return GetType().Name; } - public abstract DownloadProtocol Protocol - { - get; - } + public abstract DownloadProtocol Protocol { get; } public abstract Task Download(RemoteMovie remoteMovie, IIndexer indexer); public abstract IEnumerable GetItems(); diff --git a/src/NzbDrone.Core/Download/TorrentClientBase.cs b/src/NzbDrone.Core/Download/TorrentClientBase.cs index 07dccd118a3..2ac9194bf5d 100644 --- a/src/NzbDrone.Core/Download/TorrentClientBase.cs +++ b/src/NzbDrone.Core/Download/TorrentClientBase.cs @@ -134,7 +134,9 @@ private async Task DownloadFromWebUrl(RemoteMovie remoteMovie, IIndexer 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 25743f2ed0c..36849135b20 100644 --- a/src/NzbDrone.Core/Download/UsenetClientBase.cs +++ b/src/NzbDrone.Core/Download/UsenetClientBase.cs @@ -48,7 +48,9 @@ public override async Task Download(RemoteMovie remoteMovie, IIndexer in var request = indexer?.GetDownloadRequest(url) ?? new HttpRequest(url); request.RateLimitKey = remoteMovie?.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/Radarr.Core.csproj b/src/NzbDrone.Core/Radarr.Core.csproj index ec6f237cc75..48d9f54b2e3 100644 --- a/src/NzbDrone.Core/Radarr.Core.csproj +++ b/src/NzbDrone.Core/Radarr.Core.csproj @@ -8,6 +8,7 @@ +