Skip to main content

Swansong

Struct Swansong 

Source
pub struct Swansong { /* private fields */ }
Expand description

ยง๐Ÿฆข Shutdown manager

See crate level docs for overview and example.

Two Swansongs are Eq if and only if they are clones of each other.

Implementationsยง

Sourceยง

impl Swansong

Source

pub fn new() -> Swansong

Construct a new Swansong.

Source

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.

Source

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

Source

pub fn state(&self) -> ShutdownState

Determine if this Swansong is running, shutting down, or complete.

Source

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.

Source

pub fn guard(&self) -> Guard

Returns a new Guard, which forstalls shutdown until it is dropped.

Source

pub fn guard_count(&self) -> usize

The current number of outstanding Guards.

Source

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;
Source

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 ServerConfig

Sourceยง

fn as_ref(&self) -> &Swansong

Converts this type into a shared reference of the (usually inferred) input type.
Sourceยง

impl Clone for Swansong

Sourceยง

fn clone(&self) -> Swansong

Returns a duplicate of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl Debug for Swansong

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl Default for Swansong

Sourceยง

fn default() -> Swansong

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl Drop for Swansong

If we are dropping the only Swansong, stop the associated futures

Sourceยง

fn drop(&mut self)

Executes the destructor for this type. Read more
Sourceยง

impl IntoFuture for Swansong

Sourceยง

type Output = ()

The output that the future will produce on completion.
Sourceยง

type IntoFuture = ShutdownCompletion

Which kind of future are we turning this into?
Sourceยง

fn into_future(self) -> <Swansong as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Sourceยง

impl PartialEq for Swansong

Sourceยง

fn eq(&self, other: &Swansong) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl Eq for Swansong

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.