Repository navigation
router: store Spec per route so dynamic dispatch carries it - #120
Merged
Merged
Conversation
The dynamic Router returned spec: None from lookup() because its registration API had nowhere to put a Spec. The codegen-emitted register(Router) has the static *_SPEC consts in scope but route() never accepted them, so a Router built through register() was metadata-blind: RequestContext::spec() was always None for handlers and interceptors, even though the generated FooServiceServer<T> dispatcher for the same service supplied a Spec for every method. RequestContext::path() (#116) papers over the procedure name; stream_type and idempotency were lost. Each registered Method now carries an Option<Spec>, set after the fact by Router::with_spec(spec) — the route is keyed by spec.procedure so the codegen can chain it after the route_view* call without changing the route_* signatures. The generated register() emits .with_spec(SPEC_CONST) per method, so a Router built through codegen surfaces the same per-method metadata as the monomorphic dispatcher. Hand-written route_* registrations without a with_spec call behave exactly as before (spec: None). Debug builds assert that with_spec finds a registered route at spec.procedure, and that the route's idempotent flag agrees with spec.idempotency_level — the latter would otherwise silently disable Connect GET for idempotent methods. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
All contributors have signed the CLA ✍️ ✅ |
iainmcgin
marked this pull request as ready for review
May 19, 2026 22:32
aknott-ant
approved these changes
May 19, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Why
The dynamic
Routerreturnedspec: Nonefromlookup()because its registration API had nowhere to put aSpec. The codegen-emittedregister(Router)has the static*_SPECconsts in scope (#112) butroute()never accepted them — so aRouterbuilt throughregister()was metadata-blind:RequestContext::spec()was alwaysNonefor handlers and interceptors, even though the monomorphicFooServiceServer<T>dispatcher for the same service supplies aSpecfor every method.RequestContext::path()(#116) papers over the procedure name;stream_typeandidempotencywere just lost. An interceptor or span builder behaves differently depending on which dispatch path the host wired up — for the same proto.What
Each registered route now carries an
Option<Spec>, set after the fact byRouter::with_spec(spec). TheSpeckeys the route byspec.procedure(leading slash stripped, matching theroute_*methods'format!("{service}/{method}")keying), so the codegen chains.with_spec(SPEC_CONST)after eachroute_view*call without touching theroute_*signatures.lookup()returns the storedSpec.Router::with_specis the one new public method.route_*signatures and behavior are unchanged — a registration withoutwith_specbehaves exactly as before (spec: None).with_specfinds a registered route, and that the route'sidempotentflag agrees withspec.idempotency_level— a mismatch would silently disable Connect GET for an idempotent method, which is a hard-to-find bug.register()now emits.with_spec(SPEC_CONST)per method. Checked-in generated code inbenches/,examples/, andconformance/regenerated.merge_routerscarries each route'sSpecacross the merge (the storage moves with the entry).Confidence
router.rstests: round-trip throughlookup(), back-compat withoutwith_spec,merge_routerspreserves specs, debug-assert panics on unknown route and on idempotency mismatch.service.rstest (spec_flows_to_handler_context): drives a request throughhandle_unary_requestwith awith_spec-attachedRouterand asserts the handler sees theSpecand thatpath()agrees withspec().procedure.Routerpath now surfaces the sameSpecas the codegen dispatcher (they previously asserted the opposite — that's the behavior change this PR is for).cargo fmt,cargo clippy --workspace --all-targets -- -D warnings,cargo docall clean (no new warnings).*.__connect.rsviabuf generatewith the workspace'sprotoc-gen-connect-rustbuild; onlyconnect/outputs changed (buffa/outputs unchanged).🤖 Generated with Claude Code