Skip to content

Commit

Permalink
fix: do not recreate Random each time
Browse files Browse the repository at this point in the history
On .NET Framework it is incorrect to recreate Random instances each time, these should instead be one per instance. On .NET Core and later this is not a limitation, but the libraries share a codebase.

Fixes #944
  • Loading branch information
watfordsuzy committed Apr 10, 2024
1 parent b410dfd commit 826f844
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Box.V2/Utility/ExponentialBackoff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ 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);
const double RETRY_RANDOMIZATION_FACTOR = 0.5;
var minRandomization = 1 - RETRY_RANDOMIZATION_FACTOR;
var maxRandomization = 1 + RETRY_RANDOMIZATION_FACTOR;
var random = new Random();

var randomization = random.NextDouble() * (maxRandomization - minRandomization) + minRandomization;
var randomization = _random.NextDouble() * (maxRandomization - minRandomization) + minRandomization;
var exponential = Math.Pow(2, numRetries - 1);
var result = Math.Ceiling(exponential * baseInterval.TotalSeconds * randomization);
return TimeSpan.FromSeconds(result);
Expand Down

0 comments on commit 826f844

Please sign in to comment.