Skip to content

response: add RequestContext::path() — the requested procedure path - #116

Merged
iainmcgin merged 1 commit into
mainfrom
iain/request-context-path
May 19, 2026
Merged

iainmcgin merged 1 commit into
mainfrom
iain/request-context-path

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Why

Spec (#112) gives interceptors and span builders the resolved method's static metadata, but only when a generated FooServiceServer<T> dispatcher matched the route — Spec.procedure is &'static str, and the dynamic Router can't supply one because its keys are owned Strings. By the time an interceptor (#114) runs, handle_*_request has already consumed the http::Request, so there is no way back to the request URI. An interceptor that must label or gate every request — an auth boundary, a span builder, a rate limiter — therefore has no reliable source for the method name on the dynamic-Router path.

What

RequestContext::path() carries the requested procedure path (/package.Service/Method) unconditionally — populated at every dispatch site from the request URI. It is the wire truth; spec() remains the source of resolved static metadata (stream type, idempotency level). When both are present they're identical strings; path() exists for the cases where spec() cannot be.

Storage is Option<String> (matching spec()/protocol()), None only for hand-built contexts. The leading slash matches Spec::procedure, connect-go's Spec.Procedure, and http::Uri::path().

The five RequestContext::new(...) call sites already have the stripped lookup-key form (pkg.Service/Method) in scope; the leading slash is reconstructed via format!("/{path}"). strip_prefix('/') removes at most one leading slash, so the round-trip is exact for any HTTP request that reaches the dispatch layer.

Confidence

  • request_context_with_path (response.rs) — builder round-trips, None for hand-built contexts, Some("") preserved verbatim.
  • path_flows_to_handler_context (service.rs) — dispatches a request to /svc/Method through the dynamic Router and asserts the handler sees path() == Some("/svc/Method") and spec() == None — the case path() exists for.
  • cargo test (306 pass), cargo fmt, cargo clippy --all-targets -- -D warnings, cargo doc all clean. cargo check --all-features clean.
  • No wire-format change (the new field is consumed only by handler code).

Possible follow-ups (not blocking)

  • Each request currently allocates twice on the dispatch path: once for the stripped lookup key (pre-existing) and once for format!("/{path}"). They can be collapsed to one by capturing the un-stripped URI path first and slicing it for the lookup key, at the cost of restructuring borrow scopes. ~30-byte alloc per request is noise next to the body buffer and HeaderMap clone on the same path; worth it if benches/rpc/ shows a difference.
  • Test coverage exercises handle_unary_request; the gRPC-fast-path and streaming entry points use the same format!("/{path}") recipe but aren't independently covered.

🤖 Generated with Claude Code

Spec (#112) gives interceptors and span builders the resolved method's
static metadata, but only when a generated FooServiceServer<T> resolved
the route — Spec.procedure is &'static str, and the dynamic Router can't
supply one because its keys are owned Strings. An interceptor that must
label or gate every request (auth, rate limiting, tracing) has no way
back to the request URI by the time it runs: handle_*_request consumes
the http::Request before constructing RequestContext. The result is that
Spec is the only source of the procedure name, and it isn't always there.

RequestContext::path() carries the requested procedure path
("/package.Service/Method") unconditionally. It is the wire truth, set
from the request URI at every dispatch site, with the leading slash to
match Spec::procedure and connect-go's Spec.Procedure. spec() remains
the source of richer static metadata (stream type, idempotency); path()
is the always-present method name.

The leading slash is reconstructed from the stripped path variable
already in scope at each RequestContext::new site (the dispatch path
strips it for Dispatcher::lookup keys); strip_prefix removes at most one
leading slash so the round-trip is exact for any HTTP request that
reached the dispatch layer.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@iainmcgin
iainmcgin marked this pull request as ready for review May 19, 2026 00:38
@iainmcgin
iainmcgin requested review from aknott-ant and kollektiv May 19, 2026 00:38
@iainmcgin
iainmcgin enabled auto-merge (squash) May 19, 2026 00:39
@iainmcgin
iainmcgin merged commit 568e385 into main May 19, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants