pub struct Shutdown(_);
Expand description

Future for graceful shutdown.

This is a Future which doesn’t complete until a shutdown has been initiated (for example with Shutdown::shutdown) and any pending tasks have been completed.

It also contains associated functions for keeping track of tasks that need to be completed before shutdown and an initiated function to create a future that only waits until shutdown has been initiated.

Implementations

Createa a new Shutdown.

Add an early termination condition.

After shutdown has been initiated, the terminator future will be run, and if it completes before all tasks are completed the shutdown future will complete, thus finishing the shutdown even if there are outstanding tasks. This can be useful for using a timeout or signal (or combination) to force a full shutdown even if one or more tasks are taking longer than expected to finish.

The returned future has a boolean output that is true if the future was terminated early due to the termination condition, and false if all remaining tasks completed normally.

Note that terminator will not start to be polled until after shutdown has been initiated. However, you may need to delay creation of the actual timeout future until the first poll, so that the end time is set correctly. This can be done using something like

let shutdown = Shutdown::new().with_terminator(async {
    tokio::time::sleep(Duration::from_secs(30)).await;
});

Initiate shutdown.

This signals that a graceful shutdown shold be started. After calling this is_shutting_down will return true for any reference to the same Shutdown instance. And once all pending tasks are completed, the Shutdown future will complete.

Return a new future that waits for f to complete, then initiates shutdown.

Examples
let shutdown = Shutdown::new();
let interrupt = shutdown.shutdown_after(ctrl_c());
async {
    interrupt.await.expect("Unable to listen to signal");
    // peform additional cleanup before shutting down
    cleanup();
    shutdown.await;
}

Return true if shutdown as been initiated.

Return true if shutdown has not yet been initiated.

Return how many tasks are currently pending.

This should not be relied on to be completely accurate in a multi-threaded context. However, it may be useful for reporting approximately how much work still needs to be done before shutting down the program.

Wrap a future so that it prevents the Shutdown future from completing until after this future completes.

Wrap a future so that it is cancelled if shutdown is initiated.

If the future completes the result is returned in a Some. If it is canceled due to shutdown, None is returned.

Examples

Event loop with shutdown:

let shutdown = Shutdown::new();


loop {
    match shutdown.cancel_on_shutdown(getEvent()).await {
        Some(_) => unimplemented!(),
        None => break,
    }
}

shutdown.await;

Future that waits for shutdown to be initiated:

use graceful_shutdown::Shutdown;
let shutdown = Shutdown::new();
let future = shutdown.cancel_on_shutdown(std::future::pending::<()>());

Return an object that prevents the Shutdown future from completing while it is live.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more
The type of value produced on completion.
Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The output that the future will produce on completion.
Which kind of future are we turning this into?
Creates a future from a value. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.