Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl ConnectionPool for SingleTlsConnectionPool {
fn release(&mut self, conn: Option<Connection<Self::Stream>>) {
self.has_active_connection = false;
if let Some(conn) = conn {
if !conn.disconnected {
if conn.is_reusable() {
let _ = self.conn.insert(conn);
}
}
Expand Down Expand Up @@ -346,6 +346,30 @@ impl<C: ConnectionPool<CHUNK_SIZE>, const CHUNK_SIZE: usize> HttpRequest<C, CHUN
}
}

/// Marks the connection as invalidated, ensuring it will be discarded
/// when this request is dropped rather than returned to the pool.
#[inline]
pub fn invalidate(&mut self) {
if let Some(conn) = self.conn.as_mut() {
conn.invalidated = true;
}
}

/// Returns `true` if the connection has been explicitly invalidated.
#[inline]
pub fn is_invalidated(&self) -> bool {
self.conn.as_ref().is_some_and(|c| c.invalidated)
}

/// Returns `true` if the connection can be reused by the pool.
///
/// A connection is reusable if it is neither disconnected (IO error)
/// nor invalidated (user-requested).
#[inline]
pub fn is_reusable(&self) -> bool {
self.conn.as_ref().is_some_and(|c| c.is_reusable())
}

/// Read from the stream and return when complete. Must provide buffer that will hold the response.
/// It's ok to re-use the buffer as long as it's been cleared before using it with a new request.
///
Expand Down Expand Up @@ -464,6 +488,7 @@ pub struct Connection<S, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> {
stream: S,
buffer: Vec<u8>,
disconnected: bool,
invalidated: bool,
header_finder: Finder,
}

Expand Down Expand Up @@ -526,6 +551,7 @@ impl<S, const CHUNK_SIZE: usize> Connection<S, CHUNK_SIZE> {
stream,
buffer: Vec::with_capacity(CHUNK_SIZE),
disconnected: false,
invalidated: false,
header_finder: Finder::new(b"\r\n\r\n"),
}
}
Expand All @@ -535,6 +561,21 @@ impl<S, const CHUNK_SIZE: usize> Connection<S, CHUNK_SIZE> {
pub const fn is_disconnected(&self) -> bool {
self.disconnected
}

/// Returns whether the connection has been explicitly invalidated by the user.
#[inline]
pub const fn is_invalidated(&self) -> bool {
self.invalidated
}

/// Returns whether this connection can be reused by the pool.
///
/// A connection is reusable if it is neither disconnected (IO error)
/// nor invalidated (user-requested).
#[inline]
pub const fn is_reusable(&self) -> bool {
!self.disconnected && !self.invalidated
}
}

#[cfg(test)]
Expand Down
Loading