use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::time::Duration;
use http::Uri;
use hyper_util::client::legacy::connect::Connection;
use hyper_util::rt::TokioExecutor;
use tower_service::Service;
use super::connect_timeout::ConnectTimeout;
use super::lifecycle::{LifecycleConfig, LifecycleConnector};
use super::HyperRequestBody;
use crate::http_version::HttpVersionPolicyEnabler;
pub(crate) const SNI_CLIENT_CACHE_MAX_ENTRIES: usize = 256;
#[cfg(feature = "proxy")]
pub(crate) const SOCKS_CLIENT_CACHE_MAX_ENTRIES: usize = 64;
#[cfg(feature = "proxy")]
pub(crate) const FORWARD_CLIENT_CACHE_MAX_ENTRIES: usize = 64;
#[cfg(feature = "proxy")]
pub(crate) const CONNECT_CLIENT_CACHE_MAX_ENTRIES: usize = 64;
pub(crate) const RESOLVED_CLIENT_CACHE_MAX_ENTRIES: usize = 64;
#[derive(Debug, Clone, Copy)]
pub(crate) struct HyperClientPolicy {
retry_canceled_requests: bool,
idle_timeout: Option<Duration>,
max_idle_per_host: Option<usize>,
http2_only: bool,
}
impl HyperClientPolicy {
fn http2_only(enabler: HttpVersionPolicyEnabler) -> bool {
!enabler.enable_http1() && enabler.enable_http2()
}
pub(crate) fn new(
retry_canceled_requests: bool,
idle_timeout: Option<Duration>,
max_idle_per_host: Option<usize>,
enabler: HttpVersionPolicyEnabler,
) -> Self {
Self {
retry_canceled_requests,
idle_timeout,
max_idle_per_host,
http2_only: Self::http2_only(enabler),
}
}
#[cfg(feature = "proxy")]
pub(crate) fn forward_route(
retry_canceled_requests: bool,
idle_timeout: Option<Duration>,
max_idle_per_host: Option<usize>,
) -> Self {
Self {
retry_canceled_requests,
idle_timeout,
max_idle_per_host,
http2_only: false,
}
}
pub(crate) fn apply(&self, builder: &mut hyper_util::client::legacy::Builder) {
builder.retry_canceled_requests(self.retry_canceled_requests);
#[cfg(feature = "transport-http2")]
if self.http2_only {
builder.http2_only(true);
}
#[cfg(not(feature = "transport-http2"))]
let _ = self.http2_only;
if let Some(idle_timeout) = self.idle_timeout {
builder.pool_timer(hyper_util::rt::TokioTimer::new());
builder.pool_idle_timeout(idle_timeout);
}
if let Some(max_idle_per_host) = self.max_idle_per_host {
builder.pool_max_idle_per_host(max_idle_per_host);
}
}
}
pub(crate) fn build_hyper_client<C>(
connector: C,
policy: &HyperClientPolicy,
connect_timeout: Option<Duration>,
lifecycle: &Arc<LifecycleConfig>,
) -> hyper_util::client::legacy::Client<LifecycleConnector<ConnectTimeout<C>>, HyperRequestBody>
where
C: Service<Uri> + Clone + Send + Sync + 'static,
C::Response: hyper::rt::Read + hyper::rt::Write + Connection + Unpin + Send + 'static,
C::Error: Into<Box<dyn std::error::Error + Send + Sync>> + 'static,
C::Future: Send + 'static,
{
let connector = ConnectTimeout::new(connector, connect_timeout);
let connector = LifecycleConnector::new(connector, lifecycle.clone());
let mut builder = hyper_util::client::legacy::Client::builder(TokioExecutor::new());
policy.apply(&mut builder);
builder.build(connector)
}
#[derive(Debug)]
pub(crate) struct BoundedClientCache<K, V> {
entries: HashMap<K, V>,
capacity: usize,
}
impl<K, V> BoundedClientCache<K, V> {
pub(crate) fn new(capacity: usize) -> Self {
Self {
entries: HashMap::new(),
capacity,
}
}
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
self.entries.len()
}
#[cfg(test)]
pub(crate) fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
impl<K, V> BoundedClientCache<K, V>
where
K: Eq + Hash,
{
pub(crate) fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: std::borrow::Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.entries.get(key)
}
}
impl<K, V> BoundedClientCache<K, V>
where
K: Eq + Hash + Clone,
{
pub(crate) fn insert(&mut self, key: K, value: V) {
if self.entries.len() >= self.capacity && !self.entries.contains_key(&key) {
if let Some(victim) = self.entries.keys().next().cloned() {
self.entries.remove(&victim);
}
}
self.entries.insert(key, value);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::HttpVersionPolicy;
#[test]
fn bounded_cache_returns_hits_without_growth() {
let mut cache = BoundedClientCache::new(2);
cache.insert("a".to_owned(), 1_u32);
assert_eq!(cache.get("a"), Some(&1));
assert_eq!(cache.len(), 1);
cache.insert("a".to_owned(), 2_u32);
assert_eq!(cache.get("a"), Some(&2));
assert_eq!(cache.len(), 1);
}
#[test]
fn bounded_cache_evicts_arbitrary_entry_at_capacity() {
let mut cache = BoundedClientCache::new(2);
cache.insert("a".to_owned(), 1_u32);
cache.insert("b".to_owned(), 2_u32);
cache.insert("c".to_owned(), 3_u32);
assert_eq!(cache.len(), 2);
assert_eq!(cache.get("c"), Some(&3));
assert_eq!(
usize::from(cache.get("a").is_some()) + usize::from(cache.get("b").is_some()),
1
);
}
#[test]
fn bounded_cache_miss_returns_none() {
let cache: BoundedClientCache<String, u32> = BoundedClientCache::new(64);
assert!(cache.is_empty());
assert_eq!(cache.get("missing"), None);
}
#[test]
fn policy_http2_only_matches_h2_only_client() {
let h2_only = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http2Only);
let auto =
HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto { allow_http3: false });
let h1_only = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http1Only);
#[cfg(feature = "transport-http2")]
assert!(HyperClientPolicy::new(true, None, None, h2_only).http2_only);
#[cfg(not(feature = "transport-http2"))]
assert!(!HyperClientPolicy::new(true, None, None, h2_only).http2_only);
assert!(!HyperClientPolicy::new(true, None, None, auto).http2_only);
assert!(!HyperClientPolicy::new(true, None, None, h1_only).http2_only);
}
#[cfg(feature = "proxy")]
#[test]
fn forward_route_never_selects_http2_only() {
let h2_only = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http2Only);
let _ = h2_only;
assert!(!HyperClientPolicy::forward_route(true, None, None).http2_only);
}
#[test]
fn shared_policy_carries_resolved_idle_policy() {
use std::time::Duration;
let enabler =
HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto { allow_http3: false });
let policy =
HyperClientPolicy::new(true, Some(Duration::from_millis(100)), Some(3), enabler);
assert_eq!(policy.idle_timeout, Some(Duration::from_millis(100)));
assert_eq!(policy.max_idle_per_host, Some(3));
}
#[cfg(feature = "proxy")]
#[test]
fn forward_route_carries_resolved_idle_policy() {
use std::time::Duration;
let policy =
HyperClientPolicy::forward_route(true, Some(Duration::from_millis(100)), Some(3));
assert_eq!(policy.idle_timeout, Some(Duration::from_millis(100)));
assert_eq!(policy.max_idle_per_host, Some(3));
assert!(!policy.http2_only);
}
}