forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleRetryHandler.cs
More file actions
108 lines (94 loc) · 3.89 KB
/
SimpleRetryHandler.cs
File metadata and controls
108 lines (94 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
namespace SpotifyAPI.Web
{
public class SimpleRetryHandler : IRetryHandler
{
private readonly Func<TimeSpan, CancellationToken, Task> _sleep;
/// <summary>
/// Specifies after how many milliseconds should a failed request be retried.
/// </summary>
public TimeSpan RetryAfter { get; set; }
/// <summary>
/// Maximum number of tries for one failed request.
/// </summary>
public int RetryTimes { get; set; }
/// <summary>
/// Whether a failure of type "Too Many Requests" should use up one of the allocated retry attempts.
/// </summary>
public bool TooManyRequestsConsumesARetry { get; set; }
/// <summary>
/// Error codes that will trigger auto-retry
/// </summary>
public IEnumerable<HttpStatusCode> RetryErrorCodes { get; set; }
/// <summary>
/// A simple retry handler which retries a request based on status codes with a fixed sleep interval.
/// It also supports Retry-After headers sent by spotify. The execution will be delayed by the amount in
/// the Retry-After header
/// </summary>
/// <returns></returns>
public SimpleRetryHandler() : this(sleepWithCancel: Task.Delay) { }
public SimpleRetryHandler(Func<TimeSpan, Task> sleep) : this((t, _) => sleep(t)) { }
public SimpleRetryHandler(Func<TimeSpan, CancellationToken, Task> sleepWithCancel)
{
_sleep = sleepWithCancel;
RetryAfter = TimeSpan.FromMilliseconds(50);
RetryTimes = 10;
TooManyRequestsConsumesARetry = false;
RetryErrorCodes = new[] {
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable
};
}
private static TimeSpan? ParseTooManyRetries(IResponse response)
{
if (response.StatusCode != (HttpStatusCode)429)
{
return null;
}
if (
(response.Headers.ContainsKey("Retry-After") && int.TryParse(response.Headers["Retry-After"], out int secondsToWait))
|| (response.Headers.ContainsKey("retry-after") && int.TryParse(response.Headers["retry-after"], out secondsToWait)))
{
return TimeSpan.FromSeconds(secondsToWait);
}
throw new APIException("429 received, but unable to parse Retry-After Header. This should not happen!");
}
public Task<IResponse> HandleRetry(IRequest request, IResponse response, IRetryHandler.RetryFunc retry, CancellationToken cancel = default)
{
Ensure.ArgumentNotNull(response, nameof(response));
Ensure.ArgumentNotNull(retry, nameof(retry));
return HandleRetryInternally(request, response, retry, RetryTimes, cancel);
}
private async Task<IResponse> HandleRetryInternally(
IRequest request,
IResponse response,
IRetryHandler.RetryFunc retry,
int triesLeft,
CancellationToken cancel)
{
cancel.ThrowIfCancellationRequested();
var secondsToWait = ParseTooManyRetries(response);
if (secondsToWait != null && (!TooManyRequestsConsumesARetry || triesLeft > 0))
{
await _sleep(secondsToWait.Value, cancel).ConfigureAwait(false);
response = await retry(request, cancel).ConfigureAwait(false);
var newTriesLeft = TooManyRequestsConsumesARetry ? triesLeft - 1 : triesLeft;
return await HandleRetryInternally(request, response, retry, newTriesLeft, cancel).ConfigureAwait(false);
}
while (RetryErrorCodes.Contains(response.StatusCode) && triesLeft > 0)
{
await _sleep(RetryAfter, cancel).ConfigureAwait(false);
response = await retry(request, cancel).ConfigureAwait(false);
return await HandleRetryInternally(request, response, retry, triesLeft - 1, cancel).ConfigureAwait(false);
}
return response;
}
}
}