| | | 1 | | namespace ArturRios.Util.FlowControl; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides a simple retry mechanism for executing actions or functions with optional fixed delay between attempts. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// Configure using <see cref="MaxAttempts"/> and <see cref="DelayMilliseconds"/> then call <see cref="Execute(Action)"/ |
| | | 8 | | /// Exceptions are rethrown after the maximum number of attempts has been exhausted. |
| | | 9 | | /// </remarks> |
| | | 10 | | public class Retry |
| | | 11 | | { |
| | | 12 | | private int _delayMilliseconds; |
| | | 13 | | private int _maxAttempts; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a new <see cref="Retry"/> instance. Syntactic sugar for <c>new Retry()</c>. |
| | | 17 | | /// </summary> |
| | 2 | 18 | | public static Retry New => new(); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Sets the maximum number of retry attempts before giving up and rethrowing the last exception. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="maxAttempts">Number of attempts; must be greater than zero.</param> |
| | | 24 | | /// <returns>The configured <see cref="Retry"/> instance.</returns> |
| | | 25 | | public Retry MaxAttempts(int maxAttempts) |
| | 2 | 26 | | { |
| | 2 | 27 | | _maxAttempts = maxAttempts; |
| | | 28 | | |
| | 2 | 29 | | return this; |
| | 2 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Sets a fixed delay (in milliseconds) to wait after a failed attempt before retrying. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="delayMilliseconds">Delay duration in milliseconds.</param> |
| | | 36 | | /// <returns>The configured <see cref="Retry"/> instance.</returns> |
| | | 37 | | public Retry DelayMilliseconds(int delayMilliseconds) |
| | 2 | 38 | | { |
| | 2 | 39 | | _delayMilliseconds = delayMilliseconds; |
| | | 40 | | |
| | 2 | 41 | | return this; |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Executes an <see cref="Action"/> applying the configured retry strategy. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="action">The action to execute.</param> |
| | | 48 | | /// <exception cref="Exception">Rethrows the last exception encountered after all retries fail.</exception> |
| | | 49 | | public void Execute(Action action) |
| | 1 | 50 | | { |
| | 3 | 51 | | while (true) |
| | 3 | 52 | | { |
| | | 53 | | try |
| | 3 | 54 | | { |
| | 3 | 55 | | action(); |
| | 1 | 56 | | break; |
| | | 57 | | } |
| | 2 | 58 | | catch |
| | 2 | 59 | | { |
| | 2 | 60 | | if (_maxAttempts-- <= 0) |
| | 0 | 61 | | { |
| | 0 | 62 | | throw; |
| | | 63 | | } |
| | | 64 | | |
| | 2 | 65 | | if (_delayMilliseconds > 0) |
| | 2 | 66 | | { |
| | 2 | 67 | | Thread.Sleep(_delayMilliseconds); |
| | 2 | 68 | | } |
| | 2 | 69 | | } |
| | 2 | 70 | | } |
| | 1 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Executes a function applying the configured retry strategy and returns its result. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <typeparam name="T">Return type of the function.</typeparam> |
| | | 77 | | /// <param name="func">The function to invoke.</param> |
| | | 78 | | /// <returns>The value returned by <paramref name="func"/>.</returns> |
| | | 79 | | /// <exception cref="Exception">Rethrows the last exception encountered after all retries fail.</exception> |
| | | 80 | | public T Execute<T>(Func<T> func) |
| | 1 | 81 | | { |
| | 3 | 82 | | while (true) |
| | 3 | 83 | | { |
| | | 84 | | try |
| | 3 | 85 | | { |
| | 3 | 86 | | return func(); |
| | | 87 | | } |
| | 2 | 88 | | catch |
| | 2 | 89 | | { |
| | 2 | 90 | | if (_maxAttempts-- <= 0) |
| | 0 | 91 | | { |
| | 0 | 92 | | throw; |
| | | 93 | | } |
| | | 94 | | |
| | 2 | 95 | | if (_delayMilliseconds > 0) |
| | 2 | 96 | | { |
| | 2 | 97 | | Thread.Sleep(_delayMilliseconds); |
| | 2 | 98 | | } |
| | 2 | 99 | | } |
| | 2 | 100 | | } |
| | 1 | 101 | | } |
| | | 102 | | } |