pub struct Shutdown { /* private fields */ }
Expand description

The Shutdown struct is the main entry point to the shutdown system.

It is created by calling Shutdown::new, which takes a Future that will be awaited on when shutdown is requested. Most users will want to create a Shutdown with Shutdown::default, which uses the default signal handler to trigger shutdown. See default_signal for more info.

See the README for more info on how to use this crate.

Implementations§

source§

impl Shutdown

source

pub fn new(signal: impl Future<Output = ()> + Send + 'static) -> Self

Creates a new Shutdown struct with the given Future.

The Future will be awaited on when shutdown is requested.

source

pub fn guard(&self) -> ShutdownGuard

Returns a ShutdownGuard which primary use is to prevent the Shutdown from shutting down.

The creation of a ShutdownGuard is lockfree.

source

pub fn guard_weak(&self) -> WeakShutdownGuard

Returns a WeakShutdownGuard which in contrast to ShutdownGuard does not prevent the Shutdown from shutting down.

Instead it is used to wait for “shutdown signal” to be triggered or to create a ShutdownGuard which prevents the Shutdown once and only once it is needed.

The creation of a WeakShutdownGuard is lockfree.

source

pub fn spawn_task<T>(&self, task: T) -> JoinHandle<T::Output>where T: Future + Send + 'static, T::Output: Send + 'static,

Returns a Tokio JoinHandle that can be awaited on to wait for the spawned task to complete. See tokio::spawn for more information.

source

pub fn spawn_task_fn<T, F>(&self, task: F) -> JoinHandle<T::Output>where T: Future + Send + 'static, T::Output: Send + 'static, F: FnOnce(ShutdownGuard) -> T + Send + 'static,

Returns a Tokio JoinHandle that can be awaited on to wait for the spawned task (fn) to complete. See tokio::spawn for more information.

source

pub async fn shutdown(self) -> Duration

Returns a future that completes once the Shutdown has been triggered and all ShutdownGuards have been dropped.

The resolved Duration is the time it took for the Shutdown to to wait for all ShutdownGuards to be dropped.

You can use Shutdown::shutdown_with_limit to limit the time the Shutdown waits for all ShutdownGuards to be dropped.

Panics

This method can panic if the internal mutex is poisoned.

source

pub async fn shutdown_with_limit( self, limit: Duration ) -> Result<Duration, TimeoutError>

Returns a future that completes once the Shutdown has been triggered and all ShutdownGuards have been dropped or the given Duration has elapsed.

The resolved Duration is the time it took for the Shutdown to to wait for all ShutdownGuards to be dropped.

You can use Shutdown::shutdown to wait for all ShutdownGuards to be dropped without a time limit.

Panics

This method can panic if the internal mutex is poisoned.

Trait Implementations§

source§

impl Default for Shutdown

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more