Skip to content

Make shutdown_signal() Function a One Line Operation Called as a Default #2805

@LeahWhalen

Description

@LeahWhalen

Calling a default shutdown signal function prevents redundant code
I think putting the shutdown signal function as a default somewhere would make crates like axum look nicer when defining a shutdown signal, building servers would be easier, and developers wouldn't have to initialize it every time.

Proposal
One could easily make some sort of "shutdown signal" struct with a default new and a few customization options.
Here is the shutdown_signal function being referred to:

async fn shutdown_signal() {
    let ctrl_c = async {
        signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }
    println!("signal received, starting graceful shutdown");
}

Describe alternatives you've considered
I could possibly make my own wrapper? I tried to ask axum to do this as I use their crate, but they pointed out it was part of the hyper::server::Server struct

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-featureCategory: feature. This is adding a new feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions