Skip to content

Commit

Permalink
SNOW-878067: Refactor retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Nov 3, 2023
1 parent 13fa45f commit 5e9931e
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions Snowflake.Data/Core/HttpUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,23 +459,26 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

requestMessage.RequestUri = updater.Update(errorReason);

if (retryCount > 1)
{
var jitter = GetJitter(backOffInSec);

// Set backoff time
if (isLoginRequest)
{
// Choose between previous sleep time and new base sleep time for login requests
backOffInSec = (int)ChooseRandom(
backOffInSec + jitter,
Math.Pow(s_exponentialFactor, retryCount) + jitter);
}
else if (backOffInSec < MAX_BACKOFF)
{
// Multiply sleep by 2 for non-login requests
backOffInSec *= 2;
}
logger.Debug($"Sleep {backOffInSec} seconds and then retry the request, retryCount: {retryCount}");

await Task.Delay(TimeSpan.FromSeconds(backOffInSec), cancellationToken).ConfigureAwait(false);
totalRetryTime += backOffInSec;

var jitter = GetJitter(backOffInSec);

// Set backoff time
if (isLoginRequest)
{
// Choose between previous sleep time and new base sleep time for login requests
backOffInSec = (int)ChooseRandom(
backOffInSec + jitter,
Math.Pow(s_exponentialFactor, retryCount) + jitter);
}
else if (backOffInSec < MAX_BACKOFF)
{
// Multiply sleep by 2 for non-login requests
backOffInSec *= 2;
}

if ((restTimeout.TotalSeconds > 0) && (totalRetryTime + backOffInSec > restTimeout.TotalSeconds))
Expand All @@ -485,11 +488,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
// then use the remaining connection timeout
backOffInSec = Math.Min(backOffInSec, (int)restTimeout.TotalSeconds - totalRetryTime);
}

logger.Debug($"Sleep {backOffInSec} seconds and then retry the request, retryCount: {retryCount}");

await Task.Delay(TimeSpan.FromSeconds(backOffInSec), cancellationToken).ConfigureAwait(false);
totalRetryTime += backOffInSec;
}
}
}
Expand Down

0 comments on commit 5e9931e

Please sign in to comment.