feat: add flush-before-close for graceful client shutdown
This commit is contained in:
@@ -563,6 +563,31 @@ public sealed class NatsClient : IDisposable
|
||||
_logger.LogDebug("Client {ClientId} connection closed: {CloseReason}", Id, reason);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes pending data (unless skip-flush is set) and closes the connection.
|
||||
/// </summary>
|
||||
public async Task FlushAndCloseAsync(bool minimalFlush = false)
|
||||
{
|
||||
if (!ShouldSkipFlush)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var flushCts = new CancellationTokenSource(minimalFlush
|
||||
? TimeSpan.FromMilliseconds(100)
|
||||
: TimeSpan.FromSeconds(1));
|
||||
await _stream.FlushAsync(flushCts.Token);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Best effort flush — don't let it prevent close
|
||||
}
|
||||
}
|
||||
|
||||
try { _socket.Shutdown(SocketShutdown.Both); }
|
||||
catch (SocketException) { }
|
||||
catch (ObjectDisposedException) { }
|
||||
}
|
||||
|
||||
public void RemoveAllSubscriptions(SubList subList)
|
||||
{
|
||||
foreach (var sub in _subs.Values)
|
||||
|
||||
Reference in New Issue
Block a user