#shutdown #graceful #async-io

graceful-shutdown

Graceful Shutdown for async code

3 releases (breaking)

0.3.0 Jul 4, 2024
0.2.0 Nov 15, 2022
0.1.0 Feb 8, 2021

#307 in Asynchronous

Download history 2791/week @ 2024-07-02 2763/week @ 2024-07-09 1352/week @ 2024-07-16 3982/week @ 2024-07-23 3173/week @ 2024-07-30 3828/week @ 2024-08-06 4007/week @ 2024-08-13 1765/week @ 2024-08-20 1385/week @ 2024-08-27 2357/week @ 2024-09-03 1219/week @ 2024-09-10 359/week @ 2024-09-17 200/week @ 2024-09-24 1518/week @ 2024-10-01 268/week @ 2024-10-08 484/week @ 2024-10-15

2,526 downloads per month

Apache-2.0

21KB
291 lines

A library to make it easier to handle graceful shutdowns in async code.

This provides tools to wait for pending tasks to complete before finalizing shutdown.

Examples

use graceful_shutdown::Shutdown;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::signal;
use tokio::{select, spawn};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let shutdown = Shutdown::new();
    let listener = TcpListener::bind("127.0.0.1:8000").await?;
    spawn(shutdown.shutdown_after(signal::ctrl_c()));
    loop {
       match shutdown.cancel_on_shutdown(listener.accept()).await {
           Some(Ok((mut conn, _))) => {
               spawn(shutdown.graceful(async move {
                   let mut buf = [0; 1024];
                   loop {
                       let n = match conn.read(&mut buf).await {
                           Ok(n) if n == 0 => return,
                           Ok(n) => n,
                           Err(e) => {
                               eprintln!("failed to read from socket; err = {:?}", e);
                               return;
                           }
                       };
                       if let Err(e) = conn.write_all(&buf[0..n]).await {
                           eprintln!("failed to write to socket; err = {:?}", e);
                           return;
                       }
                   }
               }));
           }
           Some(Err(e)) => {
               eprintln!("Error accepting connection; err = {:?}", e);
               shutdown.shutdown();
           }
           None => {
               eprintln!("Starting shutdown");
               break;
           }
       }
    }
    shutdown.await;
    Ok(())
}

Dependencies

~0–9.5MB
~101K SLoC