Skip to content

Commit

Permalink
Merge pull request #223 from dandi/timeout-resp
Browse files Browse the repository at this point in the history
Add a response body to timeout errors
  • Loading branch information
jwodder authored Jan 16, 2025
2 parents c477043 + 964ae21 commit 7f7def5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ tera = { version = "1.20.0", default-features = false }
thiserror = "2.0.11"
time = { version = "0.3.37", features = ["formatting", "macros", "parsing", "serde"] }
tokio = { version = "1.43.0", features = ["macros", "net", "rt-multi-thread", "time"] }
tower = { version = "0.5.2", features = ["util"] }
tower-http = { version = "0.6.2", features = ["set-header", "timeout", "trace"] }
tower = { version = "0.5.2", features = ["timeout", "util"] }
tower-http = { version = "0.6.2", features = ["set-header", "trace"] }
tower_governor = { version = "0.6.0", features = ["tracing"] }
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.19", features = ["json", "local-time", "time"] }
Expand Down
19 changes: 12 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ use crate::zarrman::{ManifestFetcher, ZarrManClient};
use anyhow::Context;
use axum::{
body::Body,
error_handling::HandleErrorLayer,
extract::Request,
http::{
header::{
HeaderValue, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_LENGTH, CONTENT_TYPE, SERVER,
USER_AGENT,
},
response::Response,
Method,
Method, StatusCode,
},
middleware::{self, Next},
routing::get,
Expand All @@ -35,13 +36,11 @@ use http_body::Body as _;
use std::fmt;
use std::net::IpAddr;
use std::sync::Arc;
use tower::service_fn;
use tower::{service_fn, ServiceBuilder};
use tower_governor::{
governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor, GovernorLayer,
};
use tower_http::{
set_header::response::SetResponseHeaderLayer, timeout::TimeoutLayer, trace::TraceLayer,
};
use tower_http::{set_header::response::SetResponseHeaderLayer, trace::TraceLayer};
use tracing::Level;
use tracing_subscriber::{filter::Targets, fmt::time::OffsetTime, prelude::*};

Expand All @@ -51,6 +50,9 @@ static STYLESHEET: &str = include_str!("dav/static/styles.css");
/// The content of the `robots.txt` file to serve at `/robots.txt`
static ROBOTS_TXT: &str = "User-agent: *\nDisallow: /\n";

/// The body to return with 408 Request Timeout responses
static REQUEST_TIMEOUT_BODY: &str = "Request could not be completed in time\n";

/// WebDAV view to DANDI Archive
///
/// See <https://github.com/dandi/dandidav> for more information.
Expand Down Expand Up @@ -186,7 +188,11 @@ fn get_app(cfg: Config) -> anyhow::Result<Router> {
ACCESS_CONTROL_ALLOW_ORIGIN,
HeaderValue::from_static("*"),
))
.layer(TimeoutLayer::new(std::time::Duration::from_secs(25)))
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(|_| async { (StatusCode::REQUEST_TIMEOUT, REQUEST_TIMEOUT_BODY) }))
.timeout(std::time::Duration::from_secs(25))
)
.layer(GovernorLayer {
config: Arc::new(
GovernorConfigBuilder::default()
Expand Down Expand Up @@ -287,7 +293,6 @@ impl fmt::Display for UsizeDiff {
#[cfg(test)]
mod tests {
use super::*;
use axum::http::StatusCode;
use http_body_util::BodyExt;
use tower::ServiceExt; // for `collect`

Expand Down

0 comments on commit 7f7def5

Please sign in to comment.