mirror of
https://github.com/Readarr/Readarr
synced 2026-04-18 02:40:56 +02:00
New: Respect 429 Retry-After responses from BookInfo
This commit is contained in:
parent
70856c217c
commit
f448481460
1 changed files with 75 additions and 14 deletions
|
|
@ -378,14 +378,29 @@ public List<Book> SearchByGoodreadsBookId(int id, bool getAllEditions)
|
|||
|
||||
private Book GetEditionInfo(int id, bool getAllEditions)
|
||||
{
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"book/{id}")
|
||||
.Build();
|
||||
HttpRequest httpRequest;
|
||||
HttpResponse httpResponse;
|
||||
|
||||
httpRequest.SuppressHttpError = true;
|
||||
while (true)
|
||||
{
|
||||
httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"book/{id}")
|
||||
.Build();
|
||||
|
||||
// we expect a redirect
|
||||
var httpResponse = _httpClient.Get(httpRequest);
|
||||
httpRequest.SuppressHttpError = true;
|
||||
|
||||
// we expect a redirect
|
||||
httpResponse = _httpClient.Get(httpRequest);
|
||||
|
||||
if (httpResponse.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
WaitUntilRetry(httpResponse);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
|
|
@ -454,16 +469,32 @@ private Book GetEditionInfo(int id, bool getAllEditions)
|
|||
|
||||
private List<Book> MapSearchResult(List<int> ids)
|
||||
{
|
||||
var httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"book/bulk")
|
||||
.SetHeader("Content-Type", "application/json")
|
||||
.Build();
|
||||
HttpRequest httpRequest;
|
||||
HttpResponse<BulkBookResource> httpResponse;
|
||||
|
||||
httpRequest.SetContent(ids.ToJson());
|
||||
while (true)
|
||||
{
|
||||
httpRequest = _requestBuilder.GetRequestBuilder().Create()
|
||||
.SetSegment("route", $"book/bulk")
|
||||
.SetHeader("Content-Type", "application/json")
|
||||
.Build();
|
||||
|
||||
httpRequest.AllowAutoRedirect = true;
|
||||
httpRequest.SetContent(ids.ToJson());
|
||||
|
||||
var httpResponse = _httpClient.Post<BulkBookResource>(httpRequest);
|
||||
httpRequest.AllowAutoRedirect = true;
|
||||
httpRequest.SuppressHttpError = true;
|
||||
|
||||
httpResponse = _httpClient.Post<BulkBookResource>(httpRequest);
|
||||
|
||||
if (httpResponse.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
WaitUntilRetry(httpResponse);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var mapped = MapBulkBook(httpResponse.Resource);
|
||||
|
||||
|
|
@ -578,7 +609,12 @@ private Author PollAuthorUncached(string foreignAuthorId)
|
|||
|
||||
if (httpResponse.HasHttpError)
|
||||
{
|
||||
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
if (httpResponse.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
WaitUntilRetry(httpResponse);
|
||||
continue;
|
||||
}
|
||||
else if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw new AuthorNotFoundException(foreignAuthorId);
|
||||
}
|
||||
|
|
@ -628,6 +664,12 @@ private Tuple<string, Book, List<AuthorMetadata>> PollBook(string foreignBookId)
|
|||
// this may redirect to an author
|
||||
var httpResponse = _httpClient.Get(httpRequest);
|
||||
|
||||
if (httpResponse.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
WaitUntilRetry(httpResponse);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw new BookNotFoundException(foreignBookId);
|
||||
|
|
@ -697,6 +739,25 @@ private Tuple<string, Book, List<AuthorMetadata>> PollBook(string foreignBookId)
|
|||
return Tuple.Create(authorId, book, metadata);
|
||||
}
|
||||
|
||||
private void WaitUntilRetry(HttpResponse response)
|
||||
{
|
||||
var seconds = 5;
|
||||
|
||||
if (response.Headers.ContainsKey("Retry-After"))
|
||||
{
|
||||
var retryAfter = response.Headers["Retry-After"];
|
||||
|
||||
if (!int.TryParse(retryAfter, out seconds))
|
||||
{
|
||||
seconds = 5;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.Info("BookInfo returned 429, backing off for {0}s", seconds);
|
||||
|
||||
Thread.Sleep(TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
|
||||
private static AuthorMetadata MapAuthorMetadata(AuthorResource resource)
|
||||
{
|
||||
var metadata = new AuthorMetadata
|
||||
|
|
|
|||
Loading…
Reference in a new issue