Repository navigation
service: DeadlinePolicy for server-side timeout moderation - #103
Merged
Merged
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
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
force-pushed
the
iainmcgin/issue-92-deadline-policy
branch
from
May 14, 2026 22:31
a019107 to
99f03d3
Compare
iainmcgin
marked this pull request as ready for review
May 14, 2026 23:07
iainmcgin
enabled auto-merge (squash)
May 15, 2026 15:26
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.
Fixes #92.
Adds
DeadlinePolicyfor server-side moderation of client-asserted RPC deadlines. A client controls the per-request timeout viaConnect-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.DeadlinePolicyclamps 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.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 leastwith_max()andwith_default().minisDuration(notOption) because zero is a natural identity for a floor;max/defaultareOption<Duration>because there's no clean identity for them. The asymmetry is deliberate.Stream-body enforcement (when
enforce_on_streamsis set) is a pin-projected wrapper applied to server- and bidi-streaming response bodies. Unary and client-streaming responses are already bounded by the existingtokio::time::timeouton the handler future.inter_message_timeoutre-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 targetconnectrpc::deadlinewith the path and before/after durations.Notes for reviewers
Server::with_deadline_policy()is a new delegating builder (renamed fromdeadline_policy()after review to matchConnectRpcService::with_deadline_policyand avoid colliding with the same-named getter onConnectRpcService).Serverdoes not yet proxyConnectRpcService::with_compression()/with_compression_policy()/with_limits()— that's a pre-existing gap; theServer::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 documentfrom_serviceas the configuration path.enforce_on_streamsandinter_message_timeoutare independent knobs — either alone wraps the stream. Both default off.RequestContext.deadlinereflects the moderated value once a policy is set; the field doc cross-linksDeadlinePolicy. PR RequestContext: #[non_exhaustive] + accessor methods #101 (issue RequestContext field exposure: decide #[non_exhaustive] / accessor policy before adding fields #89) replaces the field with adeadline()accessor and addstime_remaining()— there will be a small merge conflict inresponse.rsbetween this PR and RequestContext: #[non_exhaustive] + accessor methods #101, expected and trivial.DeadlinePolicy::new()is a strict identity (moderatereturns the input unchanged;enforce_on_response_streamreturns the stream unchanged), conformance behavior is provably unaffected without a configured policy. CI runs the full suite.connectrpc/src/: ~250 lines excluding tests and doc comments. Thedeadline.rsfile is large because of doc-comment density on every public knob.