Skip to content

add support for compression body in http injection api #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dschaaff
Copy link
Contributor

@dschaaff dschaaff commented Jul 3, 2025

Compressing the body on HTTP injections can significantly save on bandwidth. It helps lower cross AZ bandwidth charged in AWS for example. I'll note that is my first time contribution in rust. I'm open to any changes or feedback there is. Thanks!

@dschaaff dschaaff force-pushed the inject-http-body-compression branch 2 times, most recently from c464252 to 7adf3fc Compare July 7, 2025 21:48
@wez
Copy link
Collaborator

wez commented Jul 8, 2025

Thanks for this!

I have an older unfinished branch from when I was initially experimenting with this, it looks similar but the layer is added more centrally:

diff --git a/crates/kumo-server-common/Cargo.toml b/crates/kumo-server-common/Cargo.toml
index 780dc087..16284260 100644
--- a/crates/kumo-server-common/Cargo.toml
+++ b/crates/kumo-server-common/Cargo.toml
@@ -60,7 +60,7 @@ throttle = {path="../throttle"}
 tokio = {workspace=true, features=["full", "tracing"]}
 tokio-metrics = "0.3.1"
 tokio-metrics-collector = "0.2.1"
-tower-http = {version="0.5", features=["trace", "compression-deflate", "compression-gzip"]}
+tower-http = {version="0.5", features=["trace", "compression-deflate", "compression-gzip", "decompression-deflate", "decompression-gzip"]}
 tracing = "0.1"
 tracing-appender = "0.2"
 tracing-subscriber = {version="0.3", features=["env-filter", "std", "fmt", "json"]}
diff --git a/crates/kumo-server-common/src/http_server/mod.rs b/crates/kumo-server-common/src/http_server/mod.rs
index 1bf5d293..4cffed4f 100644
--- a/crates/kumo-server-common/src/http_server/mod.rs
+++ b/crates/kumo-server-common/src/http_server/mod.rs
@@ -14,6 +14,7 @@ use serde::Deserialize;
 use std::net::{IpAddr, SocketAddr, TcpListener};
 use std::sync::Arc;
 use tower_http::compression::CompressionLayer;
+use tower_http::decompression::RequestDecompressionLayer;
 use tower_http::trace::TraceLayer;
 use utoipa::openapi::security::{Http, HttpAuthScheme, SecurityScheme};
 use utoipa::OpenApi;
@@ -144,6 +145,7 @@ impl HttpListenerParams {
             .deflate(true)
             .gzip(true)
             .quality(tower_http::CompressionLevel::Fastest);
+        let decompression_layer = RequestDecompressionLayer::new().deflate(true).gzip(true);

         let app = router_and_docs
             .router
@@ -167,6 +169,7 @@ impl HttpListenerParams {
                 auth_middleware,
             ))
             .layer(compression_layer)
+            .layer(decompression_layer)
             .layer(TraceLayer::new_for_http());
         let socket = TcpListener::bind(&self.listen)
             .with_context(|| format!("listen on {}", self.listen))?;

It also only explicitly enables only gzip/deflate to reduce the overall set of dependencies

@dschaaff
Copy link
Contributor Author

dschaaff commented Jul 8, 2025

It also only explicitly enables only gzip/deflate to reduce the overall set of dependencies

Thanks, I'll update this branch with that change.

...it looks similar but the layer is added more centrally:

Would you prefer I move the dependency to the kumo-server-common crate?

@dschaaff dschaaff force-pushed the inject-http-body-compression branch from c8974a1 to 36d90e5 Compare July 10, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants