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
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 ¶
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 ¶
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) NextID ¶
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 ¶
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.