-
-
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.
feat(core): Graceful shutdown and refactor Handler (#126)
* feat(core): remove Sync bounds on Handler trait * chore: MapErr * chore: MapIntoResponse * refactor: remove async_trait on Handler * feat: remove dyn-clone * feat: remove redundant 'static * feat: remove redundant 'static * chore(core): short form * chore(tower): improve * chore(core): improve hyper Service * chore(viz): implement Handler for Responder * fix: remove Arc on Tree * feat(viz): add Server * chore(viz): remove serve * feat(viz): implement Accept for tls * chore(viz): cfg rustls and native_tls * chore(viz): add not_found * fix: examples * fix: useless into * fix: make Cloneable pub crate * docs: TryHandler * fix(doc): links * chore: revert Sync * chore: revert Sync * chore: revert lint * chore: revert async_trait * fix: doc tests * chore: revert static-routes * fix: boxed bounds * fix: FnExt bounds * chore(core): remove redundance bounds on MapErr * chore(core): remove redundance bounds on Map * chore(core): remove redundance bounds on Either * chore(core): remove redundance bounds on CatchUnwind * chore(core): improve * chore(core): remove redundance allow attrs * chore(core): remove redundance bounds on Cookie Middleware * chore(core): remove redundance bounds on Cors middleware * chore(core): remove redundance bounds on Cors middleware * chore(core): remove redundance scope on Csrf middleware * chore(core): remove redundance bounds on Limits middleware * chore(core): remove redundance bounds on tests * chore(core): remove redundance bounds on Router * chore(core): remove redundance bounds on Route * chore(core): remove redundance bounds on Resources * chore(core): remove redundance bounds on Cloneable * chore(core): remove redundance bounds on docs * chore(viz): use tokio_util Listener * fix(ci): coverage
- Loading branch information
Showing
99 changed files
with
855 additions
and
833 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
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 => {}, | ||
} | ||
} |
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,26 +1,28 @@ | ||
#![deny(warnings)] | ||
#![allow(clippy::unused_async)] | ||
|
||
use std::{net::SocketAddr, sync::Arc}; | ||
use std::{net::SocketAddr, str::FromStr}; | ||
use tokio::net::TcpListener; | ||
use viz::{serve, Request, Result, Router, Tree}; | ||
use viz::{serve, Request, Result, Router}; | ||
|
||
async fn index(_: Request) -> Result<&'static str> { | ||
Ok("Hello, World!") | ||
async fn index(_: Request) -> Result<String> { | ||
Ok(String::from("Hello, World!")) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let addr = SocketAddr::from_str("[::1]:3000").unwrap(); | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("listening on http://{addr}"); | ||
|
||
let app = Router::new().get("/", index); | ||
let tree = Arc::new(Tree::from(app)); | ||
let mut app = Router::new().get("/", |_| async { Ok("Hello, World!") }); | ||
|
||
loop { | ||
let (stream, addr) = listener.accept().await?; | ||
let tree = tree.clone(); | ||
tokio::task::spawn(serve(stream, tree, Some(addr))); | ||
for n in 0..1000 { | ||
app = app.get(&format!("/{}", n), index); | ||
} | ||
|
||
if let Err(e) = serve(listener, app).await { | ||
println!("{e}"); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.