fix(client-dotnet): size the retry outer budget for all attempts (mxaccessgw#197)
ExecuteSafeUnaryAsync capped the whole retry pipeline at one DefaultCallTimeout while each attempt carried the same deadline, so a DeadlineExceeded first attempt consumed the whole budget and no retry ever ran. The outer cap is now MaxAttempts x DefaultCallTimeout + (MaxAttempts-1) x MaxDelay in both MxGatewayClient and GalaxyRepositoryClient. Adds a slow-first-attempt test (fails without the fix) and a budget unit test.
This commit is contained in:
@@ -518,7 +518,10 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using CancellationTokenSource timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeout.CancelAfter(Options.DefaultCallTimeout);
|
||||
// Outer budget covers every attempt (each carries its own DefaultCallTimeout deadline)
|
||||
// plus back-off; capping it at one DefaultCallTimeout starved retries of a
|
||||
// DeadlineExceeded first attempt (mxaccessgw#197).
|
||||
timeout.CancelAfter(MxGatewayClientRetryPolicy.ComputeOverallBudget(Options.Retry, Options.DefaultCallTimeout));
|
||||
|
||||
return await _safeUnaryRetryPipeline.ExecuteAsync(
|
||||
async token => await call(token).ConfigureAwait(false),
|
||||
|
||||
@@ -376,7 +376,10 @@ public sealed class MxGatewayClient : IAsyncDisposable
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using CancellationTokenSource timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeout.CancelAfter(Options.DefaultCallTimeout);
|
||||
// Outer budget covers every attempt (each carries its own DefaultCallTimeout deadline)
|
||||
// plus back-off; capping it at one DefaultCallTimeout starved retries of a
|
||||
// DeadlineExceeded first attempt (mxaccessgw#197).
|
||||
timeout.CancelAfter(MxGatewayClientRetryPolicy.ComputeOverallBudget(Options.Retry, Options.DefaultCallTimeout));
|
||||
|
||||
return await _safeUnaryRetryPipeline.ExecuteAsync(
|
||||
async token => await call(token).ConfigureAwait(false),
|
||||
|
||||
@@ -41,6 +41,29 @@ internal static class MxGatewayClientRetryPolicy
|
||||
.Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the overall time budget for a retried unary call: every attempt gets its own
|
||||
/// <paramref name="callTimeout"/> deadline, plus the worst-case back-off between attempts.
|
||||
/// An outer cap equal to a single <paramref name="callTimeout"/> would let a
|
||||
/// <c>DeadlineExceeded</c> first attempt consume the whole budget so no retry could ever run.
|
||||
/// </summary>
|
||||
/// <param name="options">Retry configuration.</param>
|
||||
/// <param name="callTimeout">The per-attempt call timeout.</param>
|
||||
/// <returns>The overall budget, or <see cref="Timeout.InfiniteTimeSpan"/> if it exceeds the timer range.</returns>
|
||||
public static TimeSpan ComputeOverallBudget(MxGatewayClientRetryOptions options, TimeSpan callTimeout)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
int attempts = Math.Max(1, options.MaxAttempts);
|
||||
double totalMs = (callTimeout.TotalMilliseconds * attempts)
|
||||
+ (options.MaxDelay.TotalMilliseconds * (attempts - 1));
|
||||
|
||||
// CancellationTokenSource.CancelAfter accepts at most int.MaxValue milliseconds.
|
||||
return totalMs >= int.MaxValue
|
||||
? Timeout.InfiniteTimeSpan
|
||||
: TimeSpan.FromMilliseconds(totalMs);
|
||||
}
|
||||
|
||||
/// <summary>Returns whether a command kind is eligible for automatic retry on transient failures.</summary>
|
||||
/// <param name="kind">The command kind to check.</param>
|
||||
/// <returns><see langword="true"/> if the command kind may be automatically retried; otherwise, <see langword="false"/>.</returns>
|
||||
|
||||
Reference in New Issue
Block a user