Skip to main content

HttpClient

Struct HttpClient 

Source
pub struct HttpClient { /* private fields */ }
Available on crate feature 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’s poll_ready is always Ready (internal pool/queue) — tower::balance degrades 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

Source

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.

Source

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.

Source

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.

Source

pub fn with_tls(tls_config: Arc<ClientConfig>) -> Self

Available on crate feature 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

Source§

type ResponseBody = Incoming

The response body type.
Source§

type Error = ConnectError

The error type.
Source§

fn send( &self, request: Request<ClientBody>, ) -> BoxFuture<'static, Result<Response<Self::ResponseBody>, Self::Error>>

Send an HTTP request and receive a response.
Source§

impl Clone for HttpClient

Source§

fn clone(&self) -> HttpClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HttpClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more