pub struct Pool<S> { /* private fields */ }Expand description
Per-backend HTTP/1.1 connection pool, generic over the connection type.
Holds idle connections keyed by "host:port". When a
backend responds with Connection: keep-alive (or the HTTP/1.1 default),
the stream is returned here and reused for the next request to the same
backend, eliminating the TCP-handshake cost and reducing ephemeral-port
exhaustion under load.
§Thread safety
All methods take &self and are safe to call from multiple threads.
The inner map is protected by a Mutex.
§Example
use std::sync::Arc;
use std::time::Duration;
use rust_web_server::proxy::ConnPool;
use rust_web_server::proxy::ReverseProxy;
let pool = Arc::new(ConnPool::new(16, Duration::from_secs(30)));
let _proxy = ReverseProxy::new(["http://backend:8080"])
.with_pool(Arc::clone(&pool));Implementations§
Source§impl<S> Pool<S>
impl<S> Pool<S>
Sourcepub fn new(max_idle: usize, idle_timeout: Duration) -> Pool<S>
pub fn new(max_idle: usize, idle_timeout: Duration) -> Pool<S>
Create a pool with the given per-backend idle limit and idle timeout.
Sourcepub fn new_default() -> Pool<S>
pub fn new_default() -> Pool<S>
Create a pool with defaults: 8 idle connections per backend, 25-second timeout.
Sourcepub fn acquire(&self, key: &str) -> Option<S>
pub fn acquire(&self, key: &str) -> Option<S>
Try to acquire an idle connection for key = "host:port".
Stale entries (older than idle_timeout) are discarded automatically.
Returns None if no usable connection is available.
Sourcepub fn release(&self, key: &str, stream: S)
pub fn release(&self, key: &str, stream: S)
Return a keep-alive connection to the pool.
If the backend slot is already at max_idle, the stream is dropped
(the TCP connection closes) rather than exceeding the limit.
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Total idle connections across all backends (useful for testing/metrics).
Auto Trait Implementations§
impl<S> !Freeze for Pool<S>
impl<S> RefUnwindSafe for Pool<S>
impl<S> Send for Pool<S>
impl<S> Sync for Pool<S>
impl<S> Unpin for Pool<S>
impl<S> UnsafeUnpin for Pool<S>
impl<S> UnwindSafe for Pool<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
map-response-body only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.