Skip to content

Commit

Permalink
Make HttpClients resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazzani committed Dec 28, 2024
1 parent fdabdd8 commit 760cf6b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ dotnet_diagnostic.CA1304.severity = error
dotnet_diagnostic.CA1305.severity = error
[*]
vsspell_section_id = 25685bb86f6248d2844f68d46e45a9b5
vsspell_ignored_words_25685bb86f6248d2844f68d46e45a9b5 = cron
vsspell_ignored_words_25685bb86f6248d2844f68d46e45a9b5 = File:.\IgnoredWords.dic

[[*.gitignore](gitignore)]
# VSSPELL: gitignore
Expand Down
14 changes: 14 additions & 0 deletions src/IgnoredWords.dic
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
config
cron
href
img
linkedin
Roadmap
src
tv
versioning
healthcheck
synker
itd
Webhook
yml
2 changes: 1 addition & 1 deletion src/Proxarr.Api/Controllers/QualifierController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IActionResult> Qualified(MediaAdded media, CancellationToken c
return Ok();
}

if(media is MovieAdded movie)
if (media is MovieAdded movie)
{
var result = await _radarrService.Qualify(movie, cancellationToken).ConfigureAwait(false);

Expand Down
24 changes: 22 additions & 2 deletions src/Proxarr.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.Extensions.Http.Resilience;
using Polly;
using Proxarr.Api.Configuration;
using Proxarr.Api.Core;
using Proxarr.Api.HostedServices;
Expand All @@ -6,6 +8,7 @@
using Scalar.AspNetCore;
using Serilog;
using Sonarr.Http.Client;
using System.Net;
using TMDbLib.Client;


Expand Down Expand Up @@ -52,13 +55,30 @@
builder.Services.AddScoped<IRadarrService, RadarrService>();
builder.Services.AddScoped<ISonarrService, SonarrService>();

// Add resilience strategy
HttpStatusCode[] retryStatus = [HttpStatusCode.InternalServerError, HttpStatusCode.ServiceUnavailable, HttpStatusCode.TooManyRequests];

Action<HttpStandardResilienceOptions> resilienceStragtegy = options =>
{
// Customize retry
options.Retry.ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
.Handle<HttpRequestException>()
.HandleResult(response => retryStatus.Any(x => x == response.StatusCode));
options.Retry.MaxRetryAttempts = 3;

// Customize attempt timeout
options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(5);
};

builder.Services
.AddHttpClient<RadarrClient>()
.AddHttpMessageHandler<ApiKeyDelegatingHandler>();
.AddHttpMessageHandler<ApiKeyDelegatingHandler>()
.AddStandardResilienceHandler(resilienceStragtegy);

builder.Services
.AddHttpClient<SonarrClient>()
.AddHttpMessageHandler<ApiKeyDelegatingHandler>();
.AddHttpMessageHandler<ApiKeyDelegatingHandler>()
.AddStandardResilienceHandler(resilienceStragtegy);

builder.Services.AddCronJob<FullScanHostedService>(c =>
{
Expand Down
1 change: 1 addition & 0 deletions src/Proxarr.Api/Proxarr.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0" />
<PackageReference Include="NSwag.ApiDescription.Client" Version="14.2.0">
Expand Down
34 changes: 18 additions & 16 deletions src/Proxarr.Api/Proxarr.Api.http
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@ Content-Type: application/json
Accept: application/json
{
"movie": {
"id": 75,
"title": "Carry-On",
"year": 2024,
"releaseDate": "9999-12-31",
"folderPath": "/Videos/Videos/movies/Carry-On (2024)",
"tmdbId": 1005331,
"imdbId": "tt21382296",
"overview": "An airport security officer races to outsmart a mysterious traveler forcing him to let a dangerous item slip onto a Christmas Eve flight.",
"id": 77,
"title": "Asterix: The Secret of the Magic Potion",
"year": 2018,
"releaseDate": "2019-04-10",
"folderPath": "/Videos/Videos/movies/Asterix - The Secret of the Magic Potion (2018)",
"tmdbId": 527729,
"imdbId": "tt8001346",
"overview": "Following a fall during mistletoe picking, Druid Getafix decides that it is time to secure the future of the village. Accompanied by Asterix and Obelix, he undertakes to travel the Gallic world in search of a talented young druid to transmit the Secret of the Magic Potion.",
"genres": [
"Action",
"Thriller"
"Family",
"Animation",
"Comedy",
"Adventure"
],
"images": [
{
"coverType": "poster",
"url": "/MediaCover/75/poster.jpg",
"remoteUrl": "https://image.tmdb.org/t/p/original/sjMN7DRi4sGiledsmllEw5HJjPy.jpg"
"url": "/MediaCover/77/poster.jpg",
"remoteUrl": "https://image.tmdb.org/t/p/original/5OvHpzWXBzOvgZwOoUsqNSouSwy.jpg"
},
{
"coverType": "fanart",
"url": "/MediaCover/75/fanart.jpg",
"remoteUrl": "https://image.tmdb.org/t/p/original/rhc8Mtuo3Kh8CndnlmTNMF8o9pU.jpg"
"url": "/MediaCover/77/fanart.jpg",
"remoteUrl": "https://image.tmdb.org/t/p/original/9cPMIuB2tv4IP0LbeNhrK5OyDy5.jpg"
}
],
"tags": [
"1 - heni"
],
"originalLanguage": {
"id": 1,
"name": "English"
"id": 2,
"name": "French"
}
},
"addMethod": "manual",
Expand Down

0 comments on commit 760cf6b

Please sign in to comment.