feat(client): add write timeout recovery with per-kind policies

Add WriteTimeoutPolicy enum, FlushResult record struct, and
GetWriteTimeoutPolicy static method as nested types in NatsClient.
Models Go's client.go per-kind timeout handling: CLIENT kind closes
on timeout, ROUTER/GATEWAY/LEAF use TCP-level flush recovery.
This commit is contained in:
Joseph Doherty
2026-02-25 02:37:48 -05:00
parent 494d327282
commit 7468401bd0
2 changed files with 102 additions and 0 deletions

View File

@@ -967,6 +967,47 @@ public sealed class NatsClient : INatsClient, IDisposable
_socket.Dispose();
}
/// <summary>
/// Policy for handling write timeouts based on client kind.
/// Go reference: server/client.go — CLIENT connections close on timeout,
/// ROUTER/GATEWAY/LEAF connections attempt TCP-level flush recovery.
/// </summary>
public enum WriteTimeoutPolicy
{
/// <summary>Close the connection on write timeout (used for CLIENT kind).</summary>
Close,
/// <summary>Attempt TCP-level flush and continue (used for ROUTER, GATEWAY, LEAF).</summary>
TcpFlush,
}
/// <summary>
/// Returns the write timeout policy for the given client kind.
/// Go reference: server/client.go — routes/gateways/leafnodes get TcpFlush,
/// regular clients get Close.
/// </summary>
public static WriteTimeoutPolicy GetWriteTimeoutPolicy(ClientKind kind) => kind switch
{
ClientKind.Client => WriteTimeoutPolicy.Close,
ClientKind.Router => WriteTimeoutPolicy.TcpFlush,
ClientKind.Gateway => WriteTimeoutPolicy.TcpFlush,
ClientKind.Leaf => WriteTimeoutPolicy.TcpFlush,
_ => WriteTimeoutPolicy.Close,
};
/// <summary>
/// Result of a flush operation, tracking partial write progress.
/// Go reference: server/client.go — partial write handling for routes and gateways.
/// </summary>
public readonly record struct FlushResult(long BytesAttempted, long BytesWritten)
{
/// <summary>Whether the flush was only partially completed.</summary>
public bool IsPartial => BytesWritten < BytesAttempted;
/// <summary>Number of bytes remaining to be written.</summary>
public long BytesRemaining => BytesAttempted - BytesWritten;
}
/// <summary>
/// Blocks producers when the client's outbound buffer is near capacity.
/// Go reference: server/client.go (stc channel, stalledRoute handling).