Skip to content

Commit

Permalink
feat(MangaDex): add retry logic for paged requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DevYukine committed Feb 16, 2024
1 parent 3e8ee5d commit 2e5b719
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/MangaMagnet.Api/MangaMagnet.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Polly.Extensions" Version="8.3.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.8.0" />
<PackageReference Include="Quartz.Serialization.Json" Version="3.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
Expand Down
9 changes: 9 additions & 0 deletions backend/MangaMagnet.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using MangaMagnet.Core.Services;
using Microsoft.EntityFrameworkCore;
using Npgsql;
using Polly;
using Polly.Retry;
using Quartz;
using Quartz.Impl.AdoJobStore;
using Serilog;
Expand Down Expand Up @@ -59,6 +61,13 @@
builder.Services.AddScoped<MetadataService>();
builder.Services.AddScoped<DownloadService>();

// Polly
builder.Services.AddResiliencePipeline("MangaDex-Pipeline", pipelineBuilder =>
{
pipelineBuilder
.AddRetry(new RetryStrategyOptions{ MaxRetryAttempts = 3});
});


// Progress services
builder.Services.AddSingleton<ProgressService>();
Expand Down
2 changes: 2 additions & 0 deletions backend/MangaMagnet.Api/Service/MetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public async Task<IEnumerable<MangaMetadataResponse>> UpdateAllMetadataAsync(Can
progressTask.Increment();
}

logger.LogDebug("Updated all metadata");

return updated;
}

Expand Down
3 changes: 2 additions & 1 deletion backend/MangaMagnet.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"Microsoft.EntityFrameworkCore.Database": "Warning",
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning",
"System.Net.Http.HttpClient": "Warning",
"MangaMagnet": "Debug"
"MangaMagnet": "Debug",
"Polly": "Error"
}
}
},
Expand Down
26 changes: 26 additions & 0 deletions backend/MangaMagnet.Api/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
"Npgsql": "8.0.0"
}
},
"Polly.Extensions": {
"type": "Direct",
"requested": "[8.3.0, )",
"resolved": "8.3.0",
"contentHash": "wUjK/vOqq2kaj9WTtBc/tXWykgT3Z5qpvM8ozl6UEBrIsjbatCl6a+AXMZsTwVEyqUKFF9td3aYRmSAaBDtRrQ==",
"dependencies": {
"Microsoft.Extensions.Logging.Abstractions": "8.0.0",
"Microsoft.Extensions.Options": "8.0.0",
"Polly.Core": "8.3.0",
"System.Diagnostics.DiagnosticSource": "8.0.0"
}
},
"Quartz.AspNetCore": {
"type": "Direct",
"requested": "[3.8.0, )",
Expand Down Expand Up @@ -678,6 +690,19 @@
"resolved": "1.0.12",
"contentHash": "f1nye8fMJCYfJeR+Qb7qlcvC8et4Y4TLf2GLfaFoeISgI+yisaJkbNSGfKCpFBNeVF/iPDkyFmr4J28dv342/Q=="
},
"Polly": {
"type": "Transitive",
"resolved": "8.3.0",
"contentHash": "vqAQmLO1gZcS8Ox9Ky+hrq0HxYrYVSBS1vzv6o6RZH3vvECutngIxkUHw9YijbG0QrycxvHo0Hbx0/D7MGgdow==",
"dependencies": {
"Polly.Core": "8.3.0"
}
},
"Polly.Core": {
"type": "Transitive",
"resolved": "8.3.0",
"contentHash": "A3r4xkWasffyrIJGjA3jWQi86ZGcR9BEbpIIoP9xYRAKYwiaGbKpqpsHdWv5sCeLZdaWpVs7pc4EM8Tei+LluA=="
},
"Quartz": {
"type": "Transitive",
"resolved": "3.8.0",
Expand Down Expand Up @@ -1163,6 +1188,7 @@
"ExtendedXmlSerializer": "[3.7.11, )",
"Microsoft.EntityFrameworkCore": "[8.0.1, )",
"Microsoft.Extensions.Http": "[8.0.0, )",
"Polly": "[8.3.0, )",
"System.Threading.RateLimiting": "[8.0.0, )"
}
}
Expand Down
1 change: 1 addition & 0 deletions backend/MangaMagnet.Core/MangaMagnet.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="ExtendedXmlSerializer" Version="3.7.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Polly" Version="8.3.0" />
<PackageReference Include="System.Threading.RateLimiting" Version="8.0.0" />
</ItemGroup>

Expand Down
30 changes: 24 additions & 6 deletions backend/MangaMagnet.Core/Providers/MangaDex/MangaDexApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
using MangaMagnet.Core.Providers.MangaDex.Models.Download;
using MangaMagnet.Core.Util;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Registry;
using Polly.Retry;

namespace MangaMagnet.Core.Providers.MangaDex;

public class MangaDexApiService(IHttpClientFactory httpClientFactory, ILogger<MangaDexApiService> logger)
public class MangaDexApiService(IHttpClientFactory httpClientFactory, ILogger<MangaDexApiService> logger, ResiliencePipelineProvider<string> resiliencePipelineProvider)
: SimpleRatelimitedProvider
{
public async Task<List<MangaDexMangaData>> SearchMangaByNameAsync(string input,
Expand Down Expand Up @@ -139,14 +142,29 @@ private async Task<List<T>> FetchAllPagesAsync<T>(string mangaDexId, Func<string
{
const int limit = 100;

var response = await func(mangaDexId, offset, limit, cancellationToken);
var pipeline = resiliencePipelineProvider.GetPipeline("MangaDex-Pipeline");

var responseDataCount = response.Data.Count;
await pipeline.ExecuteAsync( async cancelToken =>
{
try
{
var response = await func(mangaDexId, offset, limit, cancelToken);

inner.AddRange(response.Data);
var responseDataCount = response.Data.Count;

offset += responseDataCount;
doNextRequest = response.Total > offset;
inner.AddRange(response.Data);

offset += responseDataCount;
doNextRequest = response.Total > offset;
}
catch (Exception e)
{
var page = offset / limit;

logger.LogWarning("Failed to fetch Page {Page}: {Exception}", page, e.Message);
throw;
}
}, cancellationToken);
}

return inner;
Expand Down

0 comments on commit 2e5b719

Please sign in to comment.