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:
Joseph Doherty
2026-11-09 12:02:46 -05:00
parent 20138b60cc
commit fad1c11e44
5 changed files with 118 additions and 4 deletions
@@ -315,6 +315,80 @@ public sealed class MxGatewayClientSessionTests
Assert.Equal(2, transport.InvokeCalls.Count);
}
/// <summary>
/// Verifies that a first attempt which runs to its own call deadline and fails with DeadlineExceeded
/// does not exhaust the retry budget (mxaccessgw#197): the retry still runs and succeeds.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task InvokeAsync_RetriesAfterFirstAttemptRunsToItsDeadline()
{
FakeGatewayTransport transport = new(new MxGatewayClientOptions
{
Endpoint = new Uri("http://localhost:5000"),
ApiKey = "test-api-key",
DefaultCallTimeout = TimeSpan.FromMilliseconds(400),
Retry = new MxGatewayClientRetryOptions
{
MaxAttempts = 2,
Delay = TimeSpan.FromMilliseconds(10),
MaxDelay = TimeSpan.FromMilliseconds(20),
},
});
transport.AddInvokeReply(new MxCommandReply
{
SessionId = "session-fixture",
Kind = MxCommandKind.Ping,
ProtocolStatus = new ProtocolStatus { Code = ProtocolStatusCode.Ok },
});
transport.InvokeBehavior = async (attempt, callOptions) =>
{
if (attempt != 1)
{
return;
}
// Simulate a gateway that never answers: block until this attempt's own deadline,
// then fail the way gRPC does.
TimeSpan remaining = callOptions.Deadline!.Value - DateTime.UtcNow;
if (remaining > TimeSpan.Zero)
{
await Task.Delay(remaining + TimeSpan.FromMilliseconds(20));
}
throw new RpcException(new Status(StatusCode.DeadlineExceeded, "deadline"));
};
await using MxGatewayClient client = CreateClient(transport);
MxGatewaySession session = await client.OpenSessionAsync();
await session.InvokeAsync(new MxCommandRequest
{
SessionId = session.SessionId,
Command = new MxCommand { Kind = MxCommandKind.Ping, Ping = new PingCommand() },
});
Assert.Equal(2, transport.InvokeCalls.Count);
}
/// <summary>Verifies the overall retry budget spans every attempt plus back-off.</summary>
[Fact]
public void ComputeOverallBudget_CoversAllAttemptsAndBackoff()
{
MxGatewayClientRetryOptions retry = new()
{
MaxAttempts = 3,
Delay = TimeSpan.FromSeconds(1),
MaxDelay = TimeSpan.FromSeconds(2),
};
Assert.Equal(
TimeSpan.FromSeconds((3 * 30) + (2 * 2)),
MxGatewayClientRetryPolicy.ComputeOverallBudget(retry, TimeSpan.FromSeconds(30)));
Assert.Equal(
TimeSpan.FromSeconds(30),
MxGatewayClientRetryPolicy.ComputeOverallBudget(new MxGatewayClientRetryOptions { MaxAttempts = 1 }, TimeSpan.FromSeconds(30)));
}
/// <summary>Verifies that open session does not retry on transient RPC failure.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]