Skip to content

Commit

Permalink
SNOW-1739483 improve calculation of time to wait before retry (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dstempniak authored Oct 28, 2024
1 parent fe8e848 commit b21bfb3
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions Snowflake.Data/Core/HttpUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
else if (childCts != null && childCts.Token.IsCancellationRequested)
{
logger.Warn($"Http request timeout. Retry the request after {backOffInSec} sec.");
logger.Warn($"Http request timeout. Retry the request after max {backOffInSec} sec.");
}
else
{
Expand Down Expand Up @@ -465,7 +465,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
logger.Info("Response returned was null.");
}

if (restTimeout.TotalSeconds > 0 && totalRetryTime > restTimeout.TotalSeconds)
if (restTimeout.TotalSeconds > 0 && totalRetryTime >= restTimeout.TotalSeconds)
{
logger.Debug($"stop retry as connection_timeout {restTimeout.TotalSeconds} sec. reached");
if (response != null)
Expand All @@ -478,6 +478,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
throw new OperationCanceledException(errorMessage);
}

if (restTimeout.TotalSeconds > 0 && totalRetryTime + backOffInSec > restTimeout.TotalSeconds)
{
// No need to wait more than necessary if it can be avoided.
backOffInSec = (int)restTimeout.TotalSeconds - totalRetryTime;
}

retryCount++;
if ((maxRetryCount > 0) && (retryCount > maxRetryCount))
{
Expand Down Expand Up @@ -516,15 +522,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
// Multiply sleep by 2 for non-login requests
backOffInSec *= 2;
}

totalRetryTime = (int)((DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - startTimeInMilliseconds) / 1000);
if ((restTimeout.TotalSeconds > 0) && (totalRetryTime + backOffInSec > restTimeout.TotalSeconds))
{
// No need to wait more than necessary if it can be avoided.
// If the rest timeout will be reached before the next back-off,
// then use the remaining connection timeout.
backOffInSec = Math.Min(backOffInSec, (int)restTimeout.TotalSeconds - totalRetryTime + 1);
}
}
}
}
Expand Down

0 comments on commit b21bfb3

Please sign in to comment.