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

Retry 429s within requester #726

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
32 changes: 26 additions & 6 deletions RiotSharp/Http/RateLimitedRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ public class RateLimitedRequester : RequesterBase, IRateLimitedRequester
public readonly IDictionary<TimeSpan, int> RateLimits;

private readonly bool _throwOnDelay;
private readonly int _retryCount;
private readonly ConcurrentDictionary<Region, RateLimiter> _rateLimiters = new ConcurrentDictionary<Region, RateLimiter>();

/// <inheritdoc />
public RateLimitedRequester(string apiKey, IDictionary<TimeSpan, int> rateLimits, bool throwOnDelay = false) : base(apiKey)
public RateLimitedRequester(string apiKey, IDictionary<TimeSpan, int> rateLimits, bool throwOnDelay = false, int retryCount = 3) : base(apiKey)
{
RateLimits = rateLimits;
_throwOnDelay = throwOnDelay;
_retryCount = retryCount;
}

#region Public Methods

/// <inheritdoc />
public Task<string> CreateGetRequestAsync(string relativeUrl, Region region, List<string> queryParameters = null,
public Task<string> CreateGetRequestAsync(string relativeUrl, Region region, List<string> queryParameters = null,
bool useHttps = true)
{
var host = GetPlatformHost(region);
Expand Down Expand Up @@ -66,7 +68,7 @@ public async Task<bool> CreatePutRequestAsync(string relativeUrl, Region region,
var response = await SendAsync(request).ConfigureAwait(false);
response.Dispose();
return true;

}
catch (RiotSharpException)
{
Expand All @@ -93,12 +95,30 @@ private RateLimiter GetRateLimiter(Region region)
/// <param name="region">The region which's requests should be rate limited</param>
private async Task<string> GetRateLimitedResponseContentAsync(HttpRequestMessage request, Region region)
{
await GetRateLimiter(region).HandleRateLimitAsync().ConfigureAwait(false);
var numRetries = 0;
Exception lastException = null;

using (var response = await SendAsync(request).ConfigureAwait(false))
while (numRetries < _retryCount)
{
return await GetResponseContentAsync(response).ConfigureAwait(false);
await GetRateLimiter(region).HandleRateLimitAsync().ConfigureAwait(false);
try
{
using (var response = await SendAsync(request).ConfigureAwait(false))
{
return await GetResponseContentAsync(response).ConfigureAwait(false);
}
}
catch (RiotSharpRateLimitException e)
{
lastException = e;
GetRateLimiter(region).SetRetryAfter(e.RetryAfter);
request = request.Clone();
numRetries++;
continue;
}
}

throw lastException;
}
}
}
18 changes: 18 additions & 0 deletions RiotSharp/Misc/HttpRequestMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Net.Http;

namespace RiotSharp.Misc
{
public static class HttpRequestMessageExtensions
{

public static HttpRequestMessage Clone(this HttpRequestMessage req)
{
var newReq = new HttpRequestMessage(req.Method, req.RequestUri);
foreach (var header in req.Headers)
{
newReq.Headers.Add(header.Key, header.Value);
}
return newReq;
}
}
}