Skip to content

spec: add Spec, StreamType, IdempotencyLevel; thread through dispatch - #112

Merged
iainmcgin merged 4 commits into
connectrpc:mainfrom
aknott-ant:aknott/interceptors-pr1-spec
May 19, 2026
Merged

iainmcgin merged 4 commits into
connectrpc:mainfrom
aknott-ant:aknott/interceptors-pr1-spec

Conversation

@aknott-ant

@aknott-ant aknott-ant commented May 18, 2026 •

Copy link
Copy Markdown
Collaborator

Add static per-method metadata that handlers (and a future interceptor surface) can read without re-parsing the request URL.

  • New connectrpc::spec module: Spec (procedure path, stream type, idempotency level, client/server side), StreamType, IdempotencyLevel, SpecOrigin. All Copy and 'static, with const fn constructors (Spec::server(...) / Spec::client(...)) so codegen can emit Spec constants in .rodata.
  • MethodDescriptor gains spec: Option<Spec>, becomes #[non_exhaustive], and grows from_kind/with_idempotent/with_spec const builders. The dynamic Router returns spec: None (its keys are owned Strings, not &'static str).
  • RequestContext gains spec: Option<Spec> and protocol: Option<Protocol> with with_spec/with_protocol builders.
  • The dispatch path in service.rs threads the looked-up Spec and the negotiated Protocol into every RequestContext (Connect unary, gRPC unary fast path, server/client/bidi streaming).
  • connectrpc-codegen emits a pub const FOO_SERVICE_BAR_SPEC: Spec per method and references it from the generated Dispatcher::lookup.
  • CHANGELOG [Unreleased] entries and a docs/guide.md "Static method metadata (Spec)" subsection covering ctx.spec(), the Router-returns-None gotcha, and the generated <SERVICE>_<METHOD>_SPEC constants.

Protocol lives on RequestContext rather than Spec because it is connection-scoped, not method-scoped (matching connect-go's Spec/Peer split); a const Spec cannot carry a runtime-negotiated field.

SpecOrigin is an enum (Server / Client) rather than connect-go's IsClient bool — closed two-valued domains read better as enums in Rust (spec.origin == SpecOrigin::Client), the constructors Spec::server(...) / Spec::client(...) carry the side at the call site without a builder, and the Spec prefix disambiguates from a request "origin" in the HTTP/CORS sense.

Conformance: 3600/3600 (no behavioral change to wire handling).

Refs #87, #110.

@github-actions

github-actions Bot commented May 18, 2026 •

Copy link
Copy Markdown

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

Add static per-method metadata that handlers (and a future interceptor
surface) can read without re-parsing the request URL.

- New `connectrpc::spec` module: `Spec` (procedure path, stream type,
  idempotency, client/server side), `StreamType`, `IdempotencyLevel`.
  All `Copy` and `'static`, with `const fn` builders so codegen can emit
  `Spec` constants in `.rodata`.
- `MethodDescriptor` gains `spec: Option<Spec>`, becomes
  `#[non_exhaustive]`, and grows `from_kind`/`with_idempotent`/`with_spec`
  const builders. The dynamic `Router` returns `spec: None` (its keys are
  owned `String`s, not `&'static str`).
- `RequestContext` gains `spec: Option<Spec>` and `protocol:
  Option<Protocol>` with `with_spec`/`with_protocol` builders.
- The dispatch path in `service.rs` threads the looked-up `Spec` and the
  negotiated `Protocol` into every `RequestContext` (Connect unary, gRPC
  unary fast path, server/client/bidi streaming).
- `connectrpc-codegen` emits a `pub const FOO_SERVICE_BAR_SPEC: Spec`
  per method and references it from the generated `Dispatcher::lookup`.

`Protocol` lives on `RequestContext` rather than `Spec` because it is
connection-scoped, not method-scoped (matching connect-go's
`Spec`/`Peer` split); a `const Spec` cannot carry a runtime-negotiated
field.

Conformance: 3600/3600 (no behavioral change to wire handling).
@aknott-ant
aknott-ant force-pushed the aknott/interceptors-pr1-spec branch from 873a3c6 to ab3f73d Compare May 18, 2026 18:33
@iainmcgin

Copy link
Copy Markdown
Collaborator

recheck

1 similar comment
@iainmcgin

Copy link
Copy Markdown
Collaborator

recheck

github-actions Bot added a commit that referenced this pull request May 18, 2026
@aknott-ant

Copy link
Copy Markdown
Collaborator Author

I have read the CLA Document and I hereby sign the CLA

iainmcgin added 3 commits May 18, 2026 15:49
Review follow-ups for the Spec/RequestContext metadata PR.

- Replace `is_client: bool` with `origin: SpecOrigin` and side-specific
  constructors `Spec::server(...)` / `Spec::client(...)` (replacing
  `Spec::new(...)`). Closed two-valued domains read better as an enum
  than a bool: `spec.origin == SpecOrigin::Client` over
  `spec.is_client`, and codegen picks the side at the constructor call
  rather than via a builder. The `Spec` prefix on the type name keeps
  it distinct from a request "origin" in the HTTP/CORS sense.
- Rename `idempotency` -> `idempotency_level` (and `with_idempotency`
  -> `with_idempotency_level`) so the field name matches both the
  sibling `stream_type: StreamType` pattern and the proto option name
  (`idempotency_level`); also matches connect-go's `IdempotencyLevel`.
- Add a `const fn` debug assertion in `Spec::server` / `Spec::client`
  that the procedure path starts with `/` and contains a
  `/Service/Method` separator. A malformed path in a const SPEC is now
  a debug-build compile error instead of a silently misleading
  `service()` / `method()` accessor result.
- Add unit tests for `RequestContext::with_spec` / `with_protocol`
  round-trip and `MethodDescriptor::from_kind` / `with_idempotent` /
  `with_spec` const builder chain.
- Add CHANGELOG `[Unreleased]` entries for the new `spec` module under
  `### Added` and the `MethodDescriptor` `#[non_exhaustive]` change
  under `### Breaking`.
- Add a "Static method metadata (`Spec`)" subsection to docs/guide.md
  under Tower middleware: how to read `ctx.spec()`, the `Router`
  returns-`None` gotcha, the generated `<SERVICE>_<METHOD>_SPEC`
  constants, and the `connect-go` `Spec`/`Peer` split.
- Regenerate all checked-in `*.__connect.rs` for the renamed builders.

Refs connectrpc#87, connectrpc#110.
…r test

clippy::assertions_on_constants flags `assert!(!DESC.idempotent)` when
DESC is a `const`. Bind it as a `let` for the runtime assertions and
keep a separate `const _: MethodDescriptor = ...` to verify the builder
chain is `const`-evaluable.
@iainmcgin
iainmcgin merged commit d6cd2b0 into connectrpc: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