private Retry CreateNewRetry(RetryHistory history) { int expoBackoff = System.Convert.ToInt32(Math.Pow(2, history.Retries.Count)) * history.Configuration.BaseDelayMs; int jitteredBackoff = history.Configuration.Jitter(history.Retries, expoBackoff, history.Random); return(new Retry(expoBackoff, jitteredBackoff)); }
public async Task InvokeWithRetry(Func <Task> request, RetryHistory history) { try { await request(); } catch (Exception e) { if (history.Configuration != null && _del(e)) { await Backoff(history, e); await InvokeWithRetry(request, history); } else { throw e; } } }
public async Task <T> InvokeWithRetry <T>(Func <Task <T> > request, RetryHistory history) { try { return(await request()); } catch (Exception e) { if (history.Configuration != null && _del(e)) { await Backoff(history, e); return(await InvokeWithRetry <T>(request, history)); } else { throw e; } } }
private Task Backoff(RetryHistory history, Exception e) { if (history.Retries.Count >= history.Configuration.MaxAttempts) { throw new TaskCanceledException("Exceeded max retry attempts.", e); } Retry newRetry = CreateNewRetry(history); history.Retries.Add(newRetry); history.Configuration.RetryListener?.Invoke(history.Retries.Count, newRetry); if (history.UserCancelToken.HasValue) { return(Task.Delay(newRetry.JitterBackoff, history.UserCancelToken.Value)); } else { return(Task.Delay(newRetry.JitterBackoff)); } }