Skip to content

Commit

Permalink
Use ConcurrentRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck committed Aug 18, 2021
1 parent 0e0eeea commit c099d07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/Auth0.Core/ConcurrentRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace Auth0.Core
{
internal sealed class ConcurrentRandom
{
private static readonly Random SRandom = new Random();

/// <summary>
/// Returns a random integer that is within a specified range.
/// </summary>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned. <paramref name="maxValue" /> must be greater than or equal to <paramref name="minValue" />.</param>
/// <returns>A 32-bit signed integer greater than or equal to <paramref name="minValue" /> and less than <paramref name="maxValue" />; that is, the range of return values includes <paramref name="minValue" /> but not <paramref name="maxValue" />. If <paramref name="minValue" /> equals <paramref name="maxValue" />, <paramref name="minValue" /> is returned.</returns>
public int Next(int minValue, int maxValue)
{
lock (SRandom)
{
return SRandom.Next(minValue, maxValue);
}
}
}
}
18 changes: 9 additions & 9 deletions src/Auth0.ManagementApi/HttpClientManagementConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Auth0.Core;

namespace Auth0.ManagementApi
{
Expand All @@ -20,9 +21,16 @@ public class HttpClientManagementConnection : IManagementConnection, IDisposable

readonly HttpClient httpClient;
readonly HttpClientManagementConnectionOptions options;
readonly Random random = new Random();
bool ownHttpClient;

readonly ConcurrentRandom random = new ConcurrentRandom();
readonly int MAX_REQUEST_RETRY_JITTER = 250;
readonly int MAX_REQUEST_RETRY_DELAY = 500;
readonly int MIN_REQUEST_RETRY_DELAY = 100;
readonly int DEFAULT_NUMBER_RETRIES = 3;
readonly int MAX_NUMBER_RETRIES = 10;
readonly int BASE_DELAY = 100;

/// <summary>
/// Initializes a new instance of the <see cref="HttpClientManagementConnection"/> class.
/// </summary>
Expand Down Expand Up @@ -184,11 +192,7 @@ private static string SerializeFormBodyValue(object value)
private async Task<TResult> Retry<TResult>(Func<Task<TResult>> retryable)
{
int? configuredNrOfTries = options.NumberOfHttpRetries;
var DEFAULT_NUMBER_RETRIES = 3;
var MAX_NUMBER_RETRIES = 10;
var BASE_DELAY = 100;
var nrOfTries = 0;

var nrOfTriesToAttempt = Math.Min(MAX_NUMBER_RETRIES, configuredNrOfTries ?? DEFAULT_NUMBER_RETRIES);

while (true)
Expand All @@ -214,10 +218,6 @@ private async Task<TResult> Retry<TResult>(Func<Task<TResult>> retryable)
// ✔ Randomizes jitter, adding up to MAX_REQUEST_RETRY_JITTER (250ms)
// ✔ Never less than MIN_REQUEST_RETRY_DELAY (100ms)
// ✔ Never more than MAX_REQUEST_RETRY_DELAY (500ms)
//
var MAX_REQUEST_RETRY_JITTER = 250;
var MAX_REQUEST_RETRY_DELAY = 500;
var MIN_REQUEST_RETRY_DELAY = 100;

var wait = Convert.ToInt32(BASE_DELAY * Math.Pow(2, nrOfTries - 1));
wait = random.Next(wait + 1, wait + MAX_REQUEST_RETRY_JITTER);
Expand Down

0 comments on commit c099d07

Please sign in to comment.