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

Per-session outbound lane scheduler providing:

  * O(1) `:queue` buffering per lane slot with incremental byte accounting
  * Configurable global (`max_parked_bytes`) and per-lane (`max_lane_bytes`)
    memory caps with frame-safe `:reset_lane | :drop_oldest | :close_session`
    overflow policies
  * Strict priority-ordered / shortest-remaining-first drain (`0` control
    first, then `{urgency, pending_bytes, slot}` ascending)
  * 3-tier slot isolation (`:control`, `:small`, `:bulk`) using payload-size
    EMA with sticky slot pinning and idle-safe reclamation so small
    interactive components never collide into heavy component streams

# `overflow_action`

```elixir
@type overflow_action() ::
  :close_session
  | {:reset_lane, non_neg_integer()}
  | :dropped_new
  | {:frame_too_large, non_neg_integer(), non_neg_integer()}
```

# `overflow_policy`

```elixir
@type overflow_policy() :: :reset_lane | :drop_oldest | :close_session
```

# `t`

```elixir
@type t() :: %PhoenixWebTransport.LaneScheduler{
  bulk_threshold_bytes: non_neg_integer(),
  idle_reclaim_ms: non_neg_integer(),
  max_lane_bytes: pos_integer(),
  max_lanes: pos_integer(),
  max_outbound_frame_bytes: pos_integer() | :infinity,
  max_parked_bytes: pos_integer(),
  overflow: overflow_policy(),
  parked_bytes: non_neg_integer(),
  pins: %{
    optional(pos_integer()) =&gt; %{
      slot: non_neg_integer(),
      ema: float(),
      last_active_ms: integer()
    }
  },
  queues: %{
    optional(non_neg_integer()) =&gt; %PhoenixWebTransport.LaneScheduler.SlotQueue{
      bytes: term(),
      frames: term(),
      frames_count: term(),
      partial_head: term(),
      urgency: term()
    }
  },
  soft_send_queue_bytes: non_neg_integer()
}
```

# `tier`

```elixir
@type tier() :: :control | :small | :bulk
```

# `any_parked?`

```elixir
@spec any_parked?(t()) :: boolean()
```

Returns true when at least one chunk is parked across any slot.

# `drain_order`

```elixir
@spec drain_order(t()) :: [non_neg_integer()]
```

Returns non-empty slot ids ordered for draining: control slot `0` first,
then component slots ordered by `{urgency, pending_bytes, slot}` ascending.

# `drop_slot`

```elixir
@spec drop_slot(t(), non_neg_integer()) :: t()
```

Clears all queued data for `slot` and decrements `parked_bytes`.

# `enqueue`

```elixir
@spec enqueue(
  t(),
  non_neg_integer(),
  [binary()],
  PhoenixWebTransport.Lanes.urgency(),
  keyword()
) ::
  {:ok, t()} | {:overflow, overflow_action(), t()}
```

Enqueues an encoded frame's binary chunks onto `slot` with `urgency`.

Options:
  * `:frame_bytes` - logical frame size checked against `max_outbound_frame_bytes`
    (defaults to the sum of `chunks`, so callers adding stream/frame headers
    can still enforce the payload limit consistently).

Returns `{:ok, scheduler}` or `{:overflow, action, scheduler}` where `action`
is `:close_session`, `{:reset_lane, slot}`, `:dropped_new`, or
`{:frame_too_large, size, max}` — the last one closes the session instead of
buffering a single oversized frame whole.

# `frames_remaining`

```elixir
@spec frames_remaining(t(), non_neg_integer()) :: non_neg_integer()
```

Returns how many whole frames `slot` still holds. An in-flight `partial_head`
frame is not counted, so `0` means every frame in this slot is fully sent.

# `lane_bytes`

```elixir
@spec lane_bytes(t()) :: %{optional(non_neg_integer()) =&gt; non_neg_integer()}
```

Returns parked byte counts per slot.

# `new`

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

Builds a new scheduler from session/listener options.

# `pop_chunk`

```elixir
@spec pop_chunk(t(), non_neg_integer()) ::
  {:ok, binary(), PhoenixWebTransport.Lanes.urgency(), t()} | :empty
```

Pops the next binary chunk from `slot` in O(1), returning
`{:ok, chunk, urgency, scheduler}` or `:empty`.

# `requeue_head_chunk`

```elixir
@spec requeue_head_chunk(
  t(),
  non_neg_integer(),
  binary(),
  PhoenixWebTransport.Lanes.urgency()
) :: t()
```

Returns a previously popped head chunk back to the front of `slot`'s queue
in O(1) when the underlying QUIC send queue is full or congested.

# `slot_bytes`

```elixir
@spec slot_bytes(t(), non_neg_integer()) :: non_neg_integer()
```

Returns the current pending bytes for `slot`.

# `slot_for`

```elixir
@spec slot_for(t(), non_neg_integer(), non_neg_integer(), integer()) ::
  {non_neg_integer(), tier(), t()}
```

Assigns `{slot, tier, scheduler}` for `lane` using 3-tier isolation and
size-EMA sticky pinning.

# `slot_parked?`

```elixir
@spec slot_parked?(t(), non_neg_integer()) :: boolean()
```

Returns true when `slot` currently has parked bytes.

---

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