-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
RetryHelper.cs
90 lines (82 loc) · 3.53 KB
/
RetryHelper.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System
{
public static partial class RetryHelper
{
private static readonly Func<int, int> s_defaultBackoffFunc = i => Math.Min(i * 100, 60_000);
/// <summary>Executes the <paramref name="test"/> action up to a maximum of <paramref name="maxAttempts"/> times.</summary>
/// <param name="maxAttempts">The maximum number of times to invoke <paramref name="test"/>.</param>
/// <param name="test">The test to invoke.</param>
/// <param name="backoffFunc">After a failure, invoked to determine how many milliseconds to wait before the next attempt. It's passed the number of iterations attempted.</param>
public static void Execute(Action test, int maxAttempts = 5, Func<int, int> backoffFunc = null)
{
// Validate arguments
if (maxAttempts < 1)
{
throw new ArgumentOutOfRangeException(nameof(maxAttempts));
}
if (test == null)
{
throw new ArgumentNullException(nameof(test));
}
// Execute the test until it either passes or we run it maxAttempts times
var exceptions = new List<Exception>();
for (int i = 1; i <= maxAttempts; i++)
{
try
{
test();
return;
}
catch (Exception e)
{
exceptions.Add(e);
if (i == maxAttempts)
{
throw new AggregateException(exceptions);
}
}
Thread.Sleep((backoffFunc ?? s_defaultBackoffFunc)(i));
}
}
/// <summary>Executes the <paramref name="test"/> action up to a maximum of <paramref name="maxAttempts"/> times.</summary>
/// <param name="maxAttempts">The maximum number of times to invoke <paramref name="test"/>.</param>
/// <param name="test">The test to invoke.</param>
/// <param name="backoffFunc">After a failure, invoked to determine how many milliseconds to wait before the next attempt. It's passed the number of iterations attempted.</param>
public static async Task ExecuteAsync(Func<Task> test, int maxAttempts = 5, Func<int, int> backoffFunc = null)
{
// Validate arguments
if (maxAttempts < 1)
{
throw new ArgumentOutOfRangeException(nameof(maxAttempts));
}
if (test == null)
{
throw new ArgumentNullException(nameof(test));
}
// Execute the test until it either passes or we run it maxAttempts times
var exceptions = new List<Exception>();
for (int i = 1; i <= maxAttempts; i++)
{
try
{
await test().ConfigureAwait(false);
return;
}
catch (Exception e)
{
exceptions.Add(e);
if (i == maxAttempts)
{
throw new AggregateException(exceptions);
}
}
await Task.Delay((backoffFunc ?? s_defaultBackoffFunc)(i)).ConfigureAwait(false);
}
}
}
}