Snodo.Cancellation (snodo v0.1.0)

Copy Markdown View Source

A lightweight cooperative cancellation token backed by :atomics.

Summary

Functions

Marks the token cancelled. Cancelling an already cancelled token is a no-op.

Returns whether the token has been cancelled.

Returns a token that is not cancelled.

Recognizes a cancellation token at an untyped context boundary.

Types

t()

@opaque t()

Functions

cancel(cancellation, reason \\ nil)

@spec cancel(t(), term()) :: :ok

Marks the token cancelled. Cancelling an already cancelled token is a no-op.

The token does not record reason; it is accepted and ignored.

cancelled?(cancellation)

@spec cancelled?(t()) :: boolean()

Returns whether the token has been cancelled.

A handler finds its request's token in the cancellation field of Snodo.Context when a transport runs the request through Snodo.Server.Executor. The field is nil for direct dispatch, so check it with normalize/1 first.

new()

@spec new() :: t()

Returns a token that is not cancelled.

Copies of the token, including copies sent to other processes on the same node, share one state, so a cancellation through any copy is seen by all.

normalize(token)

@spec normalize(term()) :: {:ok, t()} | :error

Recognizes a cancellation token at an untyped context boundary.