Skip to content

server: public ConnectionInfo; Server::serve_connection returning ConnectionClosed - #306

Merged
iainmcgin merged 2 commits into
mainfrom
rpb/a-connection-info
Sep 18, 2026
Merged

iainmcgin merged 2 commits into
mainfrom
rpb/a-connection-info

Conversation

@rpb-ant

@rpb-ant rpb-ant commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Before this, owning the accept step meant raw hyper, which drops every timeout, retirement and drain guarantee. After:

let mut info = ConnectionInfo::new().with_peer_addr(peer).with_peer_certs(chain);
info.extensions_mut().insert(tenant); // cloned into every request
let closed = server.serve_connection(stream, info, drain.clone().wait()).await;
metrics.record(closed.reason()); // Closed | Shutdown | MaxAge | Idle | MaxRequests | Error
  • #[non_exhaustive] ConnectionInfo, formerly the private PeerInfo, also at the crate root. PeerAddr / PeerCerts on requests always come from its peer fields, never from the extensions.
  • Server::serve_connection(&self, io, info, shutdown) -> impl Future<Output = ConnectionClosed> + Send + 'static serves one already-accepted stream with the server's service and connection settings, on whichever runtime polls the future. Server::with_tls does not apply here: the caller completes any TLS handshake first.
  • ConnectionClosed::reason() -> CloseReason (#[non_exhaustive]). Once shutdown or a retirement trigger tells a connection to wind down, that reason is final: a peer that errors mid-drain, or that sent nothing before shutdown, reports the wind-down reason, not Error.

First of four stacked PRs: #306#307#308#304. Part of #191 and #49.

… `ConnectionClosed`

The private `PeerInfo` becomes the public `#[non_exhaustive]`
`ConnectionInfo` (`new`, `with_peer_addr`, `with_peer_certs`,
`peer_addr() -> Option<SocketAddr>`, `peer_certs()`, `extensions()`,
`extensions_mut()`): what is known about a connection before its first
request, including connection-scoped extensions that are cloned into
every request. `PeerAddr` / `PeerCerts` on requests are always set from
its typed fields, never from its extensions.

`Server::serve_connection(io, info, shutdown)` serves one stream the
caller accepted with the server's service and every connection setting
configured on it, on whichever runtime polls it, and resolves to
`ConnectionClosed` whose `reason()` names how it ended (`Closed`,
`Shutdown`, `MaxAge`, `Idle`, `MaxRequests`, `Error`) — the states the
connection lifecycle already distinguished. The built-in loop is
unchanged apart from constructing `ConnectionInfo`.

Tests: built-ins are authoritative over inserted extensions (and a
forged `PeerCerts` never reaches requests); a hand-written loop over
`Server::serve_connection` carries extensions and `PeerAddr`, is retired
by max age, and reports `MaxAge` / `Closed`.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BgcfmMV1ECyzhbqGvUV1a7
Signed-off-by: Ryan Brewster <rpb@anthropic.com>
A connection told to shut down or retire before its peer sent a byte
reported `CloseReason::Error`: hyper-util cancels protocol detection
with an `Interrupted` error in that state, and any error overrode the
reason. Once graceful shutdown has been issued, the shutdown or
retirement reason now stands; `Error` is reserved for errors while
serving.

The `ConnectionInfo` docs also claimed a request never reports a peer
the transport did not see; under `Server::serve_connection` the caller
records the peer, so they now say so.

Signed-off-by: Iain McGinniss <309153+iainmcgin@users.noreply.github.com>

@iainmcgin iainmcgin 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.

[claude code] Approving under the maintainers' agreement to review and merge each other's PRs. The added fix commits went through code, API, doc and deslop review; CI, server conformance and the MSRV check pass.

@iainmcgin
iainmcgin added this pull request to the merge queue Sep 18, 2026
Merged via the queue into main with commit 09ff8be Sep 18, 2026
14 checks passed
@iainmcgin
iainmcgin deleted the rpb/a-connection-info branch September 18, 2026 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants