public void Constructor_InitsDefaults() { ConsulRetryOptions opts = new ConsulRetryOptions(); Assert.False(opts.Enabled); Assert.Equal(ConsulRetryOptions.DEFAULT_MAX_RETRY_ATTEMPTS, opts.MaxAttempts); Assert.Equal(ConsulRetryOptions.DEFAULT_INITIAL_RETRY_INTERVAL, opts.InitialInterval); Assert.Equal(ConsulRetryOptions.DEFAULT_RETRY_MULTIPLIER, opts.Multiplier); Assert.Equal(ConsulRetryOptions.DEFAULT_MAX_RETRY_INTERVAL, opts.MaxInterval); }
private void DoWithRetry(Action retryable, ConsulRetryOptions options) { if (retryable == null) { throw new ArgumentNullException(nameof(retryable)); } _logger?.LogDebug("Starting retryable action .."); var attempts = 0; var backOff = options.InitialInterval; do { try { retryable(); _logger?.LogDebug("Finished retryable action .."); return; } catch (Exception e) { attempts++; if (attempts < options.MaxAttempts) { _logger?.LogError(e, "Exception during {attempt} attempts of retryable action, will retry", attempts); Thread.CurrentThread.Join(backOff); var nextBackoff = (int)(backOff * options.Multiplier); backOff = Math.Min(nextBackoff, options.MaxInterval); } else { _logger?.LogError(e, "Exception during {attempt} attempts of retryable action, done with retry", attempts); throw; } } }while (true); }