pub struct Pooled<M: Manage, X: Exchange<M>> { /* private fields */ }Expand description
A connection checked out of the pool.
Derefs to the underlying connection. Returns itself to the pool on drop — unless it has been poisoned, in which case it is closed instead.
§Cancellation safety
This is the part that differs most from a readiness-based pool. With
completion-based IO, dropping a future with an operation in flight does not
unwind the operation: the kernel still owns the buffer and the peer’s
response is still coming. compio keeps the buffer sound, but your
protocol is now out of step — the next reader on this connection will see
the tail of someone else’s response.
A connection cancelled mid-operation must therefore be destroyed, never
reused. Wrap each operation in begin_op:
let op = conn.begin_op();
let n = conn.read(&mut buf).await?; // if this await is cancelled…
op.complete_op(); // …this never runs, and the conn is droppedIf you never cancel (no select!, no timeouts, no early return between
submit and completion) you can skip it. Everyone believes that about their
code right up until they add a timeout.
Implementations§
Source§impl<M: Manage, X: Exchange<M>> Pooled<M, X>
impl<M: Manage, X: Exchange<M>> Pooled<M, X>
Sourcepub fn poison(&self)
pub fn poison(&self)
Marks this connection unusable. It is closed on drop instead of pooled.
Call this whenever you learn the connection is in an unknown state: a protocol error, a partial write, a response you could not parse.
Sourcepub fn is_poisoned(&self) -> bool
pub fn is_poisoned(&self) -> bool
Sourcepub fn begin_op(&self) -> OpGuard
pub fn begin_op(&self) -> OpGuard
Arms cancellation protection for one operation.
The returned guard poisons this connection when dropped, unless
[OpGuard::complete] runs first. It borrows nothing from self, so the
connection stays fully usable while the guard is alive.
Sourcepub fn take(self) -> M::Connection
pub fn take(self) -> M::Connection
Removes the connection from the pool’s control and hands it to you.
The shard’s budget is freed immediately, so a replacement may be opened.