Search code examples
rusthyper

How do I terminate a hyper server after fulfilling one request?


I need a simple hyper server that serves a single request and then exits. This is my code so far, I believe that all I need is a way to get tx into hello, so I can use tx.send(()) and it should work the way I want it. However, I can't quite work out a way to do that without having the compiler yell at me.

use std::convert::Infallible;

use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};

async fn hello(_: Request<Body>) -> Result<Response<Body>, Infallible> {
    Ok(Response::new(Body::from("Hello World!")))
}

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

    let (tx, rx) = tokio::sync::oneshot::channel::<()>();

    let make_svc = make_service_fn(|_conn| {
        async { Ok::<_, Infallible>(service_fn(hello)) }
    });

    let addr = ([127, 0, 0, 1], 3000).into();

    let server = Server::bind(&addr).serve(make_svc);

    println!("Listening on http://{}", addr);

    let graceful = server.with_graceful_shutdown(async {
        rx.await.ok();
    });

    graceful.await?;

    Ok(())
}

Rust playground

Relevant crates:

tokio = { version = "0.2", features = ["full"] }
hyper = "0.13.7"

Since How to share mutable state for a Hyper handler? and How to share mutable state for a Hyper handler?, the hyper API has changed and I am unable to compile the code when edited to work with the current version.


Solution

  • A straightforward solution would be to use global state for this, made possible by tokio's Mutex type, like so:

    use hyper::service::{make_service_fn, service_fn};
    use hyper::{Body, Request, Response, Server};
    use lazy_static::lazy_static;
    use std::convert::Infallible;
    use std::sync::Arc;
    use tokio::sync::oneshot::Sender;
    use tokio::sync::Mutex;
    
    lazy_static! {
        /// Channel used to send shutdown signal - wrapped in an Option to allow
        /// it to be taken by value (since oneshot channels consume themselves on
        /// send) and an Arc<Mutex> to allow it to be safely shared between threads
        static ref SHUTDOWN_TX: Arc<Mutex<Option<Sender<()>>>> = <_>::default();
    }
    
    async fn hello(_: Request<Body>) -> Result<Response<Body>, Infallible> {
        // Attempt to send a shutdown signal, if one hasn't already been sent
        if let Some(tx) = SHUTDOWN_TX.lock().await.take() {
            let _ = tx.send(());
        }
    
        Ok(Response::new(Body::from("Hello World!")))
    }
    
    #[tokio::main]
    pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();
        SHUTDOWN_TX.lock().await.replace(tx);
    
        let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello)) });
    
        let addr = ([127, 0, 0, 1], 3000).into();
    
        let server = Server::bind(&addr).serve(make_svc);
    
        println!("Listening on http://{}", addr);
    
        let graceful = server.with_graceful_shutdown(async {
            rx.await.ok();
        });
    
        graceful.await?;
    
        Ok(())
    }
    

    In this version of the code, we store the sender half of the shutdown signal channel in a global variable protected by a mutex lock, and then attempt to consume the channel to send the signal on every request.