diff --git a/Box.V2/Utility/ExponentialBackoff.cs b/Box.V2/Utility/ExponentialBackoff.cs index 30e40f6bb..55f29d569 100644 --- a/Box.V2/Utility/ExponentialBackoff.cs +++ b/Box.V2/Utility/ExponentialBackoff.cs @@ -4,8 +4,6 @@ namespace Box.V2.Utility { public class ExponentialBackoff : IRetryStrategy { - private readonly Random _random = new Random(); - public TimeSpan GetRetryTimeout(int numRetries) { var baseInterval = TimeSpan.FromSeconds(2.0); @@ -13,7 +11,9 @@ public TimeSpan GetRetryTimeout(int numRetries) var minRandomization = 1 - RETRY_RANDOMIZATION_FACTOR; var maxRandomization = 1 + RETRY_RANDOMIZATION_FACTOR; - var randomization = _random.NextDouble() * (maxRandomization - minRandomization) + minRandomization; + var randomization = ThreadSafeRandom.Instance.NextDouble() + * (maxRandomization - minRandomization) + minRandomization; + var exponential = Math.Pow(2, numRetries - 1); var result = Math.Ceiling(exponential * baseInterval.TotalSeconds * randomization); return TimeSpan.FromSeconds(result); diff --git a/Box.V2/Utility/ThreadSafeRandom.cs b/Box.V2/Utility/ThreadSafeRandom.cs new file mode 100644 index 000000000..fc6cc72d4 --- /dev/null +++ b/Box.V2/Utility/ThreadSafeRandom.cs @@ -0,0 +1,37 @@ +using System; +using System.Runtime.CompilerServices; + +namespace Box.V2.Utility +{ + /// + /// A thread safe implementation of , following best practices + /// for .NET Framework, .NET Standard, and .NET 6+. + /// + /// + internal static class ThreadSafeRandom + { +#if NET6_0_OR_GREATER + /// + /// An instance of specific to the calling thread. + /// Do not pass this instance to other threads or contexts. + /// + public static Random Instance => Random.Shared; +#else + // + // Adapted from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Random.cs + // + + [ThreadStatic] + private static Random _random; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Random CreateRandom() => _random = new Random(); + + /// + /// An instance of specific to the calling thread. + /// Do not pass this instance to other threads or contexts. + /// + public static Random Instance => _random ?? CreateRandom(); +#endif + } +}