4 releases (2 breaking)

new 0.3.0 Apr 23, 2024
0.2.1 Mar 8, 2023
0.2.0 Jan 4, 2022
0.1.0 Nov 21, 2021

#577 in Asynchronous

Download history 13/week @ 2023-12-27 24/week @ 2024-01-03 29/week @ 2024-01-10 40/week @ 2024-01-17 27/week @ 2024-01-24 37/week @ 2024-01-31 58/week @ 2024-02-07 95/week @ 2024-02-14 103/week @ 2024-02-21 128/week @ 2024-02-28 66/week @ 2024-03-06 85/week @ 2024-03-13 94/week @ 2024-03-20 63/week @ 2024-03-27 53/week @ 2024-04-03 10/week @ 2024-04-10

240 downloads per month

MIT license

34KB
417 lines

Elegant Departure

Crates.io License Build Status docs.rs

Rust crate to simplify graceful async shutdowns:

  • Easy to use with a minimal API
  • Runtime independent (works with tokio, async-std, smol, …)
  • Additional integrations for tokio (shutdown on ctrl-c, signals etc.)

Usage

This crate is on crates.io and can be used by adding it to your dependencies in your project's Cargo.toml.

[dependencies]
elegant-departure = "0.3"

For a optional tokio integration, you need to enable the tokio feature:

[dependencies]
elegant-departure = { version = "0.3", features = "tokio" }

Example

Examples can be found in the example directory:

  • Simple: simple example without tokio integration
  • Axum: Axum integration example
  • Tokio: Tokio integration example
  • Hyper: a shutdown example using the Hyper webserver
  • Worker: example implementation of a worker using select!
  • Smol: example using the smol runtime
  • Async Std: example using the async_std runtime

Minimal example using the tokio integration:

use std::time::Duration;

async fn worker(name: &'static str) {
    let guard = elegant_departure::get_shutdown_guard();

    println!("[{}] working", name);

    guard.wait().await;
    println!("[{}] shutting down", name);

    tokio::time::sleep(Duration::from_secs(1)).await;
    println!("[{}] done", name);
}

#[tokio::main]
async fn main() {
    tokio::spawn(worker("worker 1"));
    tokio::spawn(worker("worker 2"));

    elegant_departure::tokio::depart()
        // Shutdown on Ctrl+C and SIGTERM
        .on_termination()
        .await
}

Dependencies

~3–13MB
~121K SLoC