/// <summary> /// Calculate returns a duration of time: 2 ^ attempt * <see cref="IBackoffConfig.BackoffMultiplier"/>. /// </summary> /// <param name="backoffConfig">Read only configuration values related to backoff.</param> /// <param name="attempt">The number of times this message has been attempted (first attempt = 1).</param> /// <returns>The <see cref="TimeSpan"/> to backoff.</returns> public TimeSpan Calculate(IBackoffConfig backoffConfig, int attempt) { var backoffDuration = new TimeSpan(backoffConfig.BackoffMultiplier.Ticks * (long)Math.Pow(2, attempt)); return(backoffDuration); }
/// <summary> /// Calculate returns a random duration of time in the /// range [0, 2^(backoffLevel-1) * <see cref="IBackoffConfig.BackoffMultiplier"/>). /// </summary> /// <param name="backoffConfig">Read only configuration values related to backoff.</param> /// <param name="backoffLevel"> /// The backoff level (>= 1) used to calculate backoff duration. /// <paramref name="backoffLevel"/> increases/decreases with successive failures/successes. /// </param> /// <returns>A <see cref="BackoffCalculation"/> object with the backoff duration and whether to increase /// the backoff level.</returns> public BackoffCalculation Calculate(IBackoffConfig backoffConfig, int backoffLevel) { rngOnce.Do(() => { // lazily initialize the RNG if (rng != null) { return; } rng = new RNGCryptoServiceProvider(); } ); var backoffDuration = new TimeSpan(backoffConfig.BackoffMultiplier.Ticks * (long)Math.Pow(2, backoffLevel - 1)); int maxBackoffMilliseconds = (int)backoffDuration.TotalMilliseconds; int backoffMilliseconds = maxBackoffMilliseconds == 0 ? 0 : rng.Intn(maxBackoffMilliseconds); return(new BackoffCalculation { Duration = TimeSpan.FromMilliseconds(backoffMilliseconds), IncreaseBackoffLevel = backoffDuration < backoffConfig.MaxBackoffDuration }); }
/// <summary> /// Calculate returns a duration of time: 2^(backoffLevel-1) * <see cref="IBackoffConfig.BackoffMultiplier"/>. /// </summary> /// <param name="backoffConfig">Read only configuration values related to backoff.</param> /// <param name="backoffLevel"> /// The backoff level (>= 1) used to calculate backoff duration. /// <paramref name="backoffLevel"/> increases/decreases with successive failures/successes. /// </param> /// <returns>A <see cref="BackoffCalculation"/> object with the backoff duration and whether to increase /// the backoff level.</returns> public BackoffCalculation Calculate(IBackoffConfig backoffConfig, int backoffLevel) { var backoffDuration = new TimeSpan(backoffConfig.BackoffMultiplier.Ticks * (long)Math.Pow(2, backoffLevel - 1)); return(new BackoffCalculation { Duration = backoffDuration, IncreaseBackoffLevel = backoffDuration < backoffConfig.MaxBackoffDuration }); }
/// <summary> /// Calculate returns a random duration of time [0, 2 ^ attempt] * <see cref="IBackoffConfig.BackoffMultiplier"/>. /// </summary> /// <param name="backoffConfig">Read only configuration values related to backoff.</param> /// <param name="attempt">The number of times this message has been attempted (first attempt = 1).</param> /// <returns>The <see cref="TimeSpan"/> to backoff.</returns> public TimeSpan Calculate(IBackoffConfig backoffConfig, int attempt) { rngOnce.Do(() => { // lazily initialize the RNG if (rng != null) return; rng = new RNGCryptoServiceProvider(); } ); var backoffDuration = new TimeSpan(backoffConfig.BackoffMultiplier.Ticks * (long)Math.Pow(2, attempt)); int maxBackoffMilliseconds = (int)backoffDuration.TotalMilliseconds; int backoffMilliseconds = maxBackoffMilliseconds == 0 ? 0 : rng.Intn(maxBackoffMilliseconds); return TimeSpan.FromMilliseconds(backoffMilliseconds); }
/// <summary> /// Calculate returns a duration of time: 2 ^ attempt * <see cref="IBackoffConfig.BackoffMultiplier"/>. /// </summary> /// <param name="backoffConfig">Read only configuration values related to backoff.</param> /// <param name="attempt">The number of times this message has been attempted (first attempt = 1).</param> /// <returns>The <see cref="TimeSpan"/> to backoff.</returns> public TimeSpan Calculate(IBackoffConfig backoffConfig, int attempt) { var backoffDuration = new TimeSpan(backoffConfig.BackoffMultiplier.Ticks * (long)Math.Pow(2, attempt)); return backoffDuration; }