File: shutdown-signal.rs

package info (click to toggle)
rust-actix-server 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,561 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Demonstrates use of the `ServerBuilder::shutdown_signal` method using `tokio-util`s
//! `CancellationToken` helper using a nonsensical timer. In practice, this cancellation token would
//! be wired throughout your application and typically triggered by OS signals elsewhere.

use std::{io, time::Duration};

use actix_rt::net::TcpStream;
use actix_server::Server;
use actix_service::fn_service;
use tokio_util::sync::CancellationToken;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{prelude::*, EnvFilter};

async fn run(stop_signal: CancellationToken) -> io::Result<()> {
    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        .with(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .init();

    let addr = ("127.0.0.1", 8080);
    tracing::info!("starting server on port: {}", &addr.0);

    Server::build()
        .bind("shutdown-signal", addr, || {
            fn_service(|_stream: TcpStream| async { Ok::<_, io::Error>(()) })
        })?
        .shutdown_signal(stop_signal.cancelled_owned())
        .workers(2)
        .run()
        .await
}

#[tokio::main]
async fn main() -> io::Result<()> {
    let stop_signal = CancellationToken::new();

    tokio::spawn({
        let stop_signal = stop_signal.clone();
        async move {
            tokio::time::sleep(Duration::from_secs(10)).await;
            stop_signal.cancel();
        }
    });

    run(stop_signal).await?;
    Ok(())
}