/// <summary> /// If <see cref="JitterMaxDuration"/> is greater than <see cref="TimeSpan.Zero"/>, this method returns a randomized duration (in ms) between 0 and <see cref="JitterMaxDuration"/> that will be added to the entry's specified <see cref="Duration"/> . /// This is done to avoid a variation of the so called <a href="https://en.wikipedia.org/wiki/Thundering_herd_problem">thundering herd problem</a> that may happen when the entry for the same key expires on multiple nodes at the same time, because of high synchronization. /// </summary> /// <returns>An additional cache duration (in ms) to slightly vary the entry duration</returns> public double GetJitterDurationMs() { if (JitterMaxDuration <= TimeSpan.Zero) { return(0d); } return(ConcurrentRandom.NextDouble() * JitterMaxDuration.TotalMilliseconds); }
/// <summary> /// Randomize an actual delay with a value between <paramref name="minDelay"/> and <paramref name="maxDelay"/>. /// </summary> /// <param name="minDelay">The minimun amount of delay.</param> /// <param name="maxDelay">The maximum amount of delay.</param> /// <returns>The randomized delay.</returns> public static TimeSpan RandomizeDelay(TimeSpan minDelay, TimeSpan maxDelay) { if (minDelay <= TimeSpan.Zero && maxDelay <= TimeSpan.Zero) { return(TimeSpan.Zero); } if (minDelay >= maxDelay) { return(minDelay); } return(minDelay + TimeSpan.FromMilliseconds(ConcurrentRandom.NextDouble() * (maxDelay - minDelay).TotalMilliseconds)); }
/// <summary> /// Determines if an exception should be thrown. /// </summary> /// <param name="throwProbability">The probabilty that an exception will be thrown.</param> /// <returns>True if an exception should be thrown, false otherwise.</returns> public static bool ShouldCreateChaos(float throwProbability) { if (throwProbability <= 0f) { return(false); } if (throwProbability >= 1f) { return(true); } return(ConcurrentRandom.NextDouble() < throwProbability); }