feat(events): distinguish ResourceExhausted causes with an mxgw-reason trailer (mxaccessgw#216)

Event-queue overflow, alarm-feed overflow, subscriber cap, session limit and
oversized commands all surfaced as ResourceExhausted. The status code is
unchanged; the gateway now attaches a machine-readable `mxgw-reason` trailer
(constants in Contracts ResourceExhaustedReasons). The .NET client maps
ResourceExhausted to MxGatewayResourceExhaustedException (an
MxGatewayException subclass; additive) exposing Reason/IsOverflow/
IsSubscriberLimit. Documented in docs/ClientBehaviorFixtures.md; the other
clients get a doc note only.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:02:24 -05:00
parent 716c845fc7
commit fccc21ba8e
9 changed files with 253 additions and 5 deletions
@@ -261,7 +261,9 @@ public sealed class MxAccessGatewayService(
// client-side slowness condition with a client-side remedy (reconnect and
// re-snapshot), so it maps to ResourceExhausted like a StreamEvents overflow rather
// than reaching MapException's warning-with-stack + Unavailable fallback.
throw new RpcException(new Status(StatusCode.ResourceExhausted, overflow.Message));
throw new RpcException(
new Status(StatusCode.ResourceExhausted, overflow.Message),
ReasonTrailers(ResourceExhaustedReasons.AlarmFeedOverflow));
}
catch (Exception exception) when (exception is not RpcException)
{
@@ -1113,9 +1115,24 @@ public sealed class MxAccessGatewayService(
_ => StatusCode.Unavailable,
};
return new RpcException(new Status(statusCode, exception.Message));
string? reason = exception.ErrorCode switch
{
SessionManagerErrorCode.EventSubscriberAlreadyActive => ResourceExhaustedReasons.SubscriberAlreadyActive,
SessionManagerErrorCode.EventSubscriberLimitReached => ResourceExhaustedReasons.SubscriberLimit,
SessionManagerErrorCode.EventQueueOverflow => ResourceExhaustedReasons.EventQueueOverflow,
SessionManagerErrorCode.SessionLimitExceeded => ResourceExhaustedReasons.SessionLimit,
_ => null,
};
return reason is null
? new RpcException(new Status(statusCode, exception.Message))
: new RpcException(new Status(statusCode, exception.Message), ReasonTrailers(reason));
}
/// <summary>Builds the response trailers carrying a <see cref="ResourceExhaustedReasons"/> code.</summary>
private static Metadata ReasonTrailers(string reason) =>
[new Metadata.Entry(ResourceExhaustedReasons.TrailerKey, reason)];
private static RpcException MapWorkerClientException(WorkerClientException exception)
{
StatusCode statusCode = exception.ErrorCode switch
@@ -1129,6 +1146,8 @@ public sealed class MxAccessGatewayService(
_ => StatusCode.Unavailable,
};
return new RpcException(new Status(statusCode, exception.Message));
return exception.ErrorCode == WorkerClientErrorCode.CommandTooLarge
? new RpcException(new Status(statusCode, exception.Message), ReasonTrailers(ResourceExhaustedReasons.CommandTooLarge))
: new RpcException(new Status(statusCode, exception.Message));
}
}