pub struct HttpClient { /* private fields */ }client only.Expand description
General-purpose HTTP client supporting both HTTP/1.1 and HTTP/2.
This wraps hyper_util::client::legacy::Client with sensible defaults.
It auto-negotiates HTTP/1.1 vs HTTP/2 via ALPN (for https://) or
defaults to HTTP/1.1 for cleartext http://.
§When to use this
- Connect protocol over HTTP/1.1 (the main use case)
- You genuinely don’t know whether the server speaks h/1.1 or h/2
- You want ALPN-based protocol negotiation for TLS
§When NOT to use this
For gRPC (which mandates HTTP/2), prefer SharedHttp2Connection:
HttpClient’spoll_readyis alwaysReady(internal pool/queue) —tower::balancedegrades to random selection.- For HTTP/2, the internal pool holds exactly ONE shared connection —
all requests contend on a single h2
Mutex<Inner>, creating a throughput ceiling at ~30–40k req/s.
Http2Connection has honest poll_ready and is a single connection
by design, so you can create N of them and balance across them properly.
Available when the client feature is enabled.
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn builder() -> HttpClientBuilder
pub fn builder() -> HttpClientBuilder
Returns a builder for configuring connector-level options before choosing a transport flavour.
HttpClient::plaintext() is equivalent to
HttpClient::builder().plaintext(), and likewise for the other
constructors.
Sourcepub fn plaintext() -> Self
pub fn plaintext() -> Self
Create a plaintext HTTP client. Only for http:// URIs.
Errors at send-time if given an https:// URI — use
with_tls for TLS.
The client uses connection pooling and supports HTTP/1.1 and HTTP/2 over cleartext. TCP_NODELAY is enabled to avoid Nagle + delayed ACK latency on small messages.
Sourcepub fn plaintext_http2_only() -> Self
pub fn plaintext_http2_only() -> Self
Create a plaintext HTTP client with HTTP/2 prior-knowledge (h2c) only.
Only for http:// URIs. Errors at send-time on https://.
For TLS + HTTP/2-only (e.g. gRPC over TLS), use
Http2Connection::connect_tls instead — there is no TLS equivalent
of this constructor.
Uses HTTP/2 prior knowledge for cleartext connections. Required for gRPC over cleartext (gRPC mandates HTTP/2).
Note: For gRPC, prefer SharedHttp2Connection over this —
it has honest poll_ready and composes with tower::balance. This
method pins you to one connection per host with no way to scale out.
Sourcepub fn with_tls(tls_config: Arc<ClientConfig>) -> Self
Available on crate feature client-tls only.
pub fn with_tls(tls_config: Arc<ClientConfig>) -> Self
client-tls only.Create a TLS HTTP client. Only for https:// URIs.
Errors at send-time if given an http:// URI — use
plaintext for cleartext.
ALPN is set to ["h2", "http/1.1"] for HTTP/2-with-fallback
auto-negotiation. TCP_NODELAY is enabled.
§Certificate rotation
The config may contain a custom ResolvesClientCert for dynamic
cert rotation. rustls::ClientConfig stores the resolver as
Arc<dyn ResolvesClientCert>, so when this function clones the
config to set ALPN, the same resolver instance is shared — a
background rotation task holding its own Arc continues working.
§Example
use connectrpc::client::HttpClient;
use connectrpc::rustls;
use std::sync::Arc;
let tls_config = Arc::new(
rustls::ClientConfig::builder()
.with_root_certificates(roots)
.with_no_client_auth(),
);
let http = HttpClient::with_tls(tls_config);
let client = GreetServiceClient::new(http, config);Trait Implementations§
Source§impl ClientTransport for HttpClient
impl ClientTransport for HttpClient
Source§type ResponseBody = Incoming
type ResponseBody = Incoming
Source§type Error = ConnectError
type Error = ConnectError
Source§fn send(
&self,
request: Request<ClientBody>,
) -> BoxFuture<'static, Result<Response<Self::ResponseBody>, Self::Error>>
fn send( &self, request: Request<ClientBody>, ) -> BoxFuture<'static, Result<Response<Self::ResponseBody>, Self::Error>>
Source§impl Clone for HttpClient
impl Clone for HttpClient
Source§fn clone(&self) -> HttpClient
fn clone(&self) -> HttpClient
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more