Skip to content

Add lazy_with_connector + Unix socket support#15

Merged
iainmcgin merged 2 commits intoanthropics:mainfrom
iantay-ant:uds-and-generic-connector
Mar 26, 2026
Merged

Add lazy_with_connector + Unix socket support#15
iainmcgin merged 2 commits intoanthropics:mainfrom
iantay-ant:uds-and-generic-connector

Conversation

@iantay-ant
Copy link
Copy Markdown
Contributor

Adds Http2Connection::lazy_with_connector and connect_with_connector as the generic transport escape hatch (same pattern as tonic's Endpoint::connect_with_connector). Callers provide a tower::Service<Uri> that yields any hyper::rt::Read + Write stream; the library boxes it internally and runs the h2 handshake.

lazy_unix / connect_unix are thin convenience wrappers demonstrating the pattern — they build a service_fn closure that dials tokio::net::UnixStream.

BoxedIo is un-gated from client-tls since the custom-connector path uses the same type erasure. The box allocation is once per connection establishment, not per request.

API

// Generic — any transport
pub fn lazy_with_connector<C>(connector: C, authority: Uri) -> Self
where
    C: tower::Service<Uri> + Send + 'static,
    C::Response: hyper::rt::Read + hyper::rt::Write + Send + Unpin + 'static,
    C::Error: Into<BoxError>,
    C::Future: Send + 'static,

// Convenience — Unix domain socket
#[cfg(unix)]
pub fn lazy_unix(path: impl Into<PathBuf>, authority: Uri) -> Self

Example

let conn = Http2Connection::lazy_with_connector(
    tower::service_fn(|_uri: Uri| async {
        let stream = tokio::net::UnixStream::connect("/tmp/app.sock").await?;
        Ok::<_, std::io::Error>(hyper_util::rt::TokioIo::new(stream))
    }),
    "http://localhost".parse().unwrap(),
);

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 25, 2026

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

Adds Http2Connection::lazy_with_connector and connect_with_connector as
the generic transport escape hatch (same pattern as tonic's
Endpoint::connect_with_connector). Callers provide a tower::Service<Uri>
that yields any hyper::rt::Read + Write stream; the library boxes it and
runs the h2 handshake.

lazy_unix / connect_unix are thin wrappers demonstrating the pattern —
they build a service_fn closure that dials tokio::net::UnixStream.

BoxedIo is un-gated from client-tls since the custom-connector path uses
the same type erasure. The box allocation is once per connection
establishment, not per request.
@iantay-ant iantay-ant force-pushed the uds-and-generic-connector branch from 6f1b0f7 to 9290e30 Compare March 25, 2026 23:07
@iantay-ant
Copy link
Copy Markdown
Contributor Author

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

github-actions bot added a commit that referenced this pull request Mar 25, 2026
- Mark the lazy_with_connector doctest as ignore (UnixStream doesn't
  compile on non-Unix targets)
- Add #[cfg_attr(docsrs, doc(cfg(unix)))] to lazy_unix/connect_unix
  so docs.rs shows platform-availability badges
- Add crate-level #![cfg_attr(docsrs, feature(doc_cfg))] to enable
  the doc_cfg machinery
- Add construction and failure-path tests for lazy_with_connector,
  connect_with_connector, lazy_unix, connect_unix
@iainmcgin iainmcgin merged commit e3fafcb into anthropics:main Mar 26, 2026
10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 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