# `Snodo.Cancellation`
[🔗](https://github.com/joshrotenberg/snodo/blob/v0.2.0/lib/snodo/cancellation.ex#L1)

A lightweight cooperative cancellation token backed by `:atomics`.

# `t`

```elixir
@opaque t()
```

# `cancel`

```elixir
@spec cancel(t()) :: :ok
```

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

# `cancelled?`

```elixir
@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`

```elixir
@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`

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

Recognizes a cancellation token at an untyped context boundary.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
