| | | 1 | | using Syki.Back.Domain.Enums; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Commands; |
| | | 4 | | |
| | | 5 | | public static class CommandBackoffStrategies |
| | | 6 | | { |
| | | 7 | | public static int? GetDelaySeconds(BackoffStrategy strategy, int baseDelaySeconds, int retryAttempt) |
| | | 8 | | { |
| | 0 | 9 | | return strategy switch |
| | 0 | 10 | | { |
| | 0 | 11 | | BackoffStrategy.None => null, |
| | 0 | 12 | | BackoffStrategy.Exponential => Exponential(baseDelaySeconds, retryAttempt), |
| | 0 | 13 | | BackoffStrategy.Linear => Linear(baseDelaySeconds, retryAttempt), |
| | 0 | 14 | | BackoffStrategy.Fixed => Fixed(baseDelaySeconds), |
| | 0 | 15 | | _ => null, |
| | 0 | 16 | | }; |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// base*1, base*2, base*4, base*8... (baseDelay * 2^(retryAttempt-1)) |
| | | 21 | | /// </summary> |
| | | 22 | | private static int Exponential(int baseDelaySeconds, int retryAttempt) |
| | | 23 | | { |
| | 0 | 24 | | return baseDelaySeconds * (int)Math.Pow(2, retryAttempt - 1); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// base*1, base*2, base*3, base*4... (baseDelay * retryAttempt) |
| | | 29 | | /// </summary> |
| | | 30 | | private static int Linear(int baseDelaySeconds, int retryAttempt) |
| | | 31 | | { |
| | 0 | 32 | | return baseDelaySeconds * retryAttempt; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// base, base, base... |
| | | 37 | | /// </summary> |
| | | 38 | | private static int Fixed(int baseDelaySeconds) |
| | | 39 | | { |
| | 0 | 40 | | return baseDelaySeconds; |
| | | 41 | | } |
| | | 42 | | } |