mod dispatch;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use futures_util::future::Either;
pub use matier_core::{
Body, BodySender, BodyTimeout, BytesView, ConnectionInfo, Error, ErrorKind, NoopObserver,
Observer, OriginKey, Priority, Protocol, Recovery, ResponseTimeout, Result, ScaleReason,
Scheme, Service, UriTemplate,
};
pub use matier_pool::{OriginStats, Pool, PoolConfig};
pub use matier_transport::h1::H1Config;
pub use matier_transport::h2::H2Config;
pub use matier_transport::{ConnectConfig, Incoming, TlsConfig};
#[derive(Clone, Debug)]
pub struct Client {
pool: Pool,
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
impl Client {
#[must_use]
pub fn new() -> Self {
Self::builder()
.build()
.expect("default client configuration is valid")
}
#[must_use]
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
pub fn send(
&self,
req: http::Request<Body>,
) -> impl Future<Output = Result<http::Response<Incoming>>> + Send + use<> {
match dispatch::validate(&req) {
Ok(()) => Either::Left(self.pool.send(req)),
Err(e) => Either::Right(std::future::ready(Err(e))),
}
}
#[must_use]
pub fn pool(&self) -> &Pool {
&self.pool
}
}
impl Service<http::Request<Body>> for Client {
type Out = Result<http::Response<Incoming>>;
fn execute(&self, input: http::Request<Body>) -> impl Future<Output = Self::Out> + Send {
self.send(input)
}
}
pub struct ClientBuilder {
pool: PoolConfig,
connect: ConnectConfig,
h1: H1Config,
h2: H2Config,
observer: Arc<dyn Observer>,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self {
pool: PoolConfig::default(),
connect: ConnectConfig::default(),
h1: H1Config::default(),
h2: H2Config::default(),
observer: Arc::new(NoopObserver),
}
}
}
impl ClientBuilder {
#[must_use]
pub fn pool(mut self, cfg: PoolConfig) -> Self {
self.pool = cfg;
self
}
#[must_use]
pub fn connect(mut self, cfg: ConnectConfig) -> Self {
self.connect = cfg;
self
}
#[must_use]
pub fn h1(mut self, cfg: H1Config) -> Self {
self.h1 = cfg;
self
}
#[must_use]
pub fn h2(mut self, cfg: H2Config) -> Self {
self.h2 = cfg;
self
}
#[must_use]
pub fn tls(mut self, tls: TlsConfig) -> Self {
self.connect.tls = Some(tls);
self
}
#[must_use]
pub fn http2_prior_knowledge(mut self) -> Self {
self.connect.http2_prior_knowledge = true;
self
}
#[must_use]
pub fn connection_info(mut self, on: bool) -> Self {
self.h1.connection_info = on;
self.h2.connection_info = on;
self
}
#[must_use]
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.connect.connect_timeout = timeout;
self
}
#[must_use]
pub fn tcp_nodelay(mut self, on: bool) -> Self {
self.connect.nodelay = on;
self
}
#[must_use]
pub fn tcp_keepalive(mut self, idle: Option<Duration>) -> Self {
self.connect.keepalive = idle;
self
}
#[must_use]
pub fn tcp_send_buffer_size(mut self, bytes: Option<u32>) -> Self {
self.connect.send_buffer_size = bytes;
self
}
#[must_use]
pub fn tcp_recv_buffer_size(mut self, bytes: Option<u32>) -> Self {
self.connect.recv_buffer_size = bytes;
self
}
#[must_use]
pub fn local_address(mut self, addr: Option<std::net::IpAddr>) -> Self {
self.connect.local_address = addr;
self
}
#[must_use]
pub fn http1_only(mut self) -> Self {
self.connect.alpn_h2 = false;
self.connect.http2_prior_knowledge = false;
self
}
#[must_use]
pub fn pool_idle_timeout(mut self, timeout: Duration) -> Self {
self.pool.idle_timeout = timeout;
self
}
#[must_use]
pub fn pool_max_idle_per_origin(mut self, n: usize) -> Self {
self.pool.max_idle_per_origin = n;
self
}
#[must_use]
pub fn http1_max_connections_per_origin(mut self, n: usize) -> Self {
self.pool.max_conns_per_origin = n;
self
}
#[must_use]
pub fn max_connection_lifetime(mut self, lifetime: Option<Duration>) -> Self {
self.pool.max_connection_lifetime = lifetime;
self
}
#[must_use]
pub fn pool_checkout_timeout(mut self, timeout: Option<Duration>) -> Self {
self.pool.checkout_timeout = timeout;
self
}
#[must_use]
pub fn response_head_timeout(mut self, timeout: Option<Duration>) -> Self {
self.h1.response_head_timeout = timeout;
self.h2.response_head_timeout = timeout;
self
}
#[must_use]
pub fn body_idle_timeout(mut self, timeout: Option<Duration>) -> Self {
self.h1.body_idle_timeout = timeout;
self.h2.body_idle_timeout = timeout;
self
}
#[must_use]
pub fn http1_max_response_head_size(mut self, bytes: usize) -> Self {
self.h1.limits.max_head_bytes = bytes;
self
}
#[must_use]
pub fn http1_max_buf_size(mut self, bytes: usize) -> Self {
self.h1.max_buffer = bytes;
self
}
#[must_use]
pub fn http1_allow_obsolete_multiline_headers_in_responses(mut self, on: bool) -> Self {
self.h1.limits.allow_obs_fold = on;
self
}
#[must_use]
pub fn http1_title_case_headers(mut self, on: bool) -> Self {
self.h1.title_case_headers = on;
self
}
#[must_use]
pub fn http2_initial_stream_window_size(mut self, bytes: u32) -> Self {
self.h2.initial_stream_window = bytes;
self
}
#[must_use]
pub fn http2_initial_connection_window_size(mut self, bytes: u32) -> Self {
self.h2.initial_conn_window = bytes;
self
}
#[must_use]
pub fn http2_max_frame_size(mut self, bytes: u32) -> Self {
self.h2.max_frame_size = bytes;
self
}
#[must_use]
pub fn http2_max_header_list_size(mut self, bytes: u32) -> Self {
self.h2.max_header_list_size = bytes;
self
}
#[must_use]
pub fn http2_keep_alive_interval(mut self, interval: Option<Duration>) -> Self {
self.h2.keepalive_interval = interval;
self
}
#[must_use]
pub fn http2_keep_alive_timeout(mut self, timeout: Duration) -> Self {
self.h2.keepalive_timeout = timeout;
self
}
#[must_use]
pub fn http2_max_streams_per_connection(mut self, n: usize) -> Self {
self.pool.h2_target_streams_per_conn = n;
self
}
#[must_use]
pub fn http2_connections_per_origin(mut self, min: usize, max: usize) -> Self {
self.pool.h2_min_conns_per_origin = min;
self.pool.h2_max_conns_per_origin = max;
self
}
#[must_use]
pub fn http2_request_coalescing(mut self, on: bool) -> Self {
self.h2.coalesce_requests = on;
self
}
#[must_use]
pub fn http2_fifo_send(mut self, on: bool) -> Self {
self.h2.fifo_send = on;
self
}
#[must_use]
pub fn observer(mut self, obs: Arc<dyn Observer>) -> Self {
self.observer = obs;
self
}
pub fn build(self) -> Result<Client> {
self.h2.validate()?;
if self.pool.max_conns_per_origin == 0 {
return Err(Error::invalid_request("max_conns_per_origin must be > 0"));
}
if self.pool.h2_max_conns_per_origin == 0 {
return Err(Error::invalid_request(
"h2_max_conns_per_origin must be > 0",
));
}
let mut connect = self.connect;
if connect.tls.is_none() {
connect.tls = TlsConfig::with_platform_verifier().ok();
}
Ok(Client {
pool: Pool::new(self.pool, connect, self.h1, self.h2, self.observer),
})
}
}