Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do Graceful Shutdown with low level API #2777

Closed
jacob-pro opened this issue Mar 13, 2022 · 2 comments
Closed

How to do Graceful Shutdown with low level API #2777

jacob-pro opened this issue Mar 13, 2022 · 2 comments
Labels
C-feature Category: feature. This is adding a new feature.

Comments

@jacob-pro
Copy link

jacob-pro commented Mar 13, 2022

I am currently using the low-level Http server API, which seems to suit my usage much better (#2321)

But I am wondering how can I do a graceful shutdown, I currently have a run loop that looks like:

    loop {
        tokio::select! {
            conn = listener.accept() => {
                match conn.expect("Tls listener stream should be infinite") {
                    Ok(conn) => {
                        // Does a tokio::spawn with the http.serve_connection
                        handle_connection(http.clone(), conn, state.clone())
                    },
                    Err(e) => {
                        tracing::warn!("Bad connection: {}", e);
                    }
                }
            },
            message = rx.recv() => {
                match message {
                    Some(message) => {
                        match message {
                            Message::Shutdown => {
                                tracing::info!("Beginning shutdown");
                                break;
                            },
                            Message::ReplaceTlsAcceptor(acceptor, ack) => {
                                tracing::info!("Replacing tls acceptor");
                                listener.replace_acceptor(acceptor);
                                ack.send(()).ok();
                            },
                            Message::ReloadConfig(new_config, ack) => {
                                tracing::info!("Reloading config");
                                state = Arc::new(state.clone_with_new_config(new_config));
                                ack.send(()).ok();
                            }
                        }
                    },
                    None => {
                        tracing::warn!("Server channel is closed, shutting down");
                        break;
                    }
                }
            }
        }
    }
    // TODO: Figure out how to gracefully shutdown remaining connections

But what I would really like is some way of polling the remaining requests before exiting the server.

Something like that was proposed by @seanmonstar here:
#2321 (comment)

@jacob-pro jacob-pro added the C-feature Category: feature. This is adding a new feature. label Mar 13, 2022
@jacob-pro
Copy link
Author

Ok so I've had a go at implementing this myself here, which seems to work:

https://github.com/jacob-pro/hyper-graceful

It is largely inspired on the tokio docs (using a broadcast to shutdown workers, and an mpsc to wait for them to complete):

https://tokio.rs/tokio/topics/shutdown

@seanmonstar
Copy link
Member

Superceded by #2862

@seanmonstar seanmonstar closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: feature. This is adding a new feature.
Projects
None yet
Development

No branches or pull requests

2 participants