From 5be854b60d5cca5963f9bbf0f75d28423213b416 Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:54:06 +0000 Subject: [PATCH 1/3] Native: compress JSON responses like PHP's CompressResponse Native sent every answer uncompressed and advertised response_compression: []. PHP Server gzip- or deflate-encodes JSON answers of 1024 bytes or more when Accept-Encoding allows it (DW_COMPRESSION_ENABLED, default true). On a Dev cell behind a Cloudflare tunnel the origin answers are the cell's egress, which is shaped to 4 Mbit/s. The Cloudflare edge always asks the origin for "br, gzip" and decodes for clients that did not ask, so this reduces tunnel egress without any SDK change. Lab measurement (DW Standard Workflow v1, 1 wf/s, 4 workers): 384.8 KB -> 106.0 KB of origin response bytes per workflow. - New response_compression middleware, outermost on the router: PHP's encoding preference (gzip over deflate at equal q, *, q=0), JSON content types only, 1024-byte minimum, existing Content-Encoding left alone, Vary: Accept-Encoding. Only in-memory (exact-sized) bodies are encoded; streamed answers pass through. - DW_COMPRESSION_ENABLED (and its legacy name) is honored instead of ignored. - cluster info advertises ["gzip", "deflate"] when enabled, as PHP does; the native deviation is removed from the parity fixture. - History-page gzip reuses the same RFC 1952 encoder. Co-Authored-By: Claude Opus 5.5 --- docs/rust-server-port.md | 7 +- rust/src/config_audit.rs | 4 +- rust/src/runtime/cluster_info.rs | 6 +- rust/src/runtime/history_pages.rs | 12 +- rust/src/runtime/http.rs | 2 + rust/src/runtime/mod.rs | 1 + rust/src/runtime/response_compression.rs | 253 ++++++++++++++++++ rust/tests/execution/activity_results.rs | 2 +- .../ServerParity/cluster-capabilities.json | 4 - ...rParityClusterCapabilitiesContractTest.mjs | 2 +- 10 files changed, 269 insertions(+), 24 deletions(-) create mode 100644 rust/src/runtime/response_compression.rs diff --git a/docs/rust-server-port.md b/docs/rust-server-port.md index b3dbb5ebe..8de8d60ae 100644 --- a/docs/rust-server-port.md +++ b/docs/rust-server-port.md @@ -338,8 +338,11 @@ contract fixture before its correction. Capabilities are true only where native serves the feature. `workflow_activity_result_inspection` is now advertised: the `cluster-capabilities` fixture qualifies the activity result read against - published 2.11.7. Native advertises no manifest it does not publish and no - response compression. `native_unqualified` names each member it still + published 2.11.7. Native advertises no manifest it does not publish. + Like PHP's `CompressResponse`, native gzip- or deflate-encodes JSON answers + of 1024 bytes or more when `Accept-Encoding` allows it, and advertises + `response_compression: ["gzip", "deflate"]`, unless + `DW_COMPRESSION_ENABLED` is false. `native_unqualified` names each member it still leaves out: `topology`, the diagnostics view, the external payload transaction limit and the conformance manifests. Since server#1005 (3.1.0) native serves `supported_sdk_versions` and `client_compatibility` from one diff --git a/rust/src/config_audit.rs b/rust/src/config_audit.rs index dfaaafa23..c64684649 100644 --- a/rust/src/config_audit.rs +++ b/rust/src/config_audit.rs @@ -581,7 +581,7 @@ pub const SETTINGS: &[Setting] = &[ Setting { name: "DW_BOOTSTRAP_RETRIES", legacy: Some("WORKFLOW_SERVER_BOOTSTRAP_RETRIES"), disposition: Ignored }, Setting { name: "DW_CALLER_LABEL_HEADER", legacy: Some("WORKFLOW_SERVER_CALLER_LABEL_HEADER"), disposition: Ignored }, Setting { name: "DW_CALLER_TYPE_HEADER", legacy: Some("WORKFLOW_SERVER_CALLER_TYPE_HEADER"), disposition: Ignored }, - Setting { name: "DW_COMPRESSION_ENABLED", legacy: Some("WORKFLOW_SERVER_COMPRESSION_ENABLED"), disposition: Ignored }, + Setting { name: "DW_COMPRESSION_ENABLED", legacy: Some("WORKFLOW_SERVER_COMPRESSION_ENABLED"), disposition: Honored }, Setting { name: "DW_DEFAULT_NAMESPACE", legacy: Some("WORKFLOW_SERVER_DEFAULT_NAMESPACE"), disposition: Refused(AnyCase("default"), DEFAULT_NAMESPACE) }, Setting { name: "DW_DUE_TIMER_RECOVERY_SCAN_LIMIT", legacy: Some("WORKFLOW_SERVER_DUE_TIMER_RECOVERY_SCAN_LIMIT"), disposition: Ignored }, Setting { name: "DW_ENV_AUDIT_STRICT", legacy: None, disposition: Honored }, @@ -995,7 +995,7 @@ mod tests { &[("DW_V2_PIN_TO_RECORDED_FINGERPRINT", "no")], &[("DW_SERIALIZER", "")], &[("DW_STORAGE_CONNECTION", "pgsql"), ("DB_CONNECTION", "pgsql")], - // Ignored and reported settings take any value. + // Ignored and reported settings, and honored booleans, take any value. &[("DW_MAX_PENDING_ACTIVITIES", "10"), ("DW_COMPRESSION_ENABLED", "false")], ]; for values in accepted { diff --git a/rust/src/runtime/cluster_info.rs b/rust/src/runtime/cluster_info.rs index b8efc3520..a94fa73af 100644 --- a/rust/src/runtime/cluster_info.rs +++ b/rust/src/runtime/cluster_info.rs @@ -134,8 +134,8 @@ pub(super) fn capabilities() -> Value { json!({"schema": "durable_workflow.protocol.Value", "fingerprint": super::history_export::VALUE_FINGERPRINT, "framing": "single_object", "magic_hex": "c301"}), ); - // Native sends uncompressed responses. - map.insert("response_compression".into(), json!([])); + // PHP's CompressResponse encodings, unless DW_COMPRESSION_ENABLED is false. + map.insert("response_compression".into(), json!(super::response_compression::advertised())); Value::Object(map) } @@ -244,7 +244,7 @@ mod tests { assert_eq!(caps["child_workflow_start_turn"], json!(true)); assert_eq!(caps["condition_wait_idempotent_reopen"], json!(true)); assert_eq!(caps["condition_wait_occurrence_deadline"], json!(true)); - assert_eq!(caps["response_compression"], json!([])); + assert_eq!(caps["response_compression"], json!(["gzip", "deflate"])); assert_eq!(caps["timer_runtime_contract"], json!(false)); assert_eq!(caps["principal_attribution_contract"], json!(true)); } diff --git a/rust/src/runtime/history_pages.rs b/rust/src/runtime/history_pages.rs index 53ca3f54c..99c4e9279 100644 --- a/rust/src/runtime/history_pages.rs +++ b/rust/src/runtime/history_pages.rs @@ -113,7 +113,7 @@ pub(super) fn compress(payload: &mut Value, encoding: Option<&'static str>) -> R } let json = serde_json::to_vec(events)?; let compressed = match encoding { - "gzip" => gzip(&json), + "gzip" => super::response_compression::gzip(&json), _ => miniz_oxide::deflate::compress_to_vec(&json, LEVEL), }; payload["history_events"] = json!([]); @@ -122,16 +122,6 @@ pub(super) fn compress(payload: &mut Value, encoding: Option<&'static str>) -> R Ok(()) } -/// `gzencode`: an RFC 1952 member around one raw DEFLATE stream. -fn gzip(data: &[u8]) -> Vec { - const CRC32: crc::Crc = crc::Crc::::new(&crc::CRC_32_ISO_HDLC); - // Magic, deflate method, no flags or time, default level, Unix. - let mut member = vec![0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 3]; - member.extend(miniz_oxide::deflate::compress_to_vec(data, LEVEL)); - member.extend(CRC32.checksum(data).to_le_bytes()); - member.extend((data.len() as u32).to_le_bytes()); - member -} #[cfg(test)] mod tests { diff --git a/rust/src/runtime/http.rs b/rust/src/runtime/http.rs index da763b270..2f8d7d414 100644 --- a/rust/src/runtime/http.rs +++ b/rust/src/runtime/http.rs @@ -425,6 +425,8 @@ pub fn router(runtime: Runtime) -> Router { .layer(middleware::from_fn_with_state(runtime.clone(), storage_admission_guard)) .layer(middleware::from_fn_with_state(runtime.clone(), guard)) .layer(middleware::from_fn(super::payload_limits::enforce)) + // PHP's CompressResponse wraps every answer, refusals included. + .layer(middleware::from_fn(super::response_compression::compress)) .with_state(runtime) } diff --git a/rust/src/runtime/mod.rs b/rust/src/runtime/mod.rs index 5575946a1..8ea36a32a 100644 --- a/rust/src/runtime/mod.rs +++ b/rust/src/runtime/mod.rs @@ -99,6 +99,7 @@ mod command_payloads; mod completion_rules; mod deployments; mod priority_fairness; +mod response_compression; mod route_fallback; mod redrive; mod redrive_plan; diff --git a/rust/src/runtime/response_compression.rs b/rust/src/runtime/response_compression.rs new file mode 100644 index 000000000..368d71b8d --- /dev/null +++ b/rust/src/runtime/response_compression.rs @@ -0,0 +1,253 @@ +//! PHP's `CompressResponse` middleware: a JSON answer of 1024 bytes or more +//! is sent gzip- or deflate-encoded when the request's `Accept-Encoding` +//! allows it, unless `DW_COMPRESSION_ENABLED` is false (default true). +//! +//! As in PHP, gzip wins over deflate at equal weight, `*` stands for both, +//! `q=0` refuses an encoding, an answer that already has a +//! `Content-Encoding` is left alone, and `Vary: Accept-Encoding` is added. +//! Only answers whose whole body is already in memory (an exact size hint) +//! are encoded; streamed bodies pass through unchanged. +use axum::{ + body::{Body, HttpBody as _}, + extract::Request, + http::{HeaderMap, HeaderValue, header}, + middleware::Next, + response::Response, +}; +use std::sync::OnceLock; + +/// `CompressResponse::MIN_COMPRESS_BYTES`. +const MIN_COMPRESS_BYTES: u64 = 1024; +/// The largest in-memory answer that is encoded; bigger ones pass through. +const MAX_COMPRESS_BYTES: u64 = 64 * 1024 * 1024; +/// zlib's default level, which PHP's `gzencode` and `gzdeflate` use. +const LEVEL: u8 = 6; + +/// `config('server.compression.enabled')`: `filter_var(DW_COMPRESSION_ENABLED +/// ?? WORKFLOW_SERVER_COMPRESSION_ENABLED ?? true, FILTER_VALIDATE_BOOLEAN, +/// FILTER_NULL_ON_FAILURE) ?? true`, read once. +pub(super) fn enabled() -> bool { + static ENABLED: OnceLock = OnceLock::new(); + *ENABLED.get_or_init(|| configured(|name| std::env::var(name).ok())) +} + +fn configured(lookup: impl Fn(&str) -> Option) -> bool { + crate::config_audit::effective(lookup, "DW_COMPRESSION_ENABLED", Some("WORKFLOW_SERVER_COMPRESSION_ENABLED")) + .is_none_or(|value| value.filter_bool().unwrap_or(true)) +} + +/// The encodings advertised as `response_compression`. +pub(super) fn advertised() -> &'static [&'static str] { + if enabled() { &["gzip", "deflate"] } else { &[] } +} + +pub(super) async fn compress(request: Request, next: Next) -> Response { + let encoding = if enabled() { preferred(request.headers()) } else { None }; + let response = next.run(request).await; + match encoding { + Some(encoding) => encode(response, encoding).await, + None => response, + } +} + +/// `CompressResponse::preferredEncoding` over every `Accept-Encoding` value. +fn preferred(headers: &HeaderMap) -> Option<&'static str> { + let mut accept = String::new(); + for value in headers.get_all(header::ACCEPT_ENCODING) { + if let Ok(value) = value.to_str() { + if !accept.is_empty() { + accept.push(','); + } + accept.push_str(value); + } + } + let (mut gzip, mut deflate, mut wildcard) = (None::, None::, None::); + for part in accept.to_ascii_lowercase().split(',') { + let mut segments = part.split(';').map(str::trim); + let name = segments.next().unwrap_or(""); + if name.is_empty() { + continue; + } + let mut weight = 1.0; + for segment in segments { + if let Some(value) = segment.strip_prefix("q=") { + weight = value.trim().parse::().ok().filter(|q| q.is_finite()).map_or(0.0, |q| q.clamp(0.0, 1.0)); + } + } + match name { + "*" => wildcard = Some(weight), + "gzip" => gzip = Some(gzip.map_or(weight, |q| q.max(weight))), + "deflate" => deflate = Some(deflate.map_or(weight, |q| q.max(weight))), + _ => {} + } + } + let gzip = gzip.or(wildcard).unwrap_or(0.0); + let deflate = deflate.or(wildcard).unwrap_or(0.0); + if gzip > 0.0 && gzip >= deflate { + Some("gzip") + } else if deflate > 0.0 { + Some("deflate") + } else { + None + } +} + +fn json(headers: &HeaderMap) -> bool { + headers + .get(header::CONTENT_TYPE) + .and_then(|value| value.to_str().ok()) + .is_some_and(|value| value.contains("application/json")) +} + +async fn encode(response: Response, encoding: &'static str) -> Response { + let size = response.body().size_hint().exact(); + if response.headers().contains_key(header::CONTENT_ENCODING) + || !json(response.headers()) + || !size.is_some_and(|size| (MIN_COMPRESS_BYTES..=MAX_COMPRESS_BYTES).contains(&size)) + { + return response; + } + let (mut parts, body) = response.into_parts(); + let Ok(bytes) = axum::body::to_bytes(body, MAX_COMPRESS_BYTES as usize).await else { + // An exact-sized in-memory body cannot fail to collect. + return Response::from_parts(parts, Body::empty()); + }; + let encoded = match encoding { + "gzip" => gzip(&bytes), + _ => miniz_oxide::deflate::compress_to_vec(&bytes, LEVEL), + }; + parts.headers.remove(header::CONTENT_LENGTH); + parts.headers.insert(header::CONTENT_ENCODING, HeaderValue::from_static(encoding)); + let vary = parts + .headers + .get_all(header::VARY) + .iter() + .filter_map(|value| value.to_str().ok()) + .any(|value| value.split(',').any(|name| name.trim().eq_ignore_ascii_case("accept-encoding"))); + if !vary { + parts.headers.append(header::VARY, HeaderValue::from_static("Accept-Encoding")); + } + Response::from_parts(parts, Body::from(encoded)) +} + +/// `gzencode`: an RFC 1952 member around one raw DEFLATE stream. +pub(super) fn gzip(data: &[u8]) -> Vec { + const CRC32: crc::Crc = crc::Crc::::new(&crc::CRC_32_ISO_HDLC); + // Magic, deflate method, no flags or time, default level, Unix. + let mut member = vec![0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 3]; + member.extend(miniz_oxide::deflate::compress_to_vec(data, LEVEL)); + member.extend(CRC32.checksum(data).to_le_bytes()); + member.extend((data.len() as u32).to_le_bytes()); + member +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::{Router, http::StatusCode, middleware, response::IntoResponse, routing::get}; + use tower::ServiceExt as _; + + fn accept(value: &str) -> HeaderMap { + let mut headers = HeaderMap::new(); + headers.insert(header::ACCEPT_ENCODING, HeaderValue::from_str(value).unwrap()); + headers + } + + #[test] + fn preferred_encoding_follows_php() { + assert_eq!(preferred(&HeaderMap::new()), None); + assert_eq!(preferred(&accept("gzip")), Some("gzip")); + assert_eq!(preferred(&accept("br, gzip")), Some("gzip")); + assert_eq!(preferred(&accept("deflate")), Some("deflate")); + assert_eq!(preferred(&accept("deflate, gzip")), Some("gzip")); + assert_eq!(preferred(&accept("gzip;q=0.5, deflate")), Some("deflate")); + assert_eq!(preferred(&accept("gzip;q=0")), None); + assert_eq!(preferred(&accept("GZIP")), Some("gzip")); + assert_eq!(preferred(&accept("*")), Some("gzip")); + assert_eq!(preferred(&accept("*;q=0")), None); + assert_eq!(preferred(&accept("gzip;q=0, *")), Some("deflate")); + assert_eq!(preferred(&accept("br, zstd")), None); + assert_eq!(preferred(&accept("identity")), None); + assert_eq!(preferred(&accept("gzip;q=x")), None); + } + + #[test] + fn setting_defaults_to_enabled_and_reads_the_legacy_name() { + let env = |pairs: &'static [(&'static str, &'static str)]| { + move |name: &str| pairs.iter().find(|(key, _)| *key == name).map(|(_, value)| (*value).to_owned()) + }; + assert!(configured(env(&[]))); + assert!(configured(env(&[("DW_COMPRESSION_ENABLED", "true")]))); + assert!(!configured(env(&[("DW_COMPRESSION_ENABLED", "false")]))); + assert!(!configured(env(&[("DW_COMPRESSION_ENABLED", "0")]))); + assert!(!configured(env(&[("DW_COMPRESSION_ENABLED", "")]))); + assert!(configured(env(&[("DW_COMPRESSION_ENABLED", "maybe")]))); + assert!(!configured(env(&[("WORKFLOW_SERVER_COMPRESSION_ENABLED", "off")]))); + assert!(configured(env(&[("DW_COMPRESSION_ENABLED", "on"), ("WORKFLOW_SERVER_COMPRESSION_ENABLED", "off")]))); + } + + fn app() -> Router { + Router::new() + .route("/big", get(|| async { axum::Json(serde_json::json!({"blob": "x".repeat(4096)})) })) + .route("/small", get(|| async { axum::Json(serde_json::json!({"ok": true})) })) + .route("/text", get(|| async { "y".repeat(4096) })) + .route( + "/encoded", + get(|| async { + ([(header::CONTENT_TYPE, "application/json"), (header::CONTENT_ENCODING, "identity")], "z".repeat(4096)) + .into_response() + }), + ) + .route( + "/streamed", + get(|| async { + let chunks = futures_util::stream::iter(vec![Ok::<_, std::io::Error>("w".repeat(4096))]); + ([(header::CONTENT_TYPE, "application/json")], Body::from_stream(chunks)).into_response() + }), + ) + .layer(middleware::from_fn(compress)) + } + + async fn get_with(path: &str, accept_encoding: Option<&str>) -> (StatusCode, HeaderMap, Vec) { + let mut request = axum::http::Request::builder().uri(path); + if let Some(value) = accept_encoding { + request = request.header(header::ACCEPT_ENCODING, value); + } + let response = app().oneshot(request.body(Body::empty()).unwrap()).await.unwrap(); + let (parts, body) = response.into_parts(); + (parts.status, parts.headers, axum::body::to_bytes(body, usize::MAX).await.unwrap().to_vec()) + } + + #[tokio::test] + async fn large_json_answers_are_encoded_and_decode_to_the_same_bytes() { + let (_, plain_headers, plain) = get_with("/big", None).await; + assert!(!plain_headers.contains_key(header::CONTENT_ENCODING)); + let (status, headers, gzipped) = get_with("/big", Some("br, gzip")).await; + assert_eq!(status, StatusCode::OK); + assert_eq!(headers[header::CONTENT_ENCODING], "gzip"); + assert_eq!(headers[header::VARY], "Accept-Encoding"); + assert!(gzipped.len() < plain.len() / 10); + assert_eq!(&gzipped[..2], &[0x1f, 0x8b]); + let crc = u32::from_le_bytes(gzipped[gzipped.len() - 8..gzipped.len() - 4].try_into().unwrap()); + let length = u32::from_le_bytes(gzipped[gzipped.len() - 4..].try_into().unwrap()); + let inflated = miniz_oxide::inflate::decompress_to_vec(&gzipped[10..gzipped.len() - 8]).unwrap(); + assert_eq!(inflated, plain); + assert_eq!(length as usize, plain.len()); + assert_eq!(crc, crc::Crc::::new(&crc::CRC_32_ISO_HDLC).checksum(&plain)); + + let (_, headers, deflated) = get_with("/big", Some("deflate")).await; + assert_eq!(headers[header::CONTENT_ENCODING], "deflate"); + assert_eq!(miniz_oxide::inflate::decompress_to_vec(&deflated).unwrap(), plain); + } + + #[tokio::test] + async fn small_non_json_encoded_and_streamed_answers_pass_through() { + for path in ["/small", "/text", "/encoded", "/streamed"] { + let (_, plain_headers, plain) = get_with(path, None).await; + let (_, headers, body) = get_with(path, Some("gzip")).await; + assert_eq!(body, plain, "{path}"); + assert_eq!(headers.get(header::CONTENT_ENCODING), plain_headers.get(header::CONTENT_ENCODING), "{path}"); + assert!(!headers.contains_key(header::VARY), "{path}"); + } + } +} diff --git a/rust/tests/execution/activity_results.rs b/rust/tests/execution/activity_results.rs index 86a50a276..231479919 100644 --- a/rust/tests/execution/activity_results.rs +++ b/rust/tests/execution/activity_results.rs @@ -125,7 +125,7 @@ async fn activity_result_read_preserves_encoded_values_and_closed_parent_history // capability Waterline waits on is advertised. let info = request(&router(reopened.clone()), "GET", "/api/cluster/info", Value::Null).await.1; assert_eq!(info["capabilities"]["workflow_activity_result_inspection"], json!(true), "{info}"); - assert_eq!(info["capabilities"]["response_compression"], json!([]), "{info}"); + assert_eq!(info["capabilities"]["response_compression"], json!(["gzip", "deflate"]), "{info}"); assert_eq!(info["structural_limits"]["pending_signal_count"], json!(5000), "{info}"); assert_eq!(info["discovery"]["task_repair_path"], json!("/api/system/repair"), "{info}"); assert!(info["native_unqualified"]["topology"].is_string(), "{info}"); diff --git a/tests/Fixtures/ServerParity/cluster-capabilities.json b/tests/Fixtures/ServerParity/cluster-capabilities.json index 018866b0c..71b43836c 100644 --- a/tests/Fixtures/ServerParity/cluster-capabilities.json +++ b/tests/Fixtures/ServerParity/cluster-capabilities.json @@ -295,10 +295,6 @@ "value": false, "reason": "Native does not publish this manifest document, so it does not advertise the contract." }, - "capabilities.response_compression": { - "value": [], - "reason": "Native sends uncompressed responses." - }, "structural_limits.external_payload_transaction_bytes": { "value": "{absent}", "reason": "Native reads no DW_V2_LIMIT_EXTERNAL_PAYLOAD_TRANSACTION_BYTES." diff --git a/tests/Unit/ServerParityClusterCapabilitiesContractTest.mjs b/tests/Unit/ServerParityClusterCapabilitiesContractTest.mjs index b3e33a591..2d5893bb4 100644 --- a/tests/Unit/ServerParityClusterCapabilitiesContractTest.mjs +++ b/tests/Unit/ServerParityClusterCapabilitiesContractTest.mjs @@ -54,7 +54,7 @@ for (const [name, mutate] of [ ['native without build retirement', state => { delete state.capabilities.worker_build_retirement; }], ['native declaring build retirement unavailable', state => { state.capabilities.worker_build_retirement = false; }], ['native copying a manifest flag it does not publish', state => { state.capabilities.timer_runtime_contract = true; }], - ['native advertising response compression', state => { state.capabilities.response_compression = ['gzip']; }], + ['native advertising no response compression', state => { state.capabilities.response_compression = []; }], ['native without its unqualified list', state => { state.native_unqualified = []; }], ['an activity result read that differs', state => { state.activity_result.available.response.available = false; }], ]) test(`the contract rejects ${name}`, () => { From feea2571ed34c719b3c6979e99f08f5e937ea07e Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:42:54 +0000 Subject: [PATCH 2/3] Compress large answers off the async workers; zlib-wrapped deflate Review fixes: - Bodies above 256 KB are encoded on a blocking thread (spawn_blocking). At roughly 30 MB/s for level 6, an 8 MB body would otherwise hold an async worker for about 300 ms on a 2-thread runtime. - HTTP's deflate coding is the zlib format (RFC 1950), not raw DEFLATE; deflate answers are now zlib-wrapped. - Tests: a large answer encodes on the blocking path and round-trips for gzip and deflate, the zlib header is checked, and an ignored benchmark reports encode cost by size. Co-Authored-By: Claude Opus 5.5 --- rust/src/runtime/response_compression.rs | 67 +++++++++++++++++++++--- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/rust/src/runtime/response_compression.rs b/rust/src/runtime/response_compression.rs index 368d71b8d..07dc5bfbd 100644 --- a/rust/src/runtime/response_compression.rs +++ b/rust/src/runtime/response_compression.rs @@ -5,8 +5,11 @@ //! As in PHP, gzip wins over deflate at equal weight, `*` stands for both, //! `q=0` refuses an encoding, an answer that already has a //! `Content-Encoding` is left alone, and `Vary: Accept-Encoding` is added. -//! Only answers whose whole body is already in memory (an exact size hint) -//! are encoded; streamed bodies pass through unchanged. +//! `deflate` is the zlib format (RFC 1950) that HTTP's `deflate` coding names, +//! not raw DEFLATE. Only answers whose whole body is already in memory (an +//! exact size hint) are encoded; streamed bodies pass through unchanged. +//! Bodies above [`INLINE_MAX_BYTES`] are encoded on a blocking thread, so a +//! large answer never stalls the runtime's async workers. use axum::{ body::{Body, HttpBody as _}, extract::Request, @@ -20,6 +23,9 @@ use std::sync::OnceLock; const MIN_COMPRESS_BYTES: u64 = 1024; /// The largest in-memory answer that is encoded; bigger ones pass through. const MAX_COMPRESS_BYTES: u64 = 64 * 1024 * 1024; +/// The largest body encoded on an async worker (about 8 ms at ~30 MB/s, level +/// 6); bigger bodies move to a blocking thread. +const INLINE_MAX_BYTES: usize = 256 * 1024; /// zlib's default level, which PHP's `gzencode` and `gzdeflate` use. const LEVEL: u8 = 6; @@ -112,9 +118,15 @@ async fn encode(response: Response, encoding: &'static str) -> Response { // An exact-sized in-memory body cannot fail to collect. return Response::from_parts(parts, Body::empty()); }; - let encoded = match encoding { - "gzip" => gzip(&bytes), - _ => miniz_oxide::deflate::compress_to_vec(&bytes, LEVEL), + let encoded = if bytes.len() <= INLINE_MAX_BYTES { + encode_bytes(&bytes, encoding) + } else { + let source = bytes.clone(); + match tokio::task::spawn_blocking(move || encode_bytes(&source, encoding)).await { + Ok(encoded) => encoded, + // A failed encoder thread leaves the answer as it was. + Err(_) => return Response::from_parts(parts, Body::from(bytes)), + } }; parts.headers.remove(header::CONTENT_LENGTH); parts.headers.insert(header::CONTENT_ENCODING, HeaderValue::from_static(encoding)); @@ -130,6 +142,13 @@ async fn encode(response: Response, encoding: &'static str) -> Response { Response::from_parts(parts, Body::from(encoded)) } +fn encode_bytes(bytes: &[u8], encoding: &str) -> Vec { + match encoding { + "gzip" => gzip(bytes), + _ => miniz_oxide::deflate::compress_to_vec_zlib(bytes, LEVEL), + } +} + /// `gzencode`: an RFC 1952 member around one raw DEFLATE stream. pub(super) fn gzip(data: &[u8]) -> Vec { const CRC32: crc::Crc = crc::Crc::::new(&crc::CRC_32_ISO_HDLC); @@ -190,6 +209,11 @@ mod tests { Router::new() .route("/big", get(|| async { axum::Json(serde_json::json!({"blob": "x".repeat(4096)})) })) .route("/small", get(|| async { axum::Json(serde_json::json!({"ok": true})) })) + .route("/large", get(|| async { + // Above INLINE_MAX_BYTES: encoded on a blocking thread. + let events: Vec = (0..40_000).map(|n| format!("event-{n:08}-{}", "y".repeat(16))).collect(); + axum::Json(serde_json::json!({"events": events})) + })) .route("/text", get(|| async { "y".repeat(4096) })) .route( "/encoded", @@ -237,7 +261,38 @@ mod tests { let (_, headers, deflated) = get_with("/big", Some("deflate")).await; assert_eq!(headers[header::CONTENT_ENCODING], "deflate"); - assert_eq!(miniz_oxide::inflate::decompress_to_vec(&deflated).unwrap(), plain); + // HTTP `deflate` is the zlib format: a CMF/FLG header, then DEFLATE. + assert_eq!(deflated[0] & 0x0f, 8); + assert_eq!((u16::from(deflated[0]) << 8 | u16::from(deflated[1])) % 31, 0); + assert_eq!(miniz_oxide::inflate::decompress_to_vec_zlib(&deflated).unwrap(), plain); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn a_large_answer_is_encoded_off_the_async_workers_and_decodes_to_the_same_bytes() { + let (_, _, plain) = get_with("/large", None).await; + assert!(plain.len() > INLINE_MAX_BYTES, "{}", plain.len()); + let (_, headers, gzipped) = get_with("/large", Some("gzip")).await; + assert_eq!(headers[header::CONTENT_ENCODING], "gzip"); + let inflated = miniz_oxide::inflate::decompress_to_vec(&gzipped[10..gzipped.len() - 8]).unwrap(); + assert_eq!(inflated, plain); + let (_, headers, deflated) = get_with("/large", Some("deflate")).await; + assert_eq!(headers[header::CONTENT_ENCODING], "deflate"); + assert_eq!(miniz_oxide::inflate::decompress_to_vec_zlib(&deflated).unwrap(), plain); + } + + /// Level-6 cost by answer size, which sizes INLINE_MAX_BYTES: + /// `cargo test --release --lib response_compression::tests::encode_cost -- --ignored --nocapture`. + #[test] + #[ignore = "benchmark"] + fn encode_cost() { + for size in [17 * 1024, 256 * 1024, 8 * 1024 * 1024] { + let body = serde_json::to_vec(&(0..size / 24).map(|n| format!("event-{n:08}-{}", n % 97)).collect::>()).unwrap(); + let started = std::time::Instant::now(); + let encoded = gzip(&body); + let elapsed = started.elapsed(); + println!("{} -> {} bytes in {:?} ({:.0} MB/s)", body.len(), encoded.len(), elapsed, + body.len() as f64 / elapsed.as_secs_f64() / 1e6); + } } #[tokio::test] From efb0c0062393f4b8f4f4d6ec927d8f349697b44c Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:56:33 +0000 Subject: [PATCH 3/3] Record the measured inline encode cost encode_cost (release, lab): 14 KB in 0.6 ms, 217 KB in 10 ms, 7 MB in 196 ms (22-36 MB/s). Co-Authored-By: Claude Opus 5.5 --- rust/src/runtime/response_compression.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/runtime/response_compression.rs b/rust/src/runtime/response_compression.rs index 07dc5bfbd..22ee8c990 100644 --- a/rust/src/runtime/response_compression.rs +++ b/rust/src/runtime/response_compression.rs @@ -23,8 +23,8 @@ use std::sync::OnceLock; const MIN_COMPRESS_BYTES: u64 = 1024; /// The largest in-memory answer that is encoded; bigger ones pass through. const MAX_COMPRESS_BYTES: u64 = 64 * 1024 * 1024; -/// The largest body encoded on an async worker (about 8 ms at ~30 MB/s, level -/// 6); bigger bodies move to a blocking thread. +/// The largest body encoded on an async worker (measured about 10 ms at level +/// 6, 22-36 MB/s; see `encode_cost`); bigger bodies move to a blocking thread. const INLINE_MAX_BYTES: usize = 256 * 1024; /// zlib's default level, which PHP's `gzencode` and `gzdeflate` use. const LEVEL: u8 = 6;