From 5e9931e8b12f165fb1e49b71dc2519a109e801d0 Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Fri, 3 Nov 2023 11:15:15 -0700 Subject: [PATCH] SNOW-878067: Refactor retry logic --- Snowflake.Data/Core/HttpUtil.cs | 40 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/Snowflake.Data/Core/HttpUtil.cs b/Snowflake.Data/Core/HttpUtil.cs index ef5d98db0..870dcb25e 100755 --- a/Snowflake.Data/Core/HttpUtil.cs +++ b/Snowflake.Data/Core/HttpUtil.cs @@ -459,23 +459,26 @@ protected override async Task 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)) @@ -485,11 +488,6 @@ protected override async Task 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; } } }