Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): update dependency polly to v8 #321

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="Polly" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public partial class GoodreadsScraper : IScraper
[GeneratedRegex(@"([^#]+)#?(.*)")]
private static partial Regex ReSeries();

private readonly AsyncRetryPolicy _retryPolicy;
private readonly ResiliencePipeline _resiliencePipeline;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IBookSeriesMapper _bookSeriesMapper;
private readonly ILogger<GoodreadsScraper> _logger;
Expand All @@ -53,13 +53,18 @@ public GoodreadsScraper(IHttpClientFactory httpClientFactory, IBookSeriesMapper

int maxRetries = 5;

_retryPolicy = Policy.Handle<Exception>().WaitAndRetryAsync(
retryCount: maxRetries,
sleepDurationProvider: (int retryNum) => TimeSpan.FromSeconds(3),
onRetry: (exception, sleepDuration, attemptNumber, context) =>
_resiliencePipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions {
MaxRetryAttempts = maxRetries,
Delay = TimeSpan.FromSeconds(3),
BackoffType = DelayBackoffType.Constant,
OnRetry = (args) =>
{
_logger.LogWarning("Exception: {exceptionMessage}, Retrying in {sleepDuration}. {attemptNumber}/{maxRetries}", exception.Message, sleepDuration, attemptNumber, maxRetries);
});
_logger.LogWarning("Exception: {exceptionMessage}, Retrying in {sleepDuration}. {attemptNumber}/{maxRetries}", args.Outcome.Exception?.Message, args.RetryDelay, args.AttemptNumber, maxRetries);
return ValueTask.CompletedTask;
}
})
.Build();
}

public async Task<BookSearchResult> GetBookDetails(string bookUrl)
Expand All @@ -72,16 +77,16 @@ public async Task<BookSearchResult> GetBookDetails(string bookUrl)
var uri = QueryHelpers.AddQueryString(bookUrl, queryParameters);
var httpClient = _httpClientFactory.CreateClient();

return await _retryPolicy.ExecuteAsync(async () =>
return await _resiliencePipeline.ExecuteAsync(async cancellationToken =>
{
var response = await httpClient.GetAsync(uri);
var response = await httpClient.GetAsync(uri, cancellationToken);

if (!response.IsSuccessStatusCode)
{
throw new Exception($"Error getting search results from Goodreads, status code: {response.StatusCode}, reason: {response.ReasonPhrase}");
}

var responseStream = await response.Content.ReadAsStreamAsync();
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);

HtmlParser parser = new();
var doc = parser.ParseDocument(responseStream);
Expand All @@ -101,7 +106,7 @@ public async Task<BookSearchResult> GetBookDetails(string bookUrl)
}

throw new Exception($"Error getting search results from Goodreads, status code: {response.StatusCode}, reason: {response.ReasonPhrase}");
});
}, new CancellationToken());
}

public async Task<IList<BookSearchResult>> Search(string searchTerm)
Expand All @@ -118,16 +123,16 @@ public async Task<IList<BookSearchResult>> Search(string searchTerm)
uri += $"&search[query]={string.Join("+", termTokens)}";
var httpClient = _httpClientFactory.CreateClient();

return await _retryPolicy.ExecuteAsync(async () =>
return await _resiliencePipeline.ExecuteAsync(async cancellationToken =>
{
var response = await httpClient.GetAsync(uri);
var response = await httpClient.GetAsync(uri, cancellationToken);

if (!response.IsSuccessStatusCode)
{
throw new Exception($"Error getting search results from Goodreads, status code: {response.StatusCode}, reason: {response.ReasonPhrase}");
}

var responseStream = await response.Content.ReadAsStreamAsync();
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);

HtmlParser parser = new();
var doc = parser.ParseDocument(responseStream);
Expand All @@ -145,7 +150,7 @@ public async Task<IList<BookSearchResult>> Search(string searchTerm)
}

throw new Exception("Invalid response from Goodreads");
});
}, new CancellationToken());
}


Expand Down