1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

use placeholder approach to shutdown-server

This commit is contained in:
Rob Ede 2022-08-07 23:58:56 +01:00
parent 2a680d0659
commit d259177eab
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 61 additions and 43 deletions

View File

@ -15,11 +15,11 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
version: ['stable', 'nightly'] version: [stable, nightly]
env: env:
# included in example of redis, not clear its purpose CI: '1'
CI: true CARGO_UNSTABLE_SPARSE_REGISTRY: 'true'
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2

2
Cargo.lock generated
View File

@ -5686,8 +5686,10 @@ name = "shutdown-server"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"actix-web-lab",
"env_logger 0.9.0", "env_logger 0.9.0",
"log", "log",
"parking_lot 0.12.1",
"tokio 1.20.1", "tokio 1.20.1",
] ]

View File

@ -6,7 +6,9 @@ description = "Send a request to the server to shut it down"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.16"
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"
parking_lot = "0.12"
tokio = { version = "1.13.1", features = ["signal"] } tokio = { version = "1.13.1", features = ["signal"] }

View File

@ -2,26 +2,27 @@
Demonstrates how to shutdown the web server in a couple of ways: Demonstrates how to shutdown the web server in a couple of ways:
1. remotely, via http request 1. remotely, via HTTP request
- Created in response to actix/actix-web#1315
1. sending a SIGINT signal to the server (control-c) 1. sending a SIGINT signal to the server (control-c)
- actix-server natively supports SIGINT - Actix Web servers support shutdown signals by default. [See here for more info.](https://actix.rs/docs/server#graceful-shutdown)
## Usage ## Usage
### Running The Server ### Running The Server
```sh ```console
cd shutdown-server $ cd shutdown-server
cargo run --bin shutdown-server $ cargo run --bin shutdown-server
[INFO] starting HTTP server at http://localhost:8080
# Starting 8 workers [INFO] Starting 2 workers
# Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080 [INFO] Actix runtime found; starting in Actix runtime
``` ```
### Available Routes ### Available Routes
- [GET /hello](http://localhost:8080/hello) - [`GET /hello`](http://localhost:8080/hello)
- Regular hello world route - Test hello world
- [POST /stop](http://localhost:8080/stop) - `POST /stop/true`
- Calling this will shutdown the server and exit - Gracefully shuts down the server and exit
- `POST /stop/false`
- Forces server shutdown and exits

View File

@ -1,17 +1,15 @@
use std::{sync::mpsc, thread}; use actix_web::{dev::ServerHandle, get, middleware, post, web, App, HttpResponse, HttpServer};
use actix_web_lab::extract::Path;
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer}; use parking_lot::Mutex;
#[get("/hello")] #[get("/hello")]
async fn hello() -> &'static str { async fn hello() -> &'static str {
"Hello world!" "Hello world!"
} }
#[post("/stop")] #[post("/stop/{graceful}")]
async fn stop(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse { async fn stop(Path(graceful): Path<bool>, stop_handle: web::Data<StopHandle>) -> HttpResponse {
// make request that sends message through the Sender let _ = stop_handle.stop(graceful);
stopper.send(()).unwrap();
HttpResponse::NoContent().finish() HttpResponse::NoContent().finish()
} }
@ -19,33 +17,48 @@ async fn stop(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse {
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// create a channel // create the stop handle container
let (tx, rx) = mpsc::channel::<()>(); let stop_handle = web::Data::new(StopHandle::default());
log::info!("starting HTTP server at http://localhost:8080"); log::info!("starting HTTP server at http://localhost:8080");
// start server as normal but don't .await after .run() yet // start server as normal but don't .await after .run() yet
let server = HttpServer::new(move || { let srv = HttpServer::new({
// give the server a Sender in .data let stop_handle = stop_handle.clone();
App::new()
.app_data(web::Data::new(tx.clone())) move || {
.wrap(middleware::Logger::default()) // give the server a Sender in .data
.service(hello) App::new()
.service(stop) .app_data(stop_handle.clone())
.service(hello)
.service(stop)
.wrap(middleware::Logger::default())
}
}) })
.bind(("127.0.0.1", 8080))? .bind(("127.0.0.1", 8080))?
.workers(2)
.run(); .run();
// clone the server handle // register the server handle with the stop handle
let srv = server.handle(); stop_handle.register(srv.handle());
thread::spawn(move || {
// wait for shutdown signal
rx.recv().unwrap();
// send stop server gracefully command
srv.stop(true)
});
// run server until stopped (either by ctrl-c or stop endpoint) // 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);
}
} }