From 55ebf54aebe3f070fd90498b87c42c666a6c6732 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 12 May 2024 11:07:41 +0100 Subject: [PATCH] Add default request timeout --- Cargo.lock | 1 + Cargo.toml | 2 +- flake.nix | 7 ++++++- src/main.rs | 6 +++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59a72d0..eb24f9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2802,6 +2802,7 @@ dependencies = [ "http-body 1.0.0", "http-body-util", "pin-project-lite", + "tokio", "tower-layer", "tower-service", ] diff --git a/Cargo.toml b/Cargo.toml index d0fe320..fc84831 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ tokio-stream = "0.1" tower = "0.4" tower-service = "0.3" tower-layer = "0.3" -tower-http = { version = "0.5", features = ["cors"] } +tower-http = { version = "0.5", features = ["cors", "timeout"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } unix_mode = "0.1" diff --git a/flake.nix b/flake.nix index e4b23d5..de71546 100644 --- a/flake.nix +++ b/flake.nix @@ -47,6 +47,11 @@ description = "Path to repositories"; type = types.path; }; + requestTimeout = mkOption { + default = "10s"; + description = "Timeout for incoming HTTP requests"; + type = types.str; + }; }; config = mkIf cfg.enable { @@ -65,7 +70,7 @@ path = [ pkgs.git ]; serviceConfig = { Type = "exec"; - ExecStart = "${self.defaultPackage."${system}"}/bin/rgit --db-store ${cfg.dbStorePath} ${cfg.bindAddress} ${cfg.repositoryStorePath}"; + ExecStart = "${self.defaultPackage."${system}"}/bin/rgit --request-timeout ${cfg.requestTimeout} --db-store ${cfg.dbStorePath} ${cfg.bindAddress} ${cfg.repositoryStorePath}"; Restart = "on-failure"; User = "rgit"; diff --git a/src/main.rs b/src/main.rs index 234aa87..05dc0f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ use tokio::{ signal::unix::{signal, SignalKind}, sync::mpsc, }; -use tower_http::cors::CorsLayer; +use tower_http::{cors::CorsLayer, timeout::TimeoutLayer}; use tower_layer::layer_fn; use tracing::{error, info, instrument, warn}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; @@ -76,6 +76,9 @@ pub struct Args { /// Configures the metadata refresh interval (eg. "never" or "60s") #[clap(long, default_value_t = RefreshInterval::Duration(Duration::from_secs(300)))] refresh_interval: RefreshInterval, + /// Configures the request timeout. + #[clap(long, default_value_t = Duration::from_secs(10).into())] + request_timeout: humantime::Duration, } #[derive(Debug, Clone, Copy)] @@ -204,6 +207,7 @@ async fn main() -> Result<(), anyhow::Error> { get(static_favicon(include_bytes!("../statics/favicon.ico"))), ) .fallback(methods::repo::service) + .layer(TimeoutLayer::new(args.request_timeout.into())) .layer(layer_fn(LoggingMiddleware)) .layer(Extension(Arc::new(Git::new(syntax_set)))) .layer(Extension(db))