pub struct Swansong { /* private fields */ }Expand description
Implementationsยง
Sourceยงimpl Swansong
impl Swansong
Sourcepub fn shut_down(&self) -> ShutdownCompletion
pub fn shut_down(&self) -> ShutdownCompletion
Initiate graceful shutdown.
This will gracefully stop any associated Interrupts.
This returns a ShutdownCompletion type, which can be blocked on with
ShutdownCompletion::block or awaited as a Future.
Sourcepub fn block_on_shutdown_completion(self)
pub fn block_on_shutdown_completion(self)
Blocks the current thread until graceful shutdown is complete
Do not use this in async contexts. Instead, await the Swansong
Sourcepub fn state(&self) -> ShutdownState
pub fn state(&self) -> ShutdownState
Determine if this Swansong is running, shutting down, or complete.
Sourcepub fn shutting_down(&self) -> ShuttingDown
pub fn shutting_down(&self) -> ShuttingDown
Returns a ShuttingDown future that resolves when shutdown has been
initiated on this Swansong.
This does not initiate shutdown โ it only observes the transition from
ShutdownState::Running to ShutdownState::ShuttingDown. The future
resolves as soon as Swansong::shut_down is called (on this handle or
any parent), or when the last root handle is dropped.
The returned future does not wait for outstanding Guards to drop. To
wait for full shutdown (stopped and all guards dropped), use
Swansong::shut_down or Swansong::into_future.
ShuttingDown can also be used in blocking contexts via
ShuttingDown::block.
Sourcepub fn interrupt<T>(&self, wrapped_type: T) -> Interrupt<T>
pub fn interrupt<T>(&self, wrapped_type: T) -> Interrupt<T>
Wrap any type with an Interrupt, allowing it to cancel or seal on shutdown.
Interrupt<T> has custom implementations of Future,
Stream, [AsyncRead][futures_io::AsyncRead],
[AsyncWrite][futures_io::AsyncWrite], [AsyncBufRead][futures_io::AsyncBufRead], and
Iterator. See further documentation for the behaviors for each of these types at
Interrupt.
Each of the trait implementations will behave normally with a small overhead to check
shutdown state until shutdown has been initiated with Swansong::shut_down. When shutdown
has been initiated, any type inside of an associated Interrupt will be
interrupted/canceled/sealed at an appropriate time.
Sourcepub fn guard(&self) -> Guard
pub fn guard(&self) -> Guard
Returns a new Guard, which forstalls shutdown until it is dropped.
Sourcepub fn guard_count(&self) -> usize
pub fn guard_count(&self) -> usize
The total number of outstanding Guards in this Swansongโs subtree.
For a root or leaf Swansong with no children, this is just the Guards created
via this Swansong (and its clones). For a Swansong with children, it is the
sum of Guards held in this node plus all Guards in all descendant subtrees.
Child Swansong handles themselves are not counted as Guards; only real
Guard instances are.
This performs an O(subtree) walk; prefer not to call it in a hot path.
Sourcepub fn child(&self) -> Swansong
pub fn child(&self) -> Swansong
Create a child Swansong linked to this parent.
The child can be independently shut down without affecting the parent. However, when
the parent is shut down, the child will also be shut down. The parentโs shutdown will
not be considered complete until the child Swansong and any associated
ShutdownCompletions are dropped.
This supports multi-level hierarchies: a child can itself have children, forming a tree. Shutdown propagates downward through the entire tree.
ยงPerformance
Creating a child has O(n) cost where n is the number of live children, due to cleanup of stale internal references. This is suitable for most use cases but may not be ideal for very high-frequency child creation with many concurrent children.
ยงExample: HTTP/3 server
let server = Swansong::new();
// Each connection gets its own child swansong
let connection = server.child();
// Connection can be shut down independently (e.g., on GOAWAY)
connection.shut_down();
// Or the entire server can shut down, which also stops all connections
// server.shut_down().await;Sourcepub fn guarded<T>(&self, wrapped_type: T) -> Guarded<T>
pub fn guarded<T>(&self, wrapped_type: T) -> Guarded<T>
Attach a guard to the provided type, delaying shutdown until it drops.
This function returns a Guarded wrapper type provides transparent implementations of
various traits like Future and Stream, as
well as Derefing to the wrapped type. This is identical to moving a Guard into the
wrapped type, but sometimes itโs easier to compose the guard around a named future than to
move the guard into the future. See Guarded for more information about trait
implementations on Guarded
Trait Implementationsยง
Sourceยงimpl AsRef<Swansong> for HttpContext
impl AsRef<Swansong> for HttpContext
Sourceยงimpl Drop for Swansong
If we are dropping the last Swansong handle to a root node, stop the
associated futures. Dropping the last handle to a child (non-root) node
does not signal shutdown: the parent remains the governing authority, and
the child stays in its current state until the parent propagates stop or
until all Guards in the childโs subset naturally drain.
impl Drop for Swansong
If we are dropping the last Swansong handle to a root node, stop the
associated futures. Dropping the last handle to a child (non-root) node
does not signal shutdown: the parent remains the governing authority, and
the child stays in its current state until the parent propagates stop or
until all Guards in the childโs subset naturally drain.
Sourceยงimpl IntoFuture for Swansong
impl IntoFuture for Swansong
Sourceยงtype IntoFuture = ShutdownCompletion
type IntoFuture = ShutdownCompletion
Sourceยงfn into_future(self) -> <Swansong as IntoFuture>::IntoFuture
fn into_future(self) -> <Swansong as IntoFuture>::IntoFuture
impl Eq for Swansong
Auto Trait Implementationsยง
impl Freeze for Swansong
impl RefUnwindSafe for Swansong
impl Send for Swansong
impl Sync for Swansong
impl Unpin for Swansong
impl UnsafeUnpin for Swansong
impl UnwindSafe for Swansong
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
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Sourceยงfn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.