PhoenixWebTransport.SessionEngine (phoenix_wtransport v0.1.0)

Copy Markdown View Source

Backend-agnostic WebTransport session state machine driving a Phoenix.Socket.Transport.

PhoenixWebTransport.Quic.Session delegates routing, origin validation, dev code reloading, connect_info & auth_token bridging, inbound frame decoding, and outbound push/close normalization to this module.

Functions return {engine, actions} (or {:ok | :error, ...} on connect) where actions are backend-neutral effect tuples:

  • {:push, lane, {:text | :binary, binary()}}

  • {:push_barrier, 0, {:text, binary()}, [pos_integer()]}
  • {:push_datagram, {:text | :binary, binary()}}

  • :drain_session
  • {:close_session, close_code :: non_neg_integer(), reason :: binary()}

Summary

Functions

Returns true when socket.connect/1 succeeded or a pre-join :auth frame is pending.

Returns true when a new inbound client stream can be admitted without exceeding max_client_streams.

Returns true while awaiting a pre-join :auth frame before socket.connect/1.

Drops state and byte accounting for a closed inbound stream.

Validates an incoming WebTransport CONNECT request against routes, checks origin, runs dev code reloading if configured, and either invokes socket.connect/1 or defers until the client sends its pre-join :auth frame.

Initiates a graceful shutdown drain (1012 + :drain_session), invoking socket.handle_info(:socket_drain, ...) when initialized.

Routes an inbound RFC 9297 WebTransport datagram through socket.handle_in/2.

Decodes inbound stream bytes into Phoenix frames (handling any pre-join :auth frame first) and dispatches them through socket.handle_in/2.

Routes an Erlang message through socket.handle_info/2 and normalizes pushes and 3/4/5-tuple :stop returns.

Initializes the Phoenix socket inside the session-owning process.

Builds a new session engine from listener or router options.

Registers an inbound client stream.

Calls socket.terminate/2 idempotently when the session stops.

Types

action()

@type action() ::
  {:push, non_neg_integer(), {:text | :binary, binary()}}
  | {:push_barrier, 0, {:text, binary()}, [pos_integer()]}
  | {:push_datagram, {:text | :binary, binary()}}
  | :drain_session
  | {:close_session, close_code(), binary()}

close_code()

@type close_code() :: non_neg_integer()

connect_request()

@type connect_request() :: %{
  optional(:protocol) => binary() | nil,
  :path => binary(),
  optional(:query) => binary() | nil,
  optional(:headers) => [{binary(), binary()}],
  optional(:peer_data) => map() | nil,
  optional(:host) => binary() | nil,
  optional(:port) => :inet.port_number() | nil
}

t()

@type t() :: %PhoenixWebTransport.SessionEngine{
  closing: boolean(),
  endpoint: module() | nil,
  in_buf: %{optional(term()) => binary()},
  in_buf_bytes: non_neg_integer(),
  max_client_streams: pos_integer(),
  max_frame_size: non_neg_integer(),
  max_in_buf_bytes: non_neg_integer(),
  max_lanes: pos_integer(),
  path: String.t(),
  pending_connect: map() | nil,
  routes: [PhoenixWebTransport.Router.route()],
  socket: module() | nil,
  socket_opts: keyword(),
  socket_state: term() | nil,
  terminated?: boolean()
}

Functions

accepted?(session_engine)

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

Returns true when socket.connect/1 succeeded or a pre-join :auth frame is pending.

admit_stream?(session_engine, stream_id)

@spec admit_stream?(t(), term()) :: boolean()

Returns true when a new inbound client stream can be admitted without exceeding max_client_streams.

awaiting_auth?(session_engine)

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

Returns true while awaiting a pre-join :auth frame before socket.connect/1.

close_stream(engine, stream_id)

@spec close_stream(t(), term()) :: t()

Drops state and byte accounting for a closed inbound stream.

connect(engine, req)

@spec connect(t(), connect_request()) :: {:ok, t()} | {:error, pos_integer(), term()}

Validates an incoming WebTransport CONNECT request against routes, checks origin, runs dev code reloading if configured, and either invokes socket.connect/1 or defers until the client sends its pre-join :auth frame.

drain(engine)

@spec drain(t()) :: {t(), [action()]}

Initiates a graceful shutdown drain (1012 + :drain_session), invoking socket.handle_info(:socket_drain, ...) when initialized.

handle_datagram(engine, data)

@spec handle_datagram(t(), binary()) :: {t(), [action()]}

Routes an inbound RFC 9297 WebTransport datagram through socket.handle_in/2.

handle_in(engine, stream_id, data)

@spec handle_in(t(), term(), binary()) :: {t(), [action()]}

Decodes inbound stream bytes into Phoenix frames (handling any pre-join :auth frame first) and dispatches them through socket.handle_in/2.

handle_info(engine, message)

@spec handle_info(t(), term()) :: {t(), [action()]}

Routes an Erlang message through socket.handle_info/2 and normalizes pushes and 3/4/5-tuple :stop returns.

init_socket(engine)

@spec init_socket(t()) :: {:ok, t()}

Initializes the Phoenix socket inside the session-owning process.

If pre-join auth is still pending, returns {:ok, engine} and initializes automatically as soon as the :auth frame arrives.

new(opts)

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

Builds a new session engine from listener or router options.

open_stream(engine, stream_id)

@spec open_stream(t(), term()) :: t()

Registers an inbound client stream.

terminate(engine, reason)

@spec terminate(t(), term()) :: t()

Calls socket.terminate/2 idempotently when the session stops.