-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetryPolicy.cs
More file actions
64 lines (58 loc) · 2.36 KB
/
RetryPolicy.cs
File metadata and controls
64 lines (58 loc) · 2.36 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
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
namespace LastFM.ReaderCore
{
public class RetryPolicy
{
private readonly int _maxRetries;
private readonly TimeSpan _initialDelay;
private readonly double _backoffMultiplier;
private readonly IErrorLogger _errorLogger;
public RetryPolicy(int maxRetries = 3, TimeSpan? initialDelay = null, double backoffMultiplier = 2.0, IErrorLogger errorLogger = null)
{
_maxRetries = maxRetries;
_initialDelay = initialDelay ?? TimeSpan.FromSeconds(1);
_backoffMultiplier = backoffMultiplier;
_errorLogger = errorLogger;
}
public async Task<T> ExecuteWithRetryAsync<T>(Func<Task<T>> action, string operationName)
{
var currentRetry = 0;
var currentDelay = _initialDelay;
while (true)
{
try
{
return await action();
}
catch (Exception ex) when (ShouldRetry(ex))
{
currentRetry++;
if (currentRetry > _maxRetries)
{
_errorLogger?.LogError(ex, $"Max retries ({_maxRetries}) exceeded for operation: {operationName}");
throw;
}
_errorLogger?.LogError(ex, $"Retry {currentRetry} of {_maxRetries} for operation: {operationName}. Waiting {currentDelay.TotalSeconds} seconds...");
await Task.Delay(currentDelay);
currentDelay = TimeSpan.FromTicks((long)(currentDelay.Ticks * _backoffMultiplier));
}
}
}
private bool ShouldRetry(Exception ex)
{
return ex switch
{
HttpRequestException => true,
StorageException storageEx => storageEx.RequestInformation?.HttpStatusCode == 429 || // Too Many Requests
storageEx.RequestInformation?.HttpStatusCode == 503 || // Service Unavailable
storageEx.RequestInformation?.HttpStatusCode == 500, // Internal Server Error
_ => false
};
}
}
}