mod origin;
pub mod scale;
mod waiters;
use std::borrow::Borrow;
use std::future::Future;
use std::hash::{BuildHasherDefault, Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use futures_util::future::Either;
use matier_core::{Body, Error, ErrorKind, Observer, OriginKey, Protocol, Result, Scheme};
use matier_transport::h1::{H1Config, Release};
use matier_transport::h2::H2Config;
use matier_transport::{ConnectConfig, Incoming};
use rustc_hash::FxHasher;
use origin::{Lease, OriginPool};
#[derive(Clone, Debug)]
pub struct PoolConfig {
pub max_idle_per_origin: usize,
pub idle_timeout: Duration,
pub max_connection_lifetime: Option<Duration>,
pub max_conns_per_origin: usize,
pub h2_target_streams_per_conn: usize,
pub h2_min_conns_per_origin: usize,
pub h2_max_conns_per_origin: usize,
pub h2_scale: scale::ScaleThresholds,
pub checkout_timeout: Option<Duration>,
pub h2_zero_capacity_drain: Duration,
}
impl Default for PoolConfig {
fn default() -> Self {
Self {
max_idle_per_origin: 256,
idle_timeout: Duration::from_secs(90),
max_connection_lifetime: None,
max_conns_per_origin: 256,
h2_target_streams_per_conn: 128,
h2_min_conns_per_origin: 1,
h2_max_conns_per_origin: scale::default_h2_max_conns(),
h2_scale: scale::ScaleThresholds::default(),
checkout_timeout: Some(Duration::from_secs(30)),
h2_zero_capacity_drain: Duration::from_secs(10),
}
}
}
pub(crate) struct Shared {
pub(crate) cfg: PoolConfig,
pub(crate) connect: ConnectConfig,
pub(crate) h1: Arc<H1Config>,
pub(crate) h2: H2Config,
pub(crate) obs: Arc<dyn Observer>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct OriginStats {
pub protocol: Option<Protocol>,
pub h1_total: usize,
pub h1_idle: usize,
pub h1_waiters: usize,
pub h2_conns: usize,
pub h2_draining: usize,
}
trait KeyParts {
fn parts(&self) -> (Scheme, &str, u16);
}
struct Key(OriginKey);
impl KeyParts for Key {
fn parts(&self) -> (Scheme, &str, u16) {
(self.0.scheme, &self.0.host, self.0.port)
}
}
struct Probe<'a>(Scheme, &'a str, u16);
impl KeyParts for Probe<'_> {
fn parts(&self) -> (Scheme, &str, u16) {
(self.0, self.1, self.2)
}
}
impl<'a> Borrow<dyn KeyParts + 'a> for Key {
fn borrow(&self) -> &(dyn KeyParts + 'a) {
self
}
}
impl Hash for dyn KeyParts + '_ {
fn hash<H: Hasher>(&self, state: &mut H) {
let (s, h, p) = self.parts();
(s as u8).hash(state);
h.hash(state);
p.hash(state);
}
}
impl PartialEq for dyn KeyParts + '_ {
fn eq(&self, other: &Self) -> bool {
self.parts() == other.parts()
}
}
impl Eq for dyn KeyParts + '_ {}
impl Hash for Key {
fn hash<H: Hasher>(&self, state: &mut H) {
<dyn KeyParts as Hash>::hash(self, state);
}
}
impl PartialEq for Key {
fn eq(&self, other: &Self) -> bool {
self.parts() == other.parts()
}
}
impl Eq for Key {}
struct PoolInner {
map: papaya::HashMap<Key, Arc<OriginPool>, BuildHasherDefault<FxHasher>>,
shared: Arc<Shared>,
}
type MapGuard<'g> = papaya::HashMapRef<
'g,
Key,
Arc<OriginPool>,
BuildHasherDefault<FxHasher>,
papaya::LocalGuard<'g>,
>;
impl PoolInner {
fn lookup<'g>(&self, map: &'g MapGuard<'g>, uri: &http::Uri) -> Option<&'g Arc<OriginPool>> {
let (scheme, host) = (uri.scheme_str()?, uri.host()?);
let sch = if scheme.eq_ignore_ascii_case("http") {
Scheme::Http
} else if scheme.eq_ignore_ascii_case("https") {
Scheme::Https
} else {
return None;
};
if host.bytes().any(|b| b.is_ascii_uppercase()) {
return None;
}
let port = uri.port_u16().unwrap_or_else(|| sch.default_port());
let probe = Probe(sch, host, port);
map.get(&probe as &dyn KeyParts)
}
fn origin_pool(&self, uri: &http::Uri) -> Result<Arc<OriginPool>> {
let map = self.map.pin();
if let Some(p) = self.lookup(&map, uri) {
return Ok(Arc::clone(p));
}
let key = Key(OriginKey::from_uri(uri)?);
if let Some(p) = map.get(&key as &dyn KeyParts) {
return Ok(Arc::clone(p));
}
let created = OriginPool::new(key.0.clone(), Arc::clone(&self.shared));
let p = map.get_or_insert(key, created);
Ok(Arc::clone(p))
}
}
#[derive(Clone)]
pub struct Pool {
inner: Arc<PoolInner>,
}
impl std::fmt::Debug for Pool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pool").finish_non_exhaustive()
}
}
impl Pool {
#[must_use]
pub fn new(
cfg: PoolConfig,
connect: ConnectConfig,
h1: H1Config,
h2: H2Config,
obs: Arc<dyn Observer>,
) -> Self {
Self {
inner: Arc::new(PoolInner {
map: papaya::HashMap::with_hasher(BuildHasherDefault::default()),
shared: Arc::new(Shared {
cfg,
connect,
h1: Arc::new(h1),
h2,
obs,
}),
}),
}
}
pub fn send(
&self,
req: http::Request<Body>,
) -> impl Future<Output = Result<http::Response<Incoming>>> + Send + use<> {
{
let map = self.inner.map.pin();
if let Some(op) = self.inner.lookup(&map, req.uri())
&& op.is_h2()
{
let snap = op.h2_snapshot();
if let Some((handle, slot)) = op.h2_pick_in(&snap) {
return Either::Right(Either::Left(handle.send(slot, req)));
}
}
}
let op = match self.inner.origin_pool(req.uri()) {
Ok(op) => op,
Err(e) => return Either::Left(std::future::ready(Err(e))),
};
match op.try_fast(false) {
Some(Lease::H2(handle, slot)) => Either::Right(Either::Left(handle.send(slot, req))),
lease => Either::Right(Either::Right(send_on(op, req, lease))),
}
}
#[must_use]
pub fn stats(&self, uri: &http::Uri) -> Option<OriginStats> {
let p = self.inner.origin_pool(uri).ok()?;
Some(p.stats())
}
}
#[allow(
clippy::manual_async_fn,
reason = "async fn would store `req` twice (future size budget)"
)]
fn send_on(
op: Arc<OriginPool>,
mut req: http::Request<Body>,
mut lease: Option<Lease>,
) -> impl Future<Output = Result<http::Response<Incoming>>> + Send {
async move {
let replayable = !matches!(req.body(), Body::Stream(_));
let idempotent = matier_transport::h1::is_idempotent(req.method());
let mut fresh = false;
let mut retried = false;
loop {
let l = match lease.take() {
Some(l) => l,
None => match op.try_fast(fresh) {
Some(l) => l,
None => op.acquire_slow(fresh).await?,
},
};
let result = match l {
Lease::H1(conn) => {
conn.send_mut(&mut req, Arc::clone(&op) as Arc<dyn Release>)
.await
}
Lease::H2(handle, slot) => {
return handle.send(slot, req).await;
}
};
match result {
Ok(resp) => return Ok(resp),
Err(e) if !retried && replayable => {
let stale = matier_transport::h1::is_stale_keepalive(&e);
let unprocessed = e.recovery() == matier_core::Recovery::Unprocessed;
if unprocessed || (stale && idempotent) {
retried = true;
fresh = stale;
continue;
}
return Err(e);
}
Err(e) => return Err(e),
}
}
}
}
pub(crate) fn dial_error(kind: ErrorKind, msg: String) -> Error {
Error::with_source(kind, matier_core::Recovery::Unprocessed, msg)
}