-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
430 additions
and
486 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
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 |
---|---|---|
@@ -1,69 +1,61 @@ | ||
#![deny(warnings)] | ||
#![allow(clippy::unused_async)] | ||
|
||
//! Graceful shutdown server. | ||
//! | ||
//! See <https://github.com/hyperium/hyper/blob/master/examples/graceful_shutdown.rs> | ||
use std::{net::SocketAddr, sync::Arc, time::Duration}; | ||
use tokio::{net::TcpListener, pin}; | ||
use viz::{server::conn::http1, Io, Request, Responder, Result, Router, Tree}; | ||
use std::net::SocketAddr; | ||
use tokio::net::TcpListener; | ||
use tokio::signal; | ||
use tracing::{error, info}; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
use viz::{serve, Request, Result, Router}; | ||
|
||
async fn index(_: Request) -> Result<&'static str> { | ||
Ok("Hello, World!") | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "tracing=debug,hyper=debug".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("listening on {addr}"); | ||
info!("listening on {addr}"); | ||
|
||
let app = Router::new().get("/", index); | ||
let tree = Arc::new(Tree::from(app)); | ||
|
||
// Use a 5 second timeout for incoming connections to the server. | ||
// If a request is in progress when the 5 second timeout elapses, | ||
// use a 2 second timeout for processing the final request and graceful shutdown. | ||
let connection_timeouts = vec![Duration::from_secs(5), Duration::from_secs(2)]; | ||
|
||
loop { | ||
// Clone the connection_timeouts so they can be passed to the new task. | ||
let connection_timeouts_clone = connection_timeouts.clone(); | ||
let server = serve(listener, app).signal(shutdown_signal()); | ||
|
||
let (stream, addr) = listener.accept().await?; | ||
let tree = tree.clone(); | ||
if let Err(e) = server.await { | ||
error!("{e}"); | ||
} | ||
|
||
tokio::task::spawn(async move { | ||
// Pin the connection object so we can use tokio::select! below. | ||
let conn = http1::Builder::new() | ||
.serve_connection(Io::new(stream), Responder::new(tree, Some(addr))); | ||
pin!(conn); | ||
Ok(()) | ||
} | ||
|
||
// Iterate the timeouts. Use tokio::select! to wait on the | ||
// result of polling the connection itself, | ||
// and also on tokio::time::sleep for the current timeout duration. | ||
for (iter, sleep_duration) in connection_timeouts_clone.iter().enumerate() { | ||
println!("iter = {iter} sleep_duration = {sleep_duration:?}"); | ||
tokio::select! { | ||
res = conn.as_mut() => { | ||
// Polling the connection returned a result. | ||
// In this case print either the successful or error result for the connection | ||
// and break out of the loop. | ||
match res { | ||
Ok(()) => println!("after polling conn, no error"), | ||
Err(e) => println!("error serving connection: {e:?}"), | ||
}; | ||
break; | ||
} | ||
() = tokio::time::sleep(*sleep_duration) => { | ||
// tokio::time::sleep returned a result. | ||
// Call graceful_shutdown on the connection and continue the loop. | ||
println!("iter = {iter} got timeout_interval, calling conn.graceful_shutdown"); | ||
conn.as_mut().graceful_shutdown(); | ||
} | ||
} | ||
} | ||
}); | ||
async fn shutdown_signal() { | ||
let ctrl_c = async { | ||
signal::ctrl_c() | ||
.await | ||
.expect("failed to install Ctrl+C handler"); | ||
}; | ||
|
||
#[cfg(unix)] | ||
let terminate = async { | ||
signal::unix::signal(signal::unix::SignalKind::terminate()) | ||
.expect("failed to install signal handler") | ||
.recv() | ||
.await; | ||
}; | ||
|
||
#[cfg(not(unix))] | ||
let terminate = std::future::pending::<()>(); | ||
|
||
tokio::select! { | ||
_ = ctrl_c => {}, | ||
_ = terminate => {}, | ||
} | ||
} |
Oops, something went wrong.