3 releases (breaking)

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

#361 in Asynchronous

Download history 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 506/week @ 2024-10-15 1014/week @ 2024-10-22 489/week @ 2024-10-29 173/week @ 2024-11-05 1100/week @ 2024-11-12 683/week @ 2024-11-19 465/week @ 2024-11-26

2,462 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
~103K SLoC