Table of Contents

Method DrainAsync

Namespace
NATS.Client.Core
Assembly
NATS.Client.Core.dll

DrainAsync(CancellationToken)

Drains the subscription: sends UNSUB so the server stops delivering new messages, waits for a PING/PONG round-trip so messages already in flight are received, then completes the message channel. Buffered messages stay available to read and the connection remains usable.

public virtual ValueTask DrainAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Bounds the best-effort PING/PONG fence wait, which lasts at most DrainPingTimeout; the token can only shorten it, not extend it past that timeout. Cancellation does not abort the drain.

Returns

ValueTask

A ValueTask that represents the asynchronous drain operation.

Remarks

Unlike UnsubscribeAsync(), which completes the channel right away and can drop messages still in the socket buffer, drain fences inbound delivery with a PING/PONG so buffered messages are preserved. The fence is best-effort and bounded by cancellationToken and DrainPingTimeout.

Cancellation only shortens the PING/PONG fence wait; it cannot abort the drain. Once DrainAsync(CancellationToken) is entered the subscription is always committed to the drained state, and cancelling the token does not throw or keep the subscription alive.