From d2c3103b6066e3037281d1857dbf9f7946fff962 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Mon, 27 Jan 2025 08:37:13 -0500 Subject: [PATCH] Make memory-logging optional (off by default) --- CHANGELOG.md | 2 ++ README.md | 4 ++++ src/main.rs | 18 ++++++++++++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc80159..fdcc883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ In Development - Handling of incoming requests now times out after 25 seconds - Increased MSRV to 1.81 - Outgoing requests to servers other than S3 now time out after 10 seconds +- Memory usage is now only logged if `--log-memory` was supplied on the command + line v0.5.0 (2024-11-18) ------------------- diff --git a/README.md b/README.md index 6b3b3d2..7a7ac89 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,10 @@ Options - `--ip-addr ` — Specify the IP address for the server to listen on [default: 127.0.0.1] +- `--log-memory` — Log the process's memory usage at the start & end of each + incoming request. Note that this slows down the overall request-processing + time. + - `-p `, `--port ` — Specify the port for the server to listen on [default: 8080] diff --git a/src/main.rs b/src/main.rs index 62711a6..bf10f78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,11 @@ struct Config { #[arg(long, default_value = DEFAULT_API_URL, value_name = "URL")] api_url: HttpUrl, + /// Log the process's memory usage at the start & end of each incoming + /// request + #[arg(long)] + log_memory: bool, + /// Redirect requests for blob assets directly to S3 instead of to Archive /// URLs that redirect to signed S3 URLs #[arg(long)] @@ -99,6 +104,7 @@ impl Default for Config { api_url: DEFAULT_API_URL .parse::() .expect("DEFAULT_API_URL should be a valid HttpUrl"), + log_memory: false, prefer_s3_redirects: false, title: env!("CARGO_PKG_NAME").into(), zarrman_cache_mb: 100, @@ -160,7 +166,7 @@ fn get_app(cfg: Config) -> anyhow::Result { templater, prefer_s3_redirects: cfg.prefer_s3_redirects, }); - Ok(Router::new() + let mut app = Router::new() .route( "/.static/styles.css", get(|| async { @@ -179,8 +185,11 @@ fn get_app(cfg: Config) -> anyhow::Result { let dav = Arc::clone(&dav); async move { dav.handle_request(req).await } })) - .layer(middleware::from_fn(handle_head)) - .layer(middleware::from_fn(log_memory)) + .layer(middleware::from_fn(handle_head)); + if cfg.log_memory { + app = app.layer(middleware::from_fn(log_memory)); + } + app = app .layer(SetResponseHeaderLayer::if_not_present( SERVER, HeaderValue::from_static(SERVER_VALUE), @@ -218,7 +227,8 @@ fn get_app(cfg: Config) -> anyhow::Result { "starting processing request", ); }), - )) + ); + Ok(app) } /// Handle `HEAD` requests by converting them to `GET` requests and discarding