mirror of
https://github.com/actix/examples
synced 2025-06-27 01:27:43 +02:00
use placeholder approach to shutdown-server
This commit is contained in:
@ -1,17 +1,15 @@
|
||||
use std::{sync::mpsc, thread};
|
||||
|
||||
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
|
||||
use actix_web::{dev::ServerHandle, get, middleware, post, web, App, HttpResponse, HttpServer};
|
||||
use actix_web_lab::extract::Path;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
#[get("/hello")]
|
||||
async fn hello() -> &'static str {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
#[post("/stop")]
|
||||
async fn stop(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse {
|
||||
// make request that sends message through the Sender
|
||||
stopper.send(()).unwrap();
|
||||
|
||||
#[post("/stop/{graceful}")]
|
||||
async fn stop(Path(graceful): Path<bool>, stop_handle: web::Data<StopHandle>) -> HttpResponse {
|
||||
let _ = stop_handle.stop(graceful);
|
||||
HttpResponse::NoContent().finish()
|
||||
}
|
||||
|
||||
@ -19,33 +17,48 @@ async fn stop(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
// create a channel
|
||||
let (tx, rx) = mpsc::channel::<()>();
|
||||
// create the stop handle container
|
||||
let stop_handle = web::Data::new(StopHandle::default());
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
// start server as normal but don't .await after .run() yet
|
||||
let server = HttpServer::new(move || {
|
||||
// give the server a Sender in .data
|
||||
App::new()
|
||||
.app_data(web::Data::new(tx.clone()))
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(hello)
|
||||
.service(stop)
|
||||
let srv = HttpServer::new({
|
||||
let stop_handle = stop_handle.clone();
|
||||
|
||||
move || {
|
||||
// give the server a Sender in .data
|
||||
App::new()
|
||||
.app_data(stop_handle.clone())
|
||||
.service(hello)
|
||||
.service(stop)
|
||||
.wrap(middleware::Logger::default())
|
||||
}
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.workers(2)
|
||||
.run();
|
||||
|
||||
// clone the server handle
|
||||
let srv = server.handle();
|
||||
thread::spawn(move || {
|
||||
// wait for shutdown signal
|
||||
rx.recv().unwrap();
|
||||
|
||||
// send stop server gracefully command
|
||||
srv.stop(true)
|
||||
});
|
||||
// register the server handle with the stop handle
|
||||
stop_handle.register(srv.handle());
|
||||
|
||||
// run server until stopped (either by ctrl-c or stop endpoint)
|
||||
server.await
|
||||
srv.await
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct StopHandle {
|
||||
inner: Mutex<Option<ServerHandle>>,
|
||||
}
|
||||
|
||||
impl StopHandle {
|
||||
/// Sets the server handle to stop.
|
||||
pub(crate) fn register(&self, handle: ServerHandle) {
|
||||
*self.inner.lock() = Some(handle);
|
||||
}
|
||||
|
||||
/// Sends stop signal through contained server handle.
|
||||
pub(crate) fn stop(&self, graceful: bool) {
|
||||
let _ = self.inner.lock().as_ref().unwrap().stop(graceful);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user