// async version of ExecUtils.RetryUntilTrue private static async ValueTask RetryUntilTrue(Func <CancellationToken, ValueTask <bool> > action, TimeSpan?timeOut = null, CancellationToken token = default) { var i = 0; var firstAttempt = DateTime.UtcNow; while (timeOut == null || DateTime.UtcNow - firstAttempt < timeOut.Value) { token.ThrowIfCancellationRequested(); i++; if (await action(token).ConfigureAwait(false)) { return; } await Task.Delay(ExecUtils.CalculateFullJitterBackOffDelay(i)).ConfigureAwait(false); } throw new TimeoutException($"Exceeded timeout of {timeOut.Value}"); }
/// <summary> /// Sleep using AWS's recommended Exponential BackOff with Full Jitter from: /// https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ /// </summary> /// <param name="retriesAttempted"></param> internal static void SleepBackOffMultiplier(this int retriesAttempted) => Thread.Sleep(ExecUtils.CalculateFullJitterBackOffDelay(retriesAttempted));
internal static async Task SleepBackOffMultiplierAsync(this int retriesAttempted, CancellationToken token = default) => await Task.Delay(ExecUtils.CalculateFullJitterBackOffDelay(retriesAttempted), token);