# `PhoenixWebTransport.MemoryBudget`
[🔗](https://github.com/knevan/phoenix-wtransport/blob/main/lib/phoenix_wtransport/memory_budget.ex#L1)

Process-wide cap on outbound parked bytes shared by every WebTransport session.

Per-session caps (`max_parked_bytes`, default 64 MiB) bound one connection, but
nothing stopped N stalled sessions from multiplying that bound by N: 100 wedged
sessions could pin 6.4 GB of reference-counted binaries and OOM the node. This
budget is the shared ceiling for the whole VM.

Reservations use a single `:atomics` slot so the per-frame send path stays
lock-free (no GenServer round-trip in the hot loop). A reservation is taken when
a frame is enqueued and released as its bytes leave the queue, so the counter
always reflects live parked bytes.

`nil` is accepted everywhere as "no shared budget configured", which keeps a
standalone listener (tests, one-off scripts) working unchanged.

# `t`

```elixir
@type t() :: {reference(), pos_integer()}
```

# `from_limit`

```elixir
@spec from_limit(term()) :: t() | nil
```

Builds a budget from a configured limit (`0` or `:infinity` disables).

# `limit`

```elixir
@spec limit(t() | nil) :: pos_integer() | :infinity
```

Returns the configured ceiling in bytes, or `:infinity`.

# `new`

```elixir
@spec new(pos_integer()) :: t()
```

Creates a budget ceiling in bytes.

# `release`

```elixir
@spec release(t() | nil, non_neg_integer()) :: :ok
```

Releases `bytes` previously reserved against `budget`.

# `reserve`

```elixir
@spec reserve(t() | nil, non_neg_integer()) :: :ok | :deny
```

Reserves `bytes` against `budget`, returning `:ok` or `:deny`.

# `used`

```elixir
@spec used(t() | nil) :: non_neg_integer()
```

Returns the reserved bytes, or `0` when no budget is configured.

---

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