1mod origin;
17pub mod scale;
18mod waiters;
19
20use std::borrow::Borrow;
21use std::future::Future;
22use std::hash::{BuildHasherDefault, Hash, Hasher};
23use std::sync::Arc;
24use std::time::Duration;
25
26use futures_util::future::Either;
27use matier_core::{Body, Error, ErrorKind, Observer, OriginKey, Protocol, Result, Scheme};
28use matier_transport::h1::{H1Config, Release};
29use matier_transport::h2::H2Config;
30use matier_transport::{ConnectConfig, Incoming};
31use rustc_hash::FxHasher;
32
33use origin::{Lease, OriginPool};
34
35#[derive(Clone, Debug)]
37pub struct PoolConfig {
38 pub max_idle_per_origin: usize,
42 pub idle_timeout: Duration,
45 pub max_connection_lifetime: Option<Duration>,
50 pub max_conns_per_origin: usize,
52 pub h2_target_streams_per_conn: usize,
55 pub h2_min_conns_per_origin: usize,
58 pub h2_max_conns_per_origin: usize,
64 pub h2_scale: scale::ScaleThresholds,
66 pub checkout_timeout: Option<Duration>,
69 pub h2_zero_capacity_drain: Duration,
72}
73
74impl Default for PoolConfig {
75 fn default() -> Self {
76 Self {
77 max_idle_per_origin: 256,
78 idle_timeout: Duration::from_secs(90),
79 max_connection_lifetime: None,
80 max_conns_per_origin: 256,
81 h2_target_streams_per_conn: 128,
82 h2_min_conns_per_origin: 1,
83 h2_max_conns_per_origin: scale::default_h2_max_conns(),
84 h2_scale: scale::ScaleThresholds::default(),
85 checkout_timeout: Some(Duration::from_secs(30)),
86 h2_zero_capacity_drain: Duration::from_secs(10),
87 }
88 }
89}
90
91pub(crate) struct Shared {
92 pub(crate) cfg: PoolConfig,
93 pub(crate) connect: ConnectConfig,
94 pub(crate) h1: Arc<H1Config>,
95 pub(crate) h2: H2Config,
96 pub(crate) obs: Arc<dyn Observer>,
97}
98
99#[derive(Clone, Debug, Default, PartialEq, Eq)]
101pub struct OriginStats {
102 pub protocol: Option<Protocol>,
104 pub h1_total: usize,
106 pub h1_idle: usize,
108 pub h1_waiters: usize,
110 pub h2_conns: usize,
112 pub h2_draining: usize,
114}
115
116trait KeyParts {
119 fn parts(&self) -> (Scheme, &str, u16);
120}
121struct Key(OriginKey);
122impl KeyParts for Key {
123 fn parts(&self) -> (Scheme, &str, u16) {
124 (self.0.scheme, &self.0.host, self.0.port)
125 }
126}
127struct Probe<'a>(Scheme, &'a str, u16);
128impl KeyParts for Probe<'_> {
129 fn parts(&self) -> (Scheme, &str, u16) {
130 (self.0, self.1, self.2)
131 }
132}
133impl<'a> Borrow<dyn KeyParts + 'a> for Key {
134 fn borrow(&self) -> &(dyn KeyParts + 'a) {
135 self
136 }
137}
138impl Hash for dyn KeyParts + '_ {
139 fn hash<H: Hasher>(&self, state: &mut H) {
140 let (s, h, p) = self.parts();
141 (s as u8).hash(state);
142 h.hash(state);
143 p.hash(state);
144 }
145}
146impl PartialEq for dyn KeyParts + '_ {
147 fn eq(&self, other: &Self) -> bool {
148 self.parts() == other.parts()
149 }
150}
151impl Eq for dyn KeyParts + '_ {}
152impl Hash for Key {
153 fn hash<H: Hasher>(&self, state: &mut H) {
154 <dyn KeyParts as Hash>::hash(self, state);
155 }
156}
157impl PartialEq for Key {
158 fn eq(&self, other: &Self) -> bool {
159 self.parts() == other.parts()
160 }
161}
162impl Eq for Key {}
163
164struct PoolInner {
165 map: papaya::HashMap<Key, Arc<OriginPool>, BuildHasherDefault<FxHasher>>,
166 shared: Arc<Shared>,
167}
168
169type MapGuard<'g> = papaya::HashMapRef<
170 'g,
171 Key,
172 Arc<OriginPool>,
173 BuildHasherDefault<FxHasher>,
174 papaya::LocalGuard<'g>,
175>;
176
177impl PoolInner {
178 fn lookup<'g>(&self, map: &'g MapGuard<'g>, uri: &http::Uri) -> Option<&'g Arc<OriginPool>> {
180 let (scheme, host) = (uri.scheme_str()?, uri.host()?);
181 let sch = if scheme.eq_ignore_ascii_case("http") {
182 Scheme::Http
183 } else if scheme.eq_ignore_ascii_case("https") {
184 Scheme::Https
185 } else {
186 return None;
187 };
188 if host.bytes().any(|b| b.is_ascii_uppercase()) {
189 return None;
190 }
191 let port = uri.port_u16().unwrap_or_else(|| sch.default_port());
192 let probe = Probe(sch, host, port);
193 map.get(&probe as &dyn KeyParts)
194 }
195
196 fn origin_pool(&self, uri: &http::Uri) -> Result<Arc<OriginPool>> {
197 let map = self.map.pin();
198 if let Some(p) = self.lookup(&map, uri) {
200 return Ok(Arc::clone(p));
201 }
202 let key = Key(OriginKey::from_uri(uri)?);
203 if let Some(p) = map.get(&key as &dyn KeyParts) {
204 return Ok(Arc::clone(p));
205 }
206 let created = OriginPool::new(key.0.clone(), Arc::clone(&self.shared));
207 let p = map.get_or_insert(key, created);
208 Ok(Arc::clone(p))
209 }
210}
211
212#[derive(Clone)]
214pub struct Pool {
215 inner: Arc<PoolInner>,
216}
217
218impl std::fmt::Debug for Pool {
219 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
220 f.debug_struct("Pool").finish_non_exhaustive()
221 }
222}
223
224impl Pool {
225 #[must_use]
227 pub fn new(
228 cfg: PoolConfig,
229 connect: ConnectConfig,
230 h1: H1Config,
231 h2: H2Config,
232 obs: Arc<dyn Observer>,
233 ) -> Self {
234 Self {
235 inner: Arc::new(PoolInner {
236 map: papaya::HashMap::with_hasher(BuildHasherDefault::default()),
237 shared: Arc::new(Shared {
238 cfg,
239 connect,
240 h1: Arc::new(h1),
241 h2,
242 obs,
243 }),
244 }),
245 }
246 }
247
248 pub fn send(
260 &self,
261 req: http::Request<Body>,
262 ) -> impl Future<Output = Result<http::Response<Incoming>>> + Send + use<> {
263 {
266 let map = self.inner.map.pin();
267 if let Some(op) = self.inner.lookup(&map, req.uri())
268 && op.is_h2()
269 {
270 let snap = op.h2_snapshot();
271 if let Some((handle, slot)) = op.h2_pick_in(&snap) {
272 return Either::Right(Either::Left(handle.send(slot, req)));
273 }
274 }
275 }
276 let op = match self.inner.origin_pool(req.uri()) {
277 Ok(op) => op,
278 Err(e) => return Either::Left(std::future::ready(Err(e))),
279 };
280 match op.try_fast(false) {
283 Some(Lease::H2(handle, slot)) => Either::Right(Either::Left(handle.send(slot, req))),
284 lease => Either::Right(Either::Right(send_on(op, req, lease))),
285 }
286 }
287
288 #[must_use]
290 pub fn stats(&self, uri: &http::Uri) -> Option<OriginStats> {
291 let p = self.inner.origin_pool(uri).ok()?;
292 Some(p.stats())
293 }
294}
295
296#[allow(
297 clippy::manual_async_fn,
298 reason = "async fn would store `req` twice (future size budget)"
299)]
300fn send_on(
303 op: Arc<OriginPool>,
304 mut req: http::Request<Body>,
305 mut lease: Option<Lease>,
306) -> impl Future<Output = Result<http::Response<Incoming>>> + Send {
307 async move {
308 let replayable = !matches!(req.body(), Body::Stream(_));
309 let idempotent = matier_transport::h1::is_idempotent(req.method());
310 let mut fresh = false;
311 let mut retried = false;
312 loop {
313 let l = match lease.take() {
314 Some(l) => l,
315 None => match op.try_fast(fresh) {
316 Some(l) => l,
317 None => op.acquire_slow(fresh).await?,
318 },
319 };
320 let result = match l {
321 Lease::H1(conn) => {
322 conn.send_mut(&mut req, Arc::clone(&op) as Arc<dyn Release>)
323 .await
324 }
325 Lease::H2(handle, slot) => {
327 return handle.send(slot, req).await;
328 }
329 };
330 match result {
331 Ok(resp) => return Ok(resp),
332 Err(e) if !retried && replayable => {
333 let stale = matier_transport::h1::is_stale_keepalive(&e);
334 let unprocessed = e.recovery() == matier_core::Recovery::Unprocessed;
335 if unprocessed || (stale && idempotent) {
336 retried = true;
337 fresh = stale;
338 continue;
339 }
340 return Err(e);
341 }
342 Err(e) => return Err(e),
343 }
344 }
345 }
346}
347
348pub(crate) fn dial_error(kind: ErrorKind, msg: String) -> Error {
349 Error::with_source(kind, matier_core::Recovery::Unprocessed, msg)
350}