Skip to content

Commit 58c4f61

Browse files
Updated code changes
1 parent ad7da75 commit 58c4f61

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Diff for: src/LCT.APICommunications/RetryHttpClientHandler.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public RetryHttpClientHandler()
2828
onRetry: (outcome, timespan, attempt, context) =>
2929
{
3030
Logger.Debug($"Retry attempt {attempt} due to: {(outcome.Exception != null ? outcome.Exception.Message : $"{outcome.Result.StatusCode}")}");
31+
Logger.Warn($"Retry attempt {attempt} will be triggered in {timespan.TotalSeconds} seconds due to: {(outcome.Exception != null ? outcome.Exception.Message : $"{outcome.Result.StatusCode}")}");
3132
});
3233
}
3334
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
@@ -45,20 +46,18 @@ public static async Task ExecuteWithRetryAsync(Func<Task> action)
4546
GetRetryInterval,
4647
onRetry: (exception, timespan, attempt, context) =>
4748
{
48-
Logger.Debug($"Retry attempt {attempt} due to: {exception?.Message ?? "No exception"}");
49+
Logger.Debug($"Retry attempt {attempt} due to: {exception?.Message ?? "No exception"}");
4950
});
5051

5152
await retryPolicy.ExecuteAsync(action);
5253
}
5354
private static TimeSpan GetRetryInterval(int attempt)
5455
{
55-
return attempt switch
56-
{
57-
1 => TimeSpan.FromSeconds(ApiConstant.APIRetryIntervalFirst), // 1st retry after 5 seconds
58-
2 => TimeSpan.FromSeconds(ApiConstant.APIRetryIntervalSecond), // 2nd retry after 10 seconds
59-
3 => TimeSpan.FromSeconds(ApiConstant.APIRetryIntervalThird), // 3rd retry after 30 seconds
60-
_ => TimeSpan.Zero // Default (not used)
61-
};
56+
// Define retry intervals as constants or values
57+
var retryIntervals = new[] { ApiConstant.APIRetryIntervalFirst, ApiConstant.APIRetryIntervalSecond, ApiConstant.APIRetryIntervalThird }; // Retry intervals for 1st, 2nd, and 3rd attempts
58+
if (attempt >= 1 && attempt <= retryIntervals.Length)
59+
return TimeSpan.FromSeconds(retryIntervals[attempt - 1]);
60+
return TimeSpan.Zero; // Default if out of range
6261
}
6362
}
6463
}

0 commit comments

Comments
 (0)