ClientBuilder

Struct ClientBuilder 

Source
pub struct ClientBuilder<IL = Identity, OL = Identity, C = DefaultMkClient, LB = DefaultLb> { /* private fields */ }
Available on crate feature client only.
Expand description

A builder for configuring an HTTP Client.

Implementations§

Source§

impl ClientBuilder<Identity, Identity, DefaultMkClient, DefaultLb>

Source

pub fn new() -> Self

Create a new client builder.

Source§

impl<IL, OL, C, LB, DISC> ClientBuilder<IL, OL, C, LbConfig<LB, DISC>>

Source

pub fn load_balance<NLB>( self, load_balance: NLB, ) -> ClientBuilder<IL, OL, C, LbConfig<NLB, DISC>>

Set load balancer for the client.

Source

pub fn discover<NDISC>( self, discover: NDISC, ) -> ClientBuilder<IL, OL, C, LbConfig<LB, NDISC>>

Set service discover for the client.

Source§

impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB>

Source

pub fn layer_inner<Inner>( self, layer: Inner, ) -> ClientBuilder<Stack<Inner, IL>, OL, C, LB>

Add a new inner layer to the client.

The layer’s Service should be Send + Sync + Clone + 'static.

§Order

Assume we already have two layers: foo and bar. We want to add a new layer baz.

The current order is: foo -> bar (the request will come to foo first, and then bar).

After we call .layer_inner(baz), we will get: foo -> bar -> baz.

The overall order for layers is: outer -> LoadBalance -> [inner] -> transport.

Source

pub fn layer_inner_front<Inner>( self, layer: Inner, ) -> ClientBuilder<Stack<IL, Inner>, OL, C, LB>

Add a new inner layer to the client.

The layer’s Service should be Send + Sync + Clone + 'static.

§Order

Assume we already have two layers: foo and bar. We want to add a new layer baz.

The current order is: foo -> bar (the request will come to foo first, and then bar).

After we call .layer_inner_front(baz), we will get: baz -> foo -> bar.

The overall order for layers is: outer -> LoadBalance -> [inner] -> transport.

Source

pub fn layer_outer<Outer>( self, layer: Outer, ) -> ClientBuilder<IL, Stack<Outer, OL>, C, LB>

Add a new outer layer to the client.

The layer’s Service should be Send + Sync + Clone + 'static.

§Order

Assume we already have two layers: foo and bar. We want to add a new layer baz.

The current order is: foo -> bar (the request will come to foo first, and then bar).

After we call .layer_outer(baz), we will get: foo -> bar -> baz.

The overall order for layers is: [outer] -> Timeout -> LoadBalance -> inner -> transport.

Source

pub fn layer_outer_front<Outer>( self, layer: Outer, ) -> ClientBuilder<IL, Stack<OL, Outer>, C, LB>

Add a new outer layer to the client.

The layer’s Service should be Send + Sync + Clone + 'static.

§Order

Assume we already have two layers: foo and bar. We want to add a new layer baz.

The current order is: foo -> bar (the request will come to foo first, and then bar).

After we call .layer_outer_front(baz), we will get: baz -> foo -> bar.

The overall order for layers is: [outer] -> LoadBalance -> inner -> transport.

Source

pub fn mk_load_balance<NLB>( self, mk_load_balance: NLB, ) -> ClientBuilder<IL, OL, C, NLB>

Set a new load balance for the client.

Source

pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
where K: TryInto<HeaderName>, K::Error: Error + Send + Sync + 'static, V: TryInto<HeaderValue>, V::Error: Error + Send + Sync + 'static,

Insert a header to the request.

Source

pub fn set_tls_config<T>(&mut self, tls_config: T) -> &mut Self
where T: Into<TlsConnector>,

Available on crate features rustls or native-tls only.

Set tls config for the client.

Source

pub fn headers(&self) -> &HeaderMap

Get a reference to the default headers of the client.

Source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Get a mutable reference to the default headers of the client.

Source

pub fn set_title_case_headers(&mut self, title_case_headers: bool) -> &mut Self

👎Deprecated since 0.4.0: set_title_case_headers has been removed into http1_config
Available on crate feature http1 only.

Set whether HTTP/1 connections will write header names as title case at the socket level.

Default is false.

Source

pub fn set_max_headers(&mut self, max_headers: usize) -> &mut Self

👎Deprecated since 0.4.0: set_max_headers has been removed into http1_config
Available on crate feature http1 only.

Set the maximum number of headers.

When a response is received, the parser will reserve a buffer to store headers for optimal performance.

If client receives more headers than the buffer size, the error “message header too large” is returned.

Note that headers is allocated on the stack by default, which has higher performance. After setting this value, headers will be allocated in heap memory, that is, heap memory allocation will occur for each response, and there will be a performance drop of about 5%.

Default is 100.

Source

pub fn http1_config(&mut self) -> &mut Config

Available on crate feature http1 only.

Get configuration of http1 part.

Source

pub fn http2_config(&mut self) -> &mut Config

Available on crate feature http2 only.

Get configuration of http2 part.

Source

pub fn disable_tls(&mut self, disable: bool) -> &mut Self

Available on crate features rustls or native-tls only.

Disable TLS for the client.

Default is false, when TLS related feature is enabled, TLS is enabled by default.

Source

pub fn set_pool_idle_timeout(&mut self, timeout: Duration) -> &mut Self

Set idle timeout of connection pool.

If a connection is idle for more than the timeout, the connection will be dropped.

Default is 20 seconds.

Source

pub fn set_max_idle_per_host(&mut self, num: usize) -> &mut Self

Set the maximum number of idle connections per host.

If the number of idle connections on a host exceeds this value, the connection pool will refuse to add new idle connections.

Default is 10240.

Source

pub fn set_connect_timeout(&mut self, timeout: Duration) -> &mut Self

Set the maximum idle time for a connection.

Source

pub fn set_read_timeout(&mut self, timeout: Duration) -> &mut Self

Set the maximum idle time for reading data from the connection.

Source

pub fn set_write_timeout(&mut self, timeout: Duration) -> &mut Self

Set the maximum idle time for writing data to the connection.

Source

pub fn set_request_timeout(&mut self, timeout: Duration) -> &mut Self

Set the maximum idle time for the whole request.

Source

pub fn user_agent<V>(&mut self, val: V) -> &mut Self
where V: TryInto<HeaderValue>, V::Error: Error + Send + Sync + 'static,

Set default User-Agent in request header.

If there is User-Agent given, a default User-Agent will be generated by crate name and version.

Source

pub fn host_mode(&mut self, mode: Host) -> &mut Self

Set mode of client setting Host in headers.

This mode only works when building client by ClientBuilder::build, ClientBuilder::build_without_extra_layers will ignore this config.

For more configurations, refer to Host.

Default is Host::Auto, it will generate a Host by target domain name or address if there is no Host in request headers.

Source

pub fn build<InnerReqBody, OuterReqBody, RespBody>(self) -> Result<C::Target>
where IL: Layer<ClientTransport<InnerReqBody>>, IL::Service: Send + Sync + 'static, LB: MkLbLayer, LB::Layer: Layer<IL::Service>, <LB::Layer as Layer<IL::Service>>::Service: Send + Sync, OL: Layer<<LB::Layer as Layer<IL::Service>>::Service>, OL::Service: Service<ClientContext, Request<OuterReqBody>, Response = Response<RespBody>, Error = ClientError> + Send + Sync + 'static, C: MkClient<Client<OuterReqBody, RespBody>>, InnerReqBody: Send, OuterReqBody: Send + 'static, RespBody: Send,

Build the HTTP client with default configurations.

This method will insert some default layers: Timeout, UserAgent and Host, and the final calling sequence will be as follows:

Source

pub fn build_without_extra_layers<InnerReqBody, OuterReqBody, RespBody>( self, ) -> Result<C::Target>
where IL: Layer<ClientTransport<InnerReqBody>>, IL::Service: Send + Sync + 'static, LB: MkLbLayer, LB::Layer: Layer<IL::Service>, <LB::Layer as Layer<IL::Service>>::Service: Send + Sync, OL: Layer<<LB::Layer as Layer<IL::Service>>::Service>, OL::Service: Service<ClientContext, Request<OuterReqBody>, Response = Response<RespBody>, Error = ClientError> + Send + Sync + 'static, C: MkClient<Client<OuterReqBody, RespBody>>, InnerReqBody: Send, OuterReqBody: Send + 'static, RespBody: Send,

Build the HTTP client without inserting any extra layers.

This method is provided for advanced users, some features may not work properly without the default layers,

See ClientBuilder::build for more details.

Trait Implementations§

Source§

impl Default for ClientBuilder<Identity, Identity, DefaultMkClient, DefaultLb>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<IL = Identity, OL = Identity, C = DefaultMkClient, LB = LbConfig<WeightedRandomBalance<DiscoverKey>, DnsResolver>> !Freeze for ClientBuilder<IL, OL, C, LB>

§

impl<IL = Identity, OL = Identity, C = DefaultMkClient, LB = LbConfig<WeightedRandomBalance<DiscoverKey>, DnsResolver>> !RefUnwindSafe for ClientBuilder<IL, OL, C, LB>

§

impl<IL, OL, C, LB> Send for ClientBuilder<IL, OL, C, LB>
where IL: Send, OL: Send, C: Send, LB: Send,

§

impl<IL, OL, C, LB> Sync for ClientBuilder<IL, OL, C, LB>
where IL: Sync, OL: Sync, C: Sync, LB: Sync,

§

impl<IL, OL, C, LB> Unpin for ClientBuilder<IL, OL, C, LB>
where IL: Unpin, OL: Unpin, C: Unpin, LB: Unpin,

§

impl<IL = Identity, OL = Identity, C = DefaultMkClient, LB = LbConfig<WeightedRandomBalance<DiscoverKey>, DnsResolver>> !UnwindSafe for ClientBuilder<IL, OL, C, LB>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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