Skip to content

Commit

Permalink
Added timeout of 30seconds for all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinastone committed Dec 15, 2023
1 parent 89f5e95 commit 767b0a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ url = "^2.2.1"
tokio = { version = "1.25.0", features = ["full"] }
tokio-stream = { version = "0.1.14", features = ["net"] }
tower = { version = "^0.4.12", features = ["full"] }
tower-http = { version = "^0.5", features=["trace"] }
tower-http = { version = "^0.5", features=["trace", "timeout"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
uri_path = { path = "uri_path" }
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::args::*;
use std::net::ToSocketAddrs;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::{runtime, signal};
use tower::ServiceBuilder;
use tower_http::timeout::TimeoutLayer;
use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

Expand Down Expand Up @@ -46,10 +48,10 @@ async fn shutdown_signal() {

fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG")
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "httpbox=debug,tower_http=debug".into()),
))
)
.with(tracing_subscriber::fmt::layer())
.init();

Expand All @@ -76,12 +78,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let runtime = runtime::Builder::new_multi_thread()
.worker_threads(threads.get())
.enable_io()
.enable_time()
.build()?;

tracing::info!("Listening on {} with {} threads", addr, threads);
runtime.block_on(async {
let service = ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(TimeoutLayer::new(Duration::from_secs(30)))
.service(service::router());

let listener = TcpListener::bind(addr).await.unwrap();
Expand Down

0 comments on commit 767b0a1

Please sign in to comment.