Skip to content

Commit

Permalink
Merge pull request #120 from dandi/log-mem
Browse files Browse the repository at this point in the history
Log current memory usage before & after handling each request
  • Loading branch information
jwodder authored Mar 28, 2024
2 parents 4c0005d + 201ed47 commit 8c239c7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
In Development
--------------
- Set `Access-Control-Allow-Origin: *` header in all responses
- Log current memory usage before & after handling each request

v0.3.0 (2024-03-15)
-------------------
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ futures-util = "0.3.30"
humansize = "2.1.3"
indoc = "2.0.5"
itertools = "0.12.1"
memory-stats = "1.1.0"
moka = { version = "0.12.5", features = ["future"] }
percent-encoding = "2.3.1"
reqwest = { version = "0.11.26", default-features = false, features = ["json", "rustls-tls-native-roots"] }
Expand Down
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,13 @@ async fn run() -> anyhow::Result<()> {
"/",
service_fn(move |req: Request| {
let dav = Arc::clone(&dav);
// Box the large future:
async move { Box::pin(dav.handle_request(req)).await }
async move {
log_memory();
// Box the large future:
let r = Box::pin(dav.handle_request(req)).await;
log_memory();
r
}
}),
)
.layer(middleware::from_fn(handle_head))
Expand Down Expand Up @@ -147,3 +152,15 @@ async fn handle_head(method: Method, mut request: Request<Body>, next: Next) ->
next.run(request).await
}
}

fn log_memory() {
if let Some(stats) = memory_stats::memory_stats() {
tracing::info!(
"Current memory usage: {} physical, {} virtual",
stats.physical_mem,
stats.virtual_mem,
);
} else {
tracing::info!("Failed to get current memory usage");
}
}

0 comments on commit 8c239c7

Please sign in to comment.