public void WaitTimes() { TimeSpan[] waitTimes = new TimeSpan[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(8), TimeSpan.FromSeconds(16), TimeSpan.FromSeconds(32), TimeSpan.FromSeconds(64), TimeSpan.FromSeconds(128), TimeSpan.FromSeconds(256), }; ExponentialBackoffRetryStrategy strategy = new ExponentialBackoffRetryStrategy(9, TimeSpan.FromSeconds(1)); Assert.That(strategy.RetryCount, Is.EqualTo(9)); for (int i = 0; i < strategy.RetryCount; i++) { Assert.That(strategy.ShouldRetry(i + 1), Is.True, "Attempt " + (i + 1)); Assert.That(strategy.GetWaitTime(i + 1), Is.EqualTo(waitTimes[i]), "Attempt " + (i + 1)); } Assert.That(strategy.ShouldRetry(11), Is.False, "Attempt 11"); Assert.Throws(Is.TypeOf<ArgumentOutOfRangeException>().And.Property("ParamName").EqualTo("attempt"), () => strategy.GetWaitTime(11)); Assert.That(strategy.ShouldRetry(0), Is.False, "Attempt 0"); Assert.Throws(Is.TypeOf<ArgumentOutOfRangeException>().And.Property("ParamName").EqualTo("attempt"), () => strategy.GetWaitTime(0)); Assert.That(strategy.ShouldRetry(-1), Is.False, "Attempt -2"); Assert.Throws(Is.TypeOf<ArgumentOutOfRangeException>().And.Property("ParamName").EqualTo("attempt"), () => strategy.GetWaitTime(-1)); }
public void WaitTimes() { TimeSpan[] waitTimes = new TimeSpan[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(8), TimeSpan.FromSeconds(16), TimeSpan.FromSeconds(32), TimeSpan.FromSeconds(64), TimeSpan.FromSeconds(128), TimeSpan.FromSeconds(256), }; ExponentialBackoffRetryStrategy strategy = new ExponentialBackoffRetryStrategy(9, TimeSpan.FromSeconds(1)); Assert.That(strategy.RetryCount, Is.EqualTo(9)); for (int i = 0; i < strategy.RetryCount; i++) { Assert.That(strategy.ShouldRetry(i + 1), Is.True, "Attempt " + (i + 1)); Assert.That(strategy.GetWaitTime(i + 1), Is.EqualTo(waitTimes[i]), "Attempt " + (i + 1)); } Assert.That(strategy.ShouldRetry(11), Is.False, "Attempt 11"); Assert.Throws(Is.TypeOf <ArgumentOutOfRangeException>().And.Property("ParamName").EqualTo("attempt"), () => strategy.GetWaitTime(11)); Assert.That(strategy.ShouldRetry(0), Is.False, "Attempt 0"); Assert.Throws(Is.TypeOf <ArgumentOutOfRangeException>().And.Property("ParamName").EqualTo("attempt"), () => strategy.GetWaitTime(0)); Assert.That(strategy.ShouldRetry(-1), Is.False, "Attempt -2"); Assert.Throws(Is.TypeOf <ArgumentOutOfRangeException>().And.Property("ParamName").EqualTo("attempt"), () => strategy.GetWaitTime(-1)); }
/// <summary> /// Initializes a new instance of the <see cref="RetryDelegatingHandler"/> class. /// Sets default retry policty base on Exponential Backoff. /// </summary> public RetryDelegatingHandler() { var retryStrategy = new ExponentialBackoffRetryStrategy( DefaultNumberOfAttempts, DefaultMinBackoff, DefaultMaxBackoff, DefaultBackoffDelta); RetryPolicy = new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(retryStrategy); }
private void Init() { var retryStrategy = new ExponentialBackoffRetryStrategy( DefaultNumberOfAttempts, DefaultMinBackoff, DefaultMaxBackoff, DefaultBackoffDelta); RetryPolicy = new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(retryStrategy); }
public void ShouldAttempt() { ExponentialBackoffRetryStrategy strategy = new ExponentialBackoffRetryStrategy(4, TimeSpan.FromSeconds(1)); Assert.That(strategy.RetryCount, Is.EqualTo(4)); Assert.That(strategy.ShouldRetry(-1), Is.False, "Attempt -1"); Assert.That(strategy.ShouldRetry(0), Is.False, "Attempt 0"); Assert.That(strategy.ShouldRetry(1), Is.True, "Attempt 1"); Assert.That(strategy.ShouldRetry(2), Is.True, "Attempt 2"); Assert.That(strategy.ShouldRetry(3), Is.True, "Attempt 3"); Assert.That(strategy.ShouldRetry(4), Is.True, "Attempt 4"); Assert.That(strategy.ShouldRetry(5), Is.False, "Attempt 5"); Assert.That(strategy.ShouldRetry(6), Is.False, "Attempt 6"); }
/// <summary> /// Provides a <see cref="ReliableSqlConnection" /> instance to use for connections. /// </summary> /// <returns>A reliable connection</returns> protected override ReliableSqlConnection CreateReliableConnection() { const string incremental = "Incremental Retry Strategy"; const string backoff = "Backoff Retry Strategy"; var connectionRetry = new ExponentialBackoffRetryStrategy(backoff, 10, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(10), false); var commandRetry = new IncrementalRetryStrategy(incremental, 10, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1)); var connection = new ReliableSqlConnection(null, new RetryPolicy <TTransientErrorDetectionStrategy>(connectionRetry), new RetryPolicy <TTransientErrorDetectionStrategy>(commandRetry) ); connection.ConnectionRetryPolicy.Retrying += ConnectionRetryEventHandler(); connection.CommandRetryPolicy.Retrying += CommandRetryEventHandler(); return(connection); }
public MessagePump( AzureStorageQueueClient queueClient, Func <IQueueMessage, CancellationToken, Task> callback, MessageHandlerOptions messageHandlerOptions, CancellationToken cancellationToken) { _queueClient = queueClient ?? throw new ArgumentNullException(nameof(queueClient)); _messageHandlerOptions = messageHandlerOptions ?? throw new ArgumentNullException(nameof(messageHandlerOptions)); _onMessageCallback = callback ?? throw new ArgumentNullException(nameof(callback)); _cancellationToken = cancellationToken; _semaphore = new SemaphoreSlim( messageHandlerOptions.MaxConcurrentCalls, messageHandlerOptions.MaxConcurrentCalls); _retryStrategy = new ExponentialBackoffRetryStrategy( delay: TimeSpan.FromSeconds(10), maximumDelay: TimeSpan.FromSeconds(30)); }
public void ExponentialBackoffDoesNotUnderflow() { var exponentialBackoff = new ExponentialBackoffRetryStrategy( MAX_RETRY_ATTEMPTS, RetryStrategy.DefaultMinBackoff, RetryStrategy.DefaultMaxBackoff, RetryStrategy.DefaultClientBackoff); ShouldRetry shouldRetry = exponentialBackoff.GetShouldRetry(); for (int i = 1; i < MAX_RETRY_ATTEMPTS; i++) { shouldRetry(i, new Exception(), out TimeSpan delay); if (delay.TotalSeconds <= 0) { Assert.Fail("Exponential backoff should never recommend a negative delay"); } } }
/// <summary> /// Set retry count of retry policy using ExponentialBackoffRetryStrategy from environement variable AZURE_PS_HTTP_MAX_RETRIES /// </summary> /// <returns>Whether succeed to set retry count or not</returns> public static bool TrySetRetryCountofRetryPolicy <TClient>(this Microsoft.Rest.ServiceClient <TClient> serviceClient) where TClient : Microsoft.Rest.ServiceClient <TClient> { int?maxretries = ServiceClientExtension.HttpRetryTimes.AzurePsHttpMaxRetries; if (maxretries != null && maxretries >= 0) { TimeSpan defaultBackoffDelta = new TimeSpan(0, 0, 10); TimeSpan defaultMaxBackoff = new TimeSpan(0, 0, 10); TimeSpan defaultMinBackoff = new TimeSpan(0, 0, 1); var retryStrategy = new ExponentialBackoffRetryStrategy( (int)maxretries, defaultBackoffDelta, defaultMaxBackoff, defaultMinBackoff); var retryPolicy = new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(retryStrategy); serviceClient.SetRetryPolicy(retryPolicy); return(true); } return(false); }
private IScheduler Create(string name) { var properties = _config.Settings; properties["quartz.scheduler.instanceId"] = "AUTO"; if (name != null) { properties["quartz.scheduler.instanceName"] = name; } var stdSchedulerFactory = new StdSchedulerFactory(properties); stdSchedulerFactory.Initialize(); var scheduler = stdSchedulerFactory.GetScheduler(); scheduler.JobFactory = _jobFactory; scheduler.ListenerManager.AddSchedulerListener(_loggingSchedulerListener); scheduler.ListenerManager.AddJobListener(_loggingJobListener); IRetryStrategy sut = new ExponentialBackoffRetryStrategy(_retrySettings); IJobListener retryListener = new RetryJobListener(sut); scheduler.ListenerManager.AddJobListener(retryListener, GroupMatcher <JobKey> .AnyGroup()); try { scheduler.Start(); } catch (SchedulerException) { lock (_locker) { _current = null; return(GetScheduler(name)); } } return(scheduler); }
/// <summary> /// Creates an instance of ExponentialBackoff. /// </summary> /// <param name="retryCount">The maximum number of retry attempts.</param> /// <param name="minBackoff">The minimum back-off time</param> /// <param name="maxBackoff">The maximum back-off time.</param> /// <param name="deltaBackoff">The value that will be used to calculate a random delta in the exponential delay between retries.</param> public ExponentialBackoff(int retryCount, TimeSpan minBackoff, TimeSpan maxBackoff, TimeSpan deltaBackoff) { _exponentialBackoffRetryStrategy = new ExponentialBackoffRetryStrategy(retryCount, minBackoff, maxBackoff, deltaBackoff); }
public void SetUp() { strategy = new ExponentialBackoffRetryStrategy(5, 1.Seconds(), 10.Seconds(), jitter: 0.0); }