diff --git a/Src/Support/Google.Apis.Core/Util/ExponentialBackOff.cs b/Src/Support/Google.Apis.Core/Util/ExponentialBackOff.cs index 5f82448b3b2..49853127c80 100644 --- a/Src/Support/Google.Apis.Core/Util/ExponentialBackOff.cs +++ b/Src/Support/Google.Apis.Core/Util/ExponentialBackOff.cs @@ -29,7 +29,8 @@ public class ExponentialBackOff : IBackOff private const int MaxAllowedNumRetries = 20; /// The random instance which generates a random number to add the to next back-off. - private static readonly Random Random = new Random(); + private static readonly Random s_random = new Random(); + private static readonly object s_lock = new object(); /// /// Time span used to bound the back-off jitter. @@ -137,8 +138,13 @@ private int GetPercentBoundedJitter(double rawBackOffMs) => 0 : GetRandomBoundedValue((int)(DeltaBackOffPercent * rawBackOffMs) / 100); - private static int GetRandomBoundedValue(int upperBound) => - Random.Next(-1 * upperBound, upperBound + 1); + private static int GetRandomBoundedValue(int upperBound) + { + lock (s_lock) + { + return s_random.Next(-1 * upperBound, upperBound + 1); + } + } #endregion }