Add configurable number of retries and timeout for http calls (#40)
This commit is contained in:
@@ -193,7 +193,9 @@ services:
|
|||||||
| RADARR__ENABLED | No | Enable or disable Radarr cleanup | false |
|
| RADARR__ENABLED | No | Enable or disable Radarr cleanup | false |
|
||||||
| RADARR__INSTANCES__0__URL | No | First Radarr instance url | http://localhost:8989 |
|
| RADARR__INSTANCES__0__URL | No | First Radarr instance url | http://localhost:8989 |
|
||||||
| RADARR__INSTANCES__0__APIKEY | No | First Radarr instance API key | empty |
|
| RADARR__INSTANCES__0__APIKEY | No | First Radarr instance API key | empty |
|
||||||
|
|||||
|
||||||
|
| HTTP_MAX_RETRIES | No | The number of times to retry a failed HTTP call (to *arrs, download clients etc.) | 0 |
|
||||||
|
| HTTP_TIMEOUT | No | The number of seconds to wait before failing an HTTP call (to *arrs, download clients etc.) | 100 |
|
||||||
#
|
#
|
||||||
### To be noted
|
### To be noted
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace Common.Configuration.General;
|
||||||
|
|
||||||
|
public class HttpConfig : IConfig
|
||||||
|
{
|
||||||
|
[ConfigurationKeyName("HTTP_MAX_RETRIES")]
|
||||||
|
public ushort MaxRetries { get; init; }
|
||||||
|
|
||||||
|
[ConfigurationKeyName("HTTP_TIMEOUT")]
|
||||||
|
public ushort Timeout { get; init; } = 100;
|
||||||
|
|
||||||
|
public void Validate()
|
||||||
|
{
|
||||||
|
if (Timeout is 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("HTTP_TIMEOUT must be greater than 0");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace Common.Configuration;
|
namespace Common.Configuration.General;
|
||||||
|
|
||||||
public sealed class TriggersConfig
|
public sealed class TriggersConfig
|
||||||
{
|
{
|
||||||
@@ -4,4 +4,6 @@ public static class Constants
|
|||||||
{
|
{
|
||||||
public static readonly TimeSpan TriggerMaxLimit = TimeSpan.FromHours(6);
|
public static readonly TimeSpan TriggerMaxLimit = TimeSpan.FromHours(6);
|
||||||
public static readonly TimeSpan CacheLimitBuffer = TimeSpan.FromHours(2);
|
public static readonly TimeSpan CacheLimitBuffer = TimeSpan.FromHours(2);
|
||||||
|
|
||||||
|
public const string HttpClientWithRetryName = "retry";
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,9 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using Common.Configuration;
|
using Common.Configuration.General;
|
||||||
using Common.Configuration.ContentBlocker;
|
using Common.Helpers;
|
||||||
using Executable.Jobs;
|
|
||||||
using Infrastructure.Verticals.Arr;
|
|
||||||
using Infrastructure.Verticals.ContentBlocker;
|
|
||||||
using Infrastructure.Verticals.DownloadClient;
|
|
||||||
using Infrastructure.Verticals.DownloadClient.Deluge;
|
using Infrastructure.Verticals.DownloadClient.Deluge;
|
||||||
using Infrastructure.Verticals.DownloadClient.QBittorrent;
|
using Polly;
|
||||||
using Infrastructure.Verticals.DownloadClient.Transmission;
|
using Polly.Extensions.Http;
|
||||||
using Infrastructure.Verticals.QueueCleaner;
|
|
||||||
|
|
||||||
namespace Executable.DependencyInjection;
|
namespace Executable.DependencyInjection;
|
||||||
|
|
||||||
@@ -17,16 +12,27 @@ public static class MainDI
|
|||||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) =>
|
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) =>
|
||||||
services
|
services
|
||||||
.AddLogging(builder => builder.ClearProviders().AddConsole())
|
.AddLogging(builder => builder.ClearProviders().AddConsole())
|
||||||
.AddHttpClients()
|
.AddHttpClients(configuration)
|
||||||
.AddConfiguration(configuration)
|
.AddConfiguration(configuration)
|
||||||
.AddMemoryCache()
|
.AddMemoryCache()
|
||||||
.AddServices()
|
.AddServices()
|
||||||
.AddQuartzServices(configuration);
|
.AddQuartzServices(configuration);
|
||||||
|
|
||||||
private static IServiceCollection AddHttpClients(this IServiceCollection services)
|
private static IServiceCollection AddHttpClients(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
// add default HttpClient
|
// add default HttpClient
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|
||||||
|
HttpConfig config = configuration.Get<HttpConfig>() ?? new();
|
||||||
|
config.Validate();
|
||||||
|
|
||||||
|
// add retry HttpClient
|
||||||
|
services
|
||||||
|
.AddHttpClient(Constants.HttpClientWithRetryName, x =>
|
||||||
|
{
|
||||||
|
x.Timeout = TimeSpan.FromSeconds(config.Timeout);
|
||||||
|
})
|
||||||
|
.AddRetryPolicyHandler(config);
|
||||||
|
|
||||||
// add Deluge HttpClient
|
// add Deluge HttpClient
|
||||||
services
|
services
|
||||||
@@ -44,8 +50,18 @@ public static class MainDI
|
|||||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
||||||
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
|
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
|
||||||
};
|
};
|
||||||
});
|
})
|
||||||
|
.AddRetryPolicyHandler(config);
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IHttpClientBuilder AddRetryPolicyHandler(this IHttpClientBuilder builder, HttpConfig config) =>
|
||||||
|
builder.AddPolicyHandler(
|
||||||
|
HttpPolicyExtensions
|
||||||
|
.HandleTransientHttpError()
|
||||||
|
// do not retry on Unauthorized
|
||||||
|
.OrResult(response => !response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.Unauthorized)
|
||||||
|
.WaitAndRetryAsync(config.MaxRetries, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using Common.Configuration;
|
using Common.Configuration;
|
||||||
using Common.Configuration.ContentBlocker;
|
using Common.Configuration.ContentBlocker;
|
||||||
|
using Common.Configuration.General;
|
||||||
using Common.Configuration.QueueCleaner;
|
using Common.Configuration.QueueCleaner;
|
||||||
using Common.Helpers;
|
using Common.Helpers;
|
||||||
using Executable.Jobs;
|
using Executable.Jobs;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.0" />
|
||||||
<PackageReference Include="Quartz" Version="3.13.1" />
|
<PackageReference Include="Quartz" Version="3.13.1" />
|
||||||
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.13.1" />
|
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.13.1" />
|
||||||
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.13.1" />
|
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.13.1" />
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
|
"HTTP_MAX_RETRIES": 0,
|
||||||
|
"HTTP_TIMEOUT": 10,
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": "Debug",
|
"LogLevel": "Debug",
|
||||||
"Enhanced": true,
|
"Enhanced": true,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
|
"HTTP_MAX_RETRIES": 0,
|
||||||
|
"HTTP_TIMEOUT": 100,
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": "Information",
|
"LogLevel": "Information",
|
||||||
"Enhanced": true,
|
"Enhanced": true,
|
||||||
|
|||||||
@@ -12,10 +12,10 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FLM.Transmission" Version="1.0.0" />
|
<PackageReference Include="FLM.QBittorrent" Version="1.0.0" />
|
||||||
|
<PackageReference Include="FLM.Transmission" Version="1.0.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
|
||||||
<PackageReference Include="QBittorrent.Client" Version="1.9.24285.1" />
|
|
||||||
<PackageReference Include="Quartz" Version="3.13.1" />
|
<PackageReference Include="Quartz" Version="3.13.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Common.Configuration.Arr;
|
using Common.Configuration.Arr;
|
||||||
using Common.Configuration.Logging;
|
using Common.Configuration.Logging;
|
||||||
using Common.Configuration.QueueCleaner;
|
using Common.Configuration.QueueCleaner;
|
||||||
|
using Common.Helpers;
|
||||||
using Domain.Enums;
|
using Domain.Enums;
|
||||||
using Domain.Models.Arr;
|
using Domain.Models.Arr;
|
||||||
using Domain.Models.Arr.Queue;
|
using Domain.Models.Arr.Queue;
|
||||||
@@ -29,7 +30,7 @@ public abstract class ArrClient
|
|||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_striker = striker;
|
_striker = striker;
|
||||||
_httpClient = httpClientFactory.CreateClient();
|
_httpClient = httpClientFactory.CreateClient(Constants.HttpClientWithRetryName);
|
||||||
_loggingConfig = loggingConfig.Value;
|
_loggingConfig = loggingConfig.Value;
|
||||||
_queueCleanerConfig = queueCleanerConfig.Value;
|
_queueCleanerConfig = queueCleanerConfig.Value;
|
||||||
_striker = striker;
|
_striker = striker;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Common.Configuration.ContentBlocker;
|
using Common.Configuration.ContentBlocker;
|
||||||
|
using Common.Helpers;
|
||||||
using Domain.Enums;
|
using Domain.Enums;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@@ -27,7 +28,7 @@ public sealed class BlocklistProvider
|
|||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_config = config.Value;
|
_config = config.Value;
|
||||||
_httpClient = httpClientFactory.CreateClient();
|
_httpClient = httpClientFactory.CreateClient(Constants.HttpClientWithRetryName);
|
||||||
|
|
||||||
_config.Validate();
|
_config.Validate();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Common.Configuration.DownloadClient;
|
using Common.Configuration.DownloadClient;
|
||||||
using Common.Configuration.QueueCleaner;
|
using Common.Configuration.QueueCleaner;
|
||||||
|
using Common.Helpers;
|
||||||
using Infrastructure.Verticals.ContentBlocker;
|
using Infrastructure.Verticals.ContentBlocker;
|
||||||
using Infrastructure.Verticals.ItemStriker;
|
using Infrastructure.Verticals.ItemStriker;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -15,6 +16,7 @@ public sealed class QBitService : DownloadServiceBase
|
|||||||
|
|
||||||
public QBitService(
|
public QBitService(
|
||||||
ILogger<QBitService> logger,
|
ILogger<QBitService> logger,
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
IOptions<QBitConfig> config,
|
IOptions<QBitConfig> config,
|
||||||
IOptions<QueueCleanerConfig> queueCleanerConfig,
|
IOptions<QueueCleanerConfig> queueCleanerConfig,
|
||||||
FilenameEvaluator filenameEvaluator,
|
FilenameEvaluator filenameEvaluator,
|
||||||
@@ -23,7 +25,7 @@ public sealed class QBitService : DownloadServiceBase
|
|||||||
{
|
{
|
||||||
_config = config.Value;
|
_config = config.Value;
|
||||||
_config.Validate();
|
_config.Validate();
|
||||||
_client = new(_config.Url);
|
_client = new(httpClientFactory.CreateClient(Constants.HttpClientWithRetryName), _config.Url);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task LoginAsync()
|
public override async Task LoginAsync()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Common.Configuration.DownloadClient;
|
using Common.Configuration.DownloadClient;
|
||||||
using Common.Configuration.QueueCleaner;
|
using Common.Configuration.QueueCleaner;
|
||||||
|
using Common.Helpers;
|
||||||
using Infrastructure.Verticals.ContentBlocker;
|
using Infrastructure.Verticals.ContentBlocker;
|
||||||
using Infrastructure.Verticals.ItemStriker;
|
using Infrastructure.Verticals.ItemStriker;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -17,6 +18,7 @@ public sealed class TransmissionService : DownloadServiceBase
|
|||||||
private TorrentInfo[]? _torrentsCache;
|
private TorrentInfo[]? _torrentsCache;
|
||||||
|
|
||||||
public TransmissionService(
|
public TransmissionService(
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
ILogger<TransmissionService> logger,
|
ILogger<TransmissionService> logger,
|
||||||
IOptions<TransmissionConfig> config,
|
IOptions<TransmissionConfig> config,
|
||||||
IOptions<QueueCleanerConfig> queueCleanerConfig,
|
IOptions<QueueCleanerConfig> queueCleanerConfig,
|
||||||
@@ -27,6 +29,7 @@ public sealed class TransmissionService : DownloadServiceBase
|
|||||||
_config = config.Value;
|
_config = config.Value;
|
||||||
_config.Validate();
|
_config.Validate();
|
||||||
_client = new(
|
_client = new(
|
||||||
|
httpClientFactory.CreateClient(Constants.HttpClientWithRetryName),
|
||||||
new Uri(_config.Url, "/transmission/rpc").ToString(),
|
new Uri(_config.Url, "/transmission/rpc").ToString(),
|
||||||
login: _config.Username,
|
login: _config.Username,
|
||||||
password: _config.Password
|
password: _config.Password
|
||||||
|
|||||||
@@ -176,6 +176,9 @@ services:
|
|||||||
- LOGGING__FILE__PATH=/var/logs
|
- LOGGING__FILE__PATH=/var/logs
|
||||||
- LOGGING__ENHANCED=true
|
- LOGGING__ENHANCED=true
|
||||||
|
|
||||||
|
- HTTP_MAX_RETRIES=0
|
||||||
|
- HTTP_TIMEOUT=20
|
||||||
|
|
||||||
- TRIGGERS__QUEUECLEANER=0/30 * * * * ?
|
- TRIGGERS__QUEUECLEANER=0/30 * * * * ?
|
||||||
- TRIGGERS__CONTENTBLOCKER=0/30 * * * * ?
|
- TRIGGERS__CONTENTBLOCKER=0/30 * * * * ?
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user