Skip to content

service: DeadlinePolicy for server-side timeout moderation - #103

Merged
iainmcgin merged 3 commits into
mainfrom
iainmcgin/issue-92-deadline-policy
May 15, 2026
Merged

iainmcgin merged 3 commits into
mainfrom
iainmcgin/issue-92-deadline-policy

Conversation

@iainmcgin

@iainmcgin iainmcgin commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #92.

Adds DeadlinePolicy for server-side moderation of client-asserted RPC deadlines. A client controls the per-request timeout via Connect-Timeout-Ms / grpc-timeout; without a policy the server trusts that value verbatim — a 1ms request cancels the handler mid-write, a 24h request holds a worker indefinitely, no header means no bound, and once a streaming handler returns its stream the items flow unbounded regardless of the header.

DeadlinePolicy clamps the client value to a server-controlled [min, max], supplies a default when the client asserts nothing (or sends garbage), and optionally extends enforcement to streaming response bodies and to the gap between yielded items.

let policy = DeadlinePolicy::new()
    .with_min(Duration::from_millis(5))
    .with_max(Duration::from_secs(30))
    .with_default(Duration::from_secs(10))
    .with_enforce_on_streams(true);
ConnectRpcService::new(router).with_deadline_policy(policy)
// or: Server::new(router).deadline_policy(policy)

DeadlinePolicy::new() is a strict no-op — no clamping, no default, no stream enforcement. Existing services that don't set a policy see exactly the previous behavior, so this is purely additive and safe in 0.5.0. The recommended production starting point is at least with_max() and with_default().

min is Duration (not Option) because zero is a natural identity for a floor; max/default are Option<Duration> because there's no clean identity for them. The asymmetry is deliberate.

Stream-body enforcement (when enforce_on_streams is set) is a pin-projected wrapper applied to server- and bidi-streaming response bodies. Unary and client-streaming responses are already bounded by the existing tokio::time::timeout on the handler future. inter_message_timeout re-arms a per-item timer to detect stalled streams independent of the absolute deadline.

When a clamp changes the client value, a tracing::debug! event fires on target connectrpc::deadline with the path and before/after durations.

Notes for reviewers

  • Server::with_deadline_policy() is a new delegating builder (renamed from deadline_policy() after review to match ConnectRpcService::with_deadline_policy and avoid colliding with the same-named getter on ConnectRpcService). Server does not yet proxy ConnectRpcService::with_compression() / with_compression_policy() / with_limits() — that's a pre-existing gap; the Server::from_service(ConnectRpcService::new(...).with_*(...)) path is the canonical way to set those today. Worth a follow-up to either add the missing proxies or document from_service as the configuration path.
  • enforce_on_streams and inter_message_timeout are independent knobs — either alone wraps the stream. Both default off.
  • RequestContext.deadline reflects the moderated value once a policy is set; the field doc cross-links DeadlinePolicy. PR RequestContext: #[non_exhaustive] + accessor methods #101 (issue RequestContext field exposure: decide #[non_exhaustive] / accessor policy before adding fields #89) replaces the field with a deadline() accessor and adds time_remaining() — there will be a small merge conflict in response.rs between this PR and RequestContext: #[non_exhaustive] + accessor methods #101, expected and trivial.
  • I have not run the conformance suite locally (the runner wasn't downloaded in the dev environment). Since DeadlinePolicy::new() is a strict identity (moderate returns the input unchanged; enforce_on_response_stream returns the stream unchanged), conformance behavior is provably unaffected without a configured policy. CI runs the full suite.
  • Per-route deadline policy is deferred to a follow-up coupled to the typed routing surface (Router::route_* manual routing is stringly typed and surfaces in rustdoc before the codegen path #91). A per-path string map now would be a second stringly-typed route surface that Router::route_* manual routing is stringly typed and surfaces in rustdoc before the codegen path #91 wants to remove.
  • Net change in connectrpc/src/: ~250 lines excluding tests and doc comments. The deadline.rs file is large because of doc-comment density on every public knob.

@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown

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

Connect and gRPC clients control the per-request timeout via
Connect-Timeout-Ms / grpc-timeout. The server previously trusted that
value verbatim: a 1ms request cancels the handler before it can do
anything; a 24h request holds a worker indefinitely; a request with no
header runs unbounded; and once a streaming handler returns its stream,
the items flow without a bound regardless of the header.

DeadlinePolicy gives the server the say. It clamps the client-asserted
timeout to a configured [min, max] range, applies a server-side default
when the client asserts nothing (or sends an unparseable header), and
optionally extends enforcement to streaming response bodies and to the
gap between yielded items.

DeadlinePolicy::new() is a strict no-op - no clamping, no default, no
stream enforcement - so the change is purely additive: services that
don't set a policy see exactly the previous behavior. The recommended
production starting point is at least with_max() and with_default().

Plumbing: the policy lives on ConnectRpcService alongside the
compression registry/policy and applies once per request right after
the timeout header is parsed. The moderated value flows into the
existing tokio::time::timeout wrapper around the handler future and
into RequestContext::deadline. Stream-body enforcement is a small
pin-projected wrapper applied to server- and bidi-streaming response
streams when enforce_on_streams is set.

When a clamp changes the client value, a tracing::debug! event fires on
target connectrpc::deadline with the path and before/after durations.

Per-route deadline policy is deferred to a follow-up coupled to the
typed routing surface (#91) - a per-path string map would be a second
stringly-typed route surface that #91 wants to remove.
@iainmcgin
iainmcgin marked this pull request as ready for review May 14, 2026 23:07
@iainmcgin
iainmcgin requested a review from rpb-ant May 14, 2026 23:07
@iainmcgin
iainmcgin enabled auto-merge (squash) May 15, 2026 15:26

@rpb-ant rpb-ant left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm :shipit:

@iainmcgin
iainmcgin merged commit 7bf0182 into main May 15, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 15, 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.

Deadline is parsed but not enforced — handler runs to completion after Connect-Timeout-Ms lapses

2 participants