PhoenixWebTransport.LaneScheduler (phoenix_wtransport v0.1.0)

Copy Markdown View Source

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

Summary

Functions

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

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

Clears all queued data for slot and decrements parked_bytes.

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

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.

Returns parked byte counts per slot.

Builds a new scheduler from session/listener options.

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

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.

Returns the current pending bytes for slot.

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

Returns true when slot currently has parked bytes.

Types

overflow_action()

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

overflow_policy()

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

t()

@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()) => %{
      slot: non_neg_integer(),
      ema: float(),
      last_active_ms: integer()
    }
  },
  queues: %{
    optional(non_neg_integer()) => %PhoenixWebTransport.LaneScheduler.SlotQueue{
      bytes: term(),
      frames: term(),
      frames_count: term(),
      partial_head: term(),
      urgency: term()
    }
  },
  soft_send_queue_bytes: non_neg_integer()
}

tier()

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

Functions

any_parked?(lane_scheduler)

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

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

drain_order(lane_scheduler)

@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(scheduler, slot)

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

Clears all queued data for slot and decrements parked_bytes.

enqueue(scheduler, slot, chunks, urgency, opts \\ [])

@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(lane_scheduler, slot)

@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(lane_scheduler)

@spec lane_bytes(t()) :: %{optional(non_neg_integer()) => non_neg_integer()}

Returns parked byte counts per slot.

new(opts \\ [])

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

Builds a new scheduler from session/listener options.

pop_chunk(scheduler, slot)

@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(scheduler, slot, chunk, urgency)

@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(lane_scheduler, slot)

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

Returns the current pending bytes for slot.

slot_for(scheduler, lane, payload_size, now_ms \\ System.monotonic_time(:millisecond))

@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?(lane_scheduler, slot)

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

Returns true when slot currently has parked bytes.