dispatch

package
v0.39.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dispatch routes a single inbound relay frame through a handler table. Route decodes the inner protocol.Envelope, refuses frames whose type is not a known inbound app-frame type via the protocol.IsKnownAppType check, and dispatches to the handler registered for the envelope Type. Conn is the per-conn_id state a handler sees: it owns the monotonic outbound id counter and the conn's outbound send seam. The package is carrier-agnostic — it imports internal/protocol and internal/devices only.

The demux that owns a goroutine per conn_id and calls Route lives in the v2 session manager (internal/relay/v2session.go); this package provides the single-frame routing primitive that manager dispatches through.

The handler table is supplied by the caller. Frames whose Envelope.Type has no registered handler fall through to a protocol.unsupported error reply; encrypted or otherwise unknown-type frames are refused via the protocol.IsKnownAppType check and map to protocol.unsupported / protocol.unknown_type. Malformed inner frames map to protocol.malformed.

Security / operational notes (per the spec's Security review, #307):

  • Inbound frame size cap is inherited from internal/transport's WS read path. Route does not re-enforce; verb slices likewise rely on the transport cap rather than per-handler limits.
  • Log policy: Route diagnostics carry conn_id, envelope type, envelope id, and the decode-error class — never the raw frame payload. Verb slices crossing this code path (message bodies, push tokens) must keep the same posture.
  • Wire-side error envelopes carry only the Code* string plus a static descriptive Message. No decode-error text, stack info, or anything derived from untrusted input is echoed back.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Route added in v0.14.0

func Route(ctx context.Context, logger *slog.Logger, conn *Conn, handlers map[string]Handler, frame json.RawMessage)

Route dispatches a single inbound envelope frame through handlers, applying the malformed / IsKnownAppType / unknown-type error-envelope paths. Suitable for callers that own their own per-conn goroutine and only need single-frame handler-table dispatch (e.g. the v2 session manager's post-AEAD-decrypt dispatch).

Error replies (malformed envelope JSON, unsupported v1 features, unknown envelope type, no registered handler) are emitted via conn.Send → conn.outbound. A non-nil error returned from the handler itself is logged at WARN; no automatic reply is synthesised. handlers may be nil — every envelope then falls through to the "no handler registered" reply path.

Route does NOT change conn.outbound's blocking behaviour: the caller is responsible for sizing the channel so handler+Route replies fit without head-of-line-blocking the dispatch loop.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn is the per-conn_id state exposed to handlers. It owns the monotonic outbound id counter and the conn's outbound send seam.

func NewConn added in v0.14.0

func NewConn(id string, outbound chan<- protocol.RoutingEnvelope, auth *devices.Device) *Conn

NewConn constructs a *Conn for production callers that own their own per-conn goroutine and dispatch envelopes through the handler table via Route (e.g. the v2 session manager, which decrypts a noise_msg before dispatching the inner envelope). The caller owns outbound and is responsible for draining it.

Distinct from NewTestConn only in policy: NewTestConn carries the "test fixtures only" restriction; NewConn is the production-allowed equivalent. The Conn returned has nextID at zero, so the first NextID() call returns 1.

func NewTestConn

func NewTestConn(id string, outbound chan<- protocol.RoutingEnvelope, auth *devices.Device) *Conn

NewTestConn constructs a *Conn for verb-handler test fixtures. Test fixtures only — do not call from production code; production callers use NewConn. The returned Conn has nextID at zero, so the first NextID() call returns 1.

func (*Conn) Auth

func (c *Conn) Auth() *devices.Device

Auth returns the authenticated device snapshot for this conn, or nil if the conn was constructed without an auth device (a pre-auth path or a test fixture that passes nil). Verb handlers MUST nil-check the result before dereferencing.

func (*Conn) ConnID

func (c *Conn) ConnID() string

ConnID returns the relay-assigned conn_id this Conn dispatches for.

func (*Conn) MultiAgent added in v0.27.0

func (c *Conn) MultiAgent() bool

MultiAgent reports whether this conn's client negotiated protocol.CapabilityMultiAgent. Handlers that send agent-aware content gate on it; false for a conn constructed without SetMultiAgent.

func (*Conn) NextID

func (c *Conn) NextID() uint64

NextID returns the next monotonic outbound envelope id for this conn. Starts at 1 on the first call. Concurrent-safe even though the conn's owning goroutine is the only writer today — atomic is cheap insurance for future fan-out inside a handler.

func (*Conn) Reply

func (c *Conn) Reply(ctx context.Context, req protocol.Envelope, respType string, payload json.RawMessage) error

Reply builds a response envelope keyed by NextID with InReplyTo set to req.ID and TS set to time.Now().UTC(), then Send's it. This is the load-bearing helper for the per-conn "in_reply_to matches request id" invariant.

func (*Conn) Send

func (c *Conn) Send(ctx context.Context, env protocol.Envelope) error

Send wraps env in a RoutingEnvelope addressed to this conn and pushes it onto this conn's outbound channel. Blocks on backpressure; returns ctx.Err if ctx is cancelled while blocked. Caller is responsible for env.ID and env.TS — use Reply for the request/response convenience path.

func (*Conn) SetMultiAgent added in v0.27.0

func (c *Conn) SetMultiAgent(v bool)

SetMultiAgent records the conn's negotiated multi_agent decision. It is a setter rather than a constructor argument so NewConn and NewTestConn keep their signatures. Call it before the Conn is handed to Route or a handler: the write is unsynchronised and relies on that hand-off to publish it.

type Handler

type Handler func(ctx context.Context, c *Conn, env protocol.Envelope) error

Handler processes a single inbound envelope on a phone conn. Returning a non-nil error is logged at WARN and otherwise ignored; Route synthesises no reply from a handler return value. Handlers reply by calling Conn.Reply or Conn.Send.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL