-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement an API to request a graceful shutdown.
Additionally listen for Ctrl-C as a shutdown signal by default.
- Loading branch information
Showing
38 changed files
with
202 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::request::{FromRequest, Outcome, Request}; | ||
use futures::channel::mpsc; | ||
|
||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # #![feature(proc_macro_hygiene)] | ||
/// # #[macro_use] extern crate rocket; | ||
/// # | ||
/// use rocket::shutdown::ShutdownHandle; | ||
/// | ||
/// #[get("/shutdown")] | ||
/// fn shutdown(handle: ShutdownHandle) -> &'static str { | ||
/// handle.shutdown(); | ||
/// "Shutting down..." | ||
/// } | ||
/// | ||
/// fn main() { | ||
/// # if false { | ||
/// rocket::ignite() | ||
/// .mount("/", routes![shutdown]) | ||
/// .launch() | ||
/// .expect("server failed unexpectedly"); | ||
/// # } | ||
/// } | ||
/// ``` | ||
#[derive(Debug, Clone)] | ||
pub struct ShutdownHandle(pub(crate) mpsc::Sender<()>); | ||
|
||
impl ShutdownHandle { | ||
/// Notify Rocket to shut down gracefully. | ||
#[inline] | ||
pub fn shutdown(mut self) { | ||
// Intentionally ignore any error, as the only scenarios this can happen | ||
// is sending too many shutdown requests or we're already shut down. | ||
let _ = self.0.try_send(()); | ||
} | ||
} | ||
|
||
impl FromRequest<'_, '_> for ShutdownHandle { | ||
type Error = std::convert::Infallible; | ||
|
||
#[inline] | ||
fn from_request(request: &Request<'_>) -> Outcome<Self, Self::Error> { | ||
Outcome::Success(request.state.managed.get::<ShutdownHandleManaged>().0.clone()) | ||
} | ||
} | ||
|
||
// Use this type in managed state to avoid placing `ShutdownHandle` in it. | ||
pub(crate) struct ShutdownHandleManaged(pub ShutdownHandle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// This example's illustration is the Rocket.toml file. | ||
fn main() { | ||
rocket::ignite().launch(); | ||
let _ = rocket::ignite().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,5 @@ fn rocket() -> rocket::Rocket { | |
} | ||
|
||
fn main() { | ||
rocket().launch(); | ||
let _ = rocket().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,5 +96,5 @@ fn rocket() -> rocket::Rocket { | |
} | ||
|
||
fn main() { | ||
rocket().launch(); | ||
let _ = rocket().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,5 +46,5 @@ fn rocket() -> rocket::Rocket { | |
} | ||
|
||
fn main() { | ||
rocket().launch(); | ||
let _ = rocket().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,5 +82,5 @@ fn rocket() -> rocket::Rocket { | |
} | ||
|
||
fn main() { | ||
rocket().launch(); | ||
let _ = rocket().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,5 +78,5 @@ fn rocket() -> rocket::Rocket { | |
} | ||
|
||
fn main() { | ||
rocket().launch(); | ||
let _ = rocket().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.