CancellationSupport

Transport-level cancellation primitive.

Transports that can propagate cancellation across the wire implement this interface. Core uses awaitRequestCancellable to convert the caller coroutine's cancellation into a sendCancel to the remote side, and server-side dispatchers use registerHandler / cancelHandler to tie incoming cancel signals to the currently running handler Job.

Implementations should treat sendCancel as best-effort — the transport may already be closed by the time the caller is cancelled, and callers rely on CancellationException propagating regardless.

Functions

Link copied to clipboard
suspend fun <T> CancellationSupport.awaitRequestCancellable(callId: RpcCallId, response: Deferred<T>): T

Await response and, if the awaiting coroutine is cancelled before completion, invoke CancellationSupport.sendCancel for callId before re-throwing.

Link copied to clipboard
abstract fun cancelHandler(callId: RpcCallId, cause: CancellationException? = null)

Cancel the handler previously registered under callId, if any.

Link copied to clipboard
abstract fun registerHandler(callId: RpcCallId, job: Job)

Register a server-side handler Job under callId so that a later cancel signal from the remote side can cancel the correct handler.

Link copied to clipboard
abstract suspend fun sendCancel(callId: RpcCallId)

Emit a cancellation signal for the given in-flight request to the remote endpoint.

Link copied to clipboard
abstract fun unregisterHandler(callId: RpcCallId)

Remove a previously registered handler. Safe to call for ids that were never registered.

Link copied to clipboard
inline suspend fun <T> CancellationSupport.withHandlerRegistered(callId: RpcCallId, block: () -> T): T

Utility to bind the currently executing coroutine's Job to callId on a CancellationSupport so that an incoming remote cancel signal cancels this coroutine.