use super::*;
impl AsyncRead for AsyncClientTransport {
#[inline(always)]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Tcp(stream) => Pin::new(stream).poll_read(cx, buf),
Self::Tls(stream) => Pin::new(&mut **stream).poll_read(cx, buf),
Self::Proxy(stream) => Pin::new(&mut **stream).poll_read(cx, buf),
}
}
}
impl AsyncWrite for AsyncClientTransport {
#[inline(always)]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
Self::Tcp(stream) => Pin::new(stream).poll_write(cx, buf),
Self::Tls(stream) => Pin::new(&mut **stream).poll_write(cx, buf),
Self::Proxy(stream) => Pin::new(&mut **stream).poll_write(cx, buf),
}
}
#[inline(always)]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Tcp(stream) => Pin::new(stream).poll_flush(cx),
Self::Tls(stream) => Pin::new(&mut **stream).poll_flush(cx),
Self::Proxy(stream) => Pin::new(&mut **stream).poll_flush(cx),
}
}
#[inline(always)]
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Tcp(stream) => Pin::new(stream).poll_shutdown(cx),
Self::Tls(stream) => Pin::new(&mut **stream).poll_shutdown(cx),
Self::Proxy(stream) => Pin::new(&mut **stream).poll_shutdown(cx),
}
}
}
impl Read for SyncClientTransport {
#[inline(always)]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Self::Tcp(stream) => stream.read(buf),
Self::Tls(stream) => stream.read(buf),
Self::Proxy(stream) => stream.read(buf),
}
}
}
impl Write for SyncClientTransport {
#[inline(always)]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self {
Self::Tcp(stream) => stream.write(buf),
Self::Tls(stream) => stream.write(buf),
Self::Proxy(stream) => stream.write(buf),
}
}
#[inline(always)]
fn flush(&mut self) -> io::Result<()> {
match self {
Self::Tcp(stream) => stream.flush(),
Self::Tls(stream) => stream.flush(),
Self::Proxy(stream) => stream.flush(),
}
}
}
impl SyncClientTransport {
#[inline(always)]
pub(crate) fn set_read_timeout(&self, timeout: Option<Duration>) {
match self {
Self::Tcp(stream) => {
let _: Result<(), io::Error> = stream.set_read_timeout(timeout);
}
Self::Tls(stream) => {
let _: Result<(), io::Error> = stream.get_ref().set_read_timeout(timeout);
}
Self::Proxy(_) => {}
}
}
}
impl<T> H1PoolStripe<T> {
#[inline(always)]
pub(crate) fn new() -> Self {
Self {
entries: Vec::new(),
total: 0,
}
}
pub(crate) fn evict_expired(&mut self, now: Instant, ttl: Duration) {
let Self { entries, total } = self;
let mut remaining: usize = 0;
entries.retain_mut(|(_, queue): &mut (PoolKey, VecDeque<PooledConn<T>>)| {
queue.retain(|item: &PooledConn<T>| {
now.saturating_duration_since(item.idle_since) < ttl
});
remaining += queue.len();
!queue.is_empty()
});
*total = remaining;
}
}
impl<T> StripedH1Pool<T> {
pub(crate) fn new() -> Self {
Self {
stripes: array::from_fn(|_: usize| Mutex::new(H1PoolStripe::new())),
non_empty_mask: AtomicU64::new(0),
}
}
#[inline(always)]
fn take_from_stripe(&self, stripe_idx: usize, lookup: PoolLookup<'_>) -> Option<T> {
let Self {
stripes,
non_empty_mask,
} = self;
let Ok(mut stripe) = stripes[stripe_idx].lock() else {
return None;
};
if stripe.total == 0 {
non_empty_mask.fetch_and(!(1u64 << stripe_idx), Ordering::Release);
return None;
}
let pos: usize = stripe
.entries
.iter()
.position(|(k, _): &H1StripeEntry<T>| {
pool_key_matches(
k,
lookup.is_https,
lookup.host,
lookup.port,
lookup.proxy,
lookup.root_fp,
lookup.sync_timeout_ms,
)
})?;
let ttl: Duration = Duration::from_secs(POOL_IDLE_TTL_SECS);
let now: Instant = Instant::now();
let mut found: Option<T> = None;
while let Some(conn) = stripe.entries[pos].1.pop_back() {
stripe.total = stripe.total.saturating_sub(1);
if now.saturating_duration_since(conn.idle_since) < ttl {
found = Some(conn.stream);
break;
}
}
if stripe.entries[pos].1.is_empty() {
let _: H1StripeEntry<T> = stripe.entries.swap_remove(pos);
}
if stripe.total == 0 {
non_empty_mask.fetch_and(!(1u64 << stripe_idx), Ordering::Release);
}
found
}
#[inline(always)]
pub(crate) fn take(&self, lookup: PoolLookup<'_>) -> Option<T> {
let Self { non_empty_mask, .. } = self;
let mask: u64 = non_empty_mask.load(Ordering::Acquire);
if mask == 0 {
return None;
}
let pref: usize = WORKER_H1_STRIPE.with(|idx: &usize| *idx);
let pref_bit: u64 = 1u64 << pref;
if (mask & pref_bit) != 0
&& let Some(conn) = self.take_from_stripe(pref, lookup)
{
return Some(conn);
}
let mut remaining: u64 = mask & !pref_bit;
while remaining != 0 {
let idx: usize = remaining.trailing_zeros() as usize;
remaining &= remaining - 1;
if let Some(conn) = self.take_from_stripe(idx, lookup) {
return Some(conn);
}
}
None
}
#[inline(always)]
pub(crate) fn put(&self, lookup: PoolLookup<'_>, stream: T) {
let Self {
stripes,
non_empty_mask,
} = self;
let pref: usize = WORKER_H1_STRIPE.with(|idx: &usize| *idx);
let mut chosen_idx: usize = pref;
let mut locked: Option<MutexGuard<'_, H1PoolStripe<T>>> = None;
for probe in 0..H1_POOL_PROBE_ATTEMPTS {
let idx: usize = (pref + probe) & (H1_POOL_STRIPES - 1);
if let Ok(guard) = stripes[idx].try_lock() {
chosen_idx = idx;
locked = Some(guard);
break;
}
}
let mut stripe: MutexGuard<'_, H1PoolStripe<T>> = match locked {
Some(guard) => guard,
None => match stripes[pref].lock() {
Ok(guard) => guard,
Err(_) => return,
},
};
let ttl: Duration = Duration::from_secs(POOL_IDLE_TTL_SECS);
let now: Instant = Instant::now();
if stripe.total >= POOL_MAX_IDLE_PER_STRIPE {
stripe.evict_expired(now, ttl);
if stripe.total >= POOL_MAX_IDLE_PER_STRIPE {
return;
}
}
let H1PoolStripe { entries, total } = &mut *stripe;
let queue_idx: usize = if let Some(pos) =
entries.iter().position(|(k, _): &H1StripeEntry<T>| {
pool_key_matches(
k,
lookup.is_https,
lookup.host,
lookup.port,
lookup.proxy,
lookup.root_fp,
lookup.sync_timeout_ms,
)
}) {
pos
} else {
let key: PoolKey = make_pool_key(
lookup.is_https,
lookup.host,
lookup.port,
lookup.proxy,
lookup.roots,
lookup.sync_timeout_ms,
);
entries.push((key, VecDeque::with_capacity(4)));
entries.len() - 1
};
let queue: &mut VecDeque<PooledConn<T>> = &mut entries[queue_idx].1;
if queue.len() >= POOL_MAX_IDLE_PER_STRIPE_HOST {
let before: usize = queue.len();
queue.retain(|item: &PooledConn<T>| {
now.saturating_duration_since(item.idle_since) < ttl
});
let evicted: usize = before - queue.len();
*total = total.saturating_sub(evicted);
if queue.len() >= POOL_MAX_IDLE_PER_STRIPE_HOST {
return;
}
}
queue.push_back(PooledConn {
stream,
idle_since: now,
});
*total += 1;
non_empty_mask.fetch_or(1u64 << chosen_idx, Ordering::Release);
}
#[cfg(test)]
pub(crate) fn contains_port(&self, port: u16) -> bool {
let Self { stripes, .. } = self;
for stripe_lock in stripes {
if let Ok(stripe) = stripe_lock.lock()
&& stripe
.entries
.iter()
.any(|(k, _): &(PoolKey, VecDeque<PooledConn<T>>)| k.port == port)
{
return true;
}
}
false
}
#[cfg(test)]
pub(crate) fn insert_for_test(&self, key: PoolKey, conn: PooledConn<T>) {
let Self {
stripes,
non_empty_mask,
} = self;
if let Ok(mut stripe) = stripes[0].lock() {
let mut queue: VecDeque<PooledConn<T>> = VecDeque::new();
queue.push_back(conn);
stripe.entries.push((key, queue));
stripe.total += 1;
non_empty_mask.fetch_or(1u64, Ordering::Release);
}
}
}