public static void Action(Action action, Action<Exception> onRetry, int retryAttempts, BackOff backoff) { Mandate.ParameterNotNull(action, "action"); Mandate.ParameterNotNull(backoff, "backoff"); backoff.Reset(); int retryCount = retryAttempts; do { try { action(); return; } catch (Exception ex) { if (retryCount <= 0) { throw; } onRetry(ex); backoff.Delay(); } } while (retryCount-- > 0); }
public void WorkerAction(object obj) { var backoff = new BackOff(TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(1000)); var cancellationToken = (CancellationToken)obj; while (!cancellationToken.IsCancellationRequested) { bool foundWork = DequeueWork(); SlowDownPollingIfNoWorkAvailable(foundWork, backoff); } }
private static void SlowDownPollingIfNoWorkAvailable(bool foundWork, BackOff backoff) { if (foundWork) { backoff.Reset(); } else { backoff.Delay(); } }
public static void Action(Action action, int retryAttempts, BackOff backOff) { Action(action, (arg1) => { }, retryAttempts, backOff); }
private void WorkerAction(object obj) { var backoff = new BackOff(TimeSpan.FromMilliseconds(10), TimeSpan.FromSeconds(5)); var cancellationToken = (CancellationToken)obj; while (!cancellationToken.IsCancellationRequested) { if (TryProcessNextTimeout()) { backoff.Reset(); } else { backoff.Delay(); } } }