Skip to content

Commit

Permalink
Limit ratelimit attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
lyarenei committed Nov 10, 2023
1 parent 226ca59 commit 980eb73
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class BaseClient : HttpClient
private readonly object _lock = new();
private readonly ILogger _logger;
private readonly ISleepService _sleepService;
private const int RateLimitAttempts = 50;

Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs

View workflow job for this annotation

GitHub Actions / build

Constant fields should appear before non-constant fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md)

Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs

View workflow job for this annotation

GitHub Actions / build

Constant fields should appear before non-constant fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md)

Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs

View workflow job for this annotation

GitHub Actions / call / test

Constant fields should appear before non-constant fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md)

Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs

View workflow job for this annotation

GitHub Actions / call / test

Constant fields should appear before non-constant fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md)

Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs

View workflow job for this annotation

GitHub Actions / call / test

Constant fields should appear before non-constant fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md)

Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs

View workflow job for this annotation

GitHub Actions / call / test

Constant fields should appear before non-constant fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md)

/// <summary>
/// Initializes a new instance of the <see cref="BaseClient"/> class.
Expand Down Expand Up @@ -100,11 +101,12 @@ protected BaseClient(IHttpClientFactory httpClientFactory, ILogger logger, ISlee
private async Task<TResponse?> DoRequest<TResponse>(HttpRequestMessage requestMessage, CancellationToken cancellationToken)
where TResponse : IListenBrainzResponse
{
HttpResponseMessage response;
// Compiler complains that the variable won't be initialized in the loop, so here we go
HttpResponseMessage? response = null;
try
{
Monitor.Enter(_lock);
while (true)
for (int i = 0; i < RateLimitAttempts; i++)
{
response = await SendRequest(requestMessage, cancellationToken);
if (response.StatusCode == HttpStatusCode.TooManyRequests)
Expand All @@ -123,6 +125,8 @@ protected BaseClient(IHttpClientFactory httpClientFactory, ILogger logger, ISlee
Monitor.Exit(_lock);
}

if (response is null) throw new InvalidResponseException("Response is NULL");

var stringContent = await response.Content.ReadAsStringAsync(cancellationToken);
var result = JsonConvert.DeserializeObject<TResponse>(stringContent, SerializerSettings);
if (result is null) throw new InvalidResponseException("Response deserialized to NULL");
Expand Down

0 comments on commit 980eb73

Please sign in to comment.