forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement more control over graceful shutdown
This implements the "instant shutdown" flag describe in rwf2#180. rwf2#180 (comment)
- Loading branch information
Showing
6 changed files
with
264 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#[macro_use] extern crate rocket; | ||
|
||
use rocket::Shutdown; | ||
use rocket::response::Response; | ||
use tokio::io::AsyncRead; | ||
|
||
use std::pin::Pin; | ||
use std::task::{Poll, Context}; | ||
use std::time::Duration; | ||
|
||
struct AsyncReader(bool); | ||
|
||
impl AsyncRead for AsyncReader { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
_cx: &mut Context, | ||
buf: &mut [u8] | ||
) -> Poll<tokio::io::Result<usize>> { | ||
if self.0 { | ||
Poll::Ready(Ok(0)) | ||
} else { | ||
buf[0] = b'a'; | ||
Pin::<&mut AsyncReader>::into_inner(self).0 = true; | ||
Poll::Ready(Ok(1)) | ||
} | ||
} | ||
} | ||
|
||
#[get("/test")] | ||
fn test(shutdown: Shutdown) -> Response<'static> { | ||
shutdown.shutdown(); | ||
Response::build() | ||
.chunked_body(AsyncReader(false), 512) | ||
.wait_on_shutdown(Duration::from_millis(u64::MAX)) | ||
.finalize() | ||
} | ||
|
||
mod tests { | ||
use super::*; | ||
use rocket::local::blocking::Client; | ||
|
||
#[test] | ||
fn graceful_shutdown_works() { | ||
let rocket = rocket::ignite() | ||
.mount("/", routes![test]); | ||
let client = Client::new(rocket).unwrap(); | ||
|
||
let response = client.get("/test").dispatch(); | ||
assert_eq!(response.into_string().unwrap(), String::from("a")); | ||
} | ||
} |
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,53 @@ | ||
#[macro_use] extern crate rocket; | ||
|
||
use rocket::Shutdown; | ||
use rocket::response::Response; | ||
use tokio::io::AsyncRead; | ||
|
||
use std::pin::Pin; | ||
use std::task::{Poll, Context}; | ||
|
||
struct AsyncReader; | ||
|
||
impl AsyncRead for AsyncReader { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
_cx: &mut Context, | ||
_buf: &mut [u8] | ||
) -> Poll<tokio::io::Result<usize>> { | ||
Poll::Pending | ||
} | ||
} | ||
|
||
#[get("/test-shutdown")] | ||
async fn test(shutdown: Shutdown) -> Response<'static> { | ||
shutdown.shutdown(); | ||
Response::build() | ||
.chunked_body(AsyncReader, 512) | ||
.finalize() | ||
} | ||
|
||
#[get("/test-wait")] | ||
async fn test2(shutdown: Shutdown) -> Response<'static> { | ||
shutdown.wait().await; | ||
Response::build() | ||
.chunked_body(AsyncReader, 512) | ||
.finalize() | ||
} | ||
|
||
mod tests { | ||
use super::*; | ||
use rocket::local::asynchronous::Client; | ||
use futures::join; | ||
|
||
#[rocket::async_test] | ||
async fn graceful_shutdown_works() { | ||
let rocket = rocket::ignite() | ||
.mount("/", routes![test, test2]); | ||
let client = Client::new(rocket).await.unwrap(); | ||
|
||
let shutdown_response = client.get("/test-shutdown").dispatch(); | ||
let wait_response = client.get("/test-wait").dispatch(); | ||
let _ = join!(shutdown_response, wait_response); | ||
} | ||
} |
Oops, something went wrong.