feat(session): add RequestMux for routing inbound requests by type#31
Merged
Conversation
RequestMux is the request-stream counterpart of Demux: it replaces the hand-rolled "AcceptRequest loop + type switch + dispatch" a server writes with per-message-type handler registration. Handle(message.Type, h), OnUnknown, and Run(ctx, sess) mirror Demux's API and concurrency contract (synchronous dispatch; handlers spawn a goroutine for long-lived streams). Run surfaces AcceptRequest errors unchanged so the caller can escalate the session-fatal ones (§10.1 Request-ID violations, token-cache faults) by closing the session — the unmatched-type default rejects with REQUEST_ERROR NOT_SUPPORTED. Includes table tests (routing by type, late registration, OnUnknown, and the default reject), an ExampleRequestMux, and a README row. Relay adoption is deferred: its per-type limiter + token-verify pre-dispatch hooks are a separate concern. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
HandleType[T](mux, func(*Request, T)) registers a handler keyed by T's message.Type and hands the handler the already-asserted typed message, so callers don't repeat req.First.(*message.X). It's a free function because Go methods can't take type parameters; the type key is derived from a zero T (the message Type() methods are constant returns, safe on a nil pointer). Adds TestRequestMuxHandleType and switches the SUBSCRIBE branch of ExampleRequestMux to HandleType. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
RequestMuxis the request-stream counterpart ofDemux. Today a server (the relay, or anysession.Server-based app) hand-rolls anAcceptRequestloop + a big type switch + dispatch.RequestMuxlets it register a handler permessage.Typeinstead:The API and concurrency contract deliberately mirror
Demux:Handle(t message.Type, h)/OnUnknown(f)/Run(ctx, *Session) error.Runis executing (a server learns some types to serve only after startup).AcceptRequest, exactly likeDemux.Run. A handler that keeps a request stream open for a subscription's lifetime spawns a goroutine.OnUnknown→REQUEST_ERROR NOT_SUPPORTED+ FIN, so the stream can't leak.RunsurfacesAcceptRequesterrors unchanged, with a doc note that the session-fatal ones (§10.1 Request-ID parity/monotonicity, token-cache faults) must be escalated by closing the session — this is the natural seam for the later "self-close on protocol violation" cleanup.Why
Removes the boilerplate accept-loop/type-switch from every server built on the library, matching the ergonomics
Demuxalready gives the subscriber/data side.Relay adoption is deferred on purpose: the relay's
dispatchalso runs a per-type limiter and token verification before handing off, which needs a pre-dispatch hook rather than a like-for-like swap. Keeping that out of this PR keeps a hot path untouched and the change reviewable.Testing
TestRequestMuxRoutesByType— routing by type, a handler registered afterRunstarts, andOnUnknownfor an unregistered type.TestRequestMuxDefaultRejectsUnhandled— noOnUnknown→ requester receivesREQUEST_ERROR NOT_SUPPORTED.ExampleRequestMux(compile-checked) + README row.go test -race ./pkg/moqt/session/..., fullgo test ./...,golangci-lint run, and themodernizestandalone check — all clean.🤖 Generated with Claude Code