Skip to content

Commit

Permalink
Remove patched async-compression, disable compression for multipart r…
Browse files Browse the repository at this point in the history
…esponses instead

This is a different work-around for #1572.

The previous work-around required a `[patch.crates-io]` section in the root `Cargo.toml`.
When the Router is used as a dependency, that root manifest is not ours
so downstream users would need to apply the same patch to get the work-around.

This came up in the context of publish the Router to crates.io
#491
but applies the same when the `apollo-router` crate is used as a git dependency.
  • Loading branch information
SimonSapin committed Sep 12, 2022
1 parent 8a44b79 commit 18cc4fa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

8 changes: 0 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ members = [
# debug = false
strip = "debuginfo"
incremental = false

# Temporary patch to async-compression
# It is used by tower-http's CompressionLayer. The compression code was not handling
# the Poll::Pending result from the underlying stream, so it was accumulating the
# entire compressed response in memory before sending it, which creates issues with
# deferred responses getting received too late
[patch.crates-io]
async-compression = { git = 'https://github.com/geal/async-compression', tag = 'encoder-flush' }
11 changes: 9 additions & 2 deletions apollo-router/src/axum_http_server_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ use tokio::sync::Notify;
use tower::util::BoxService;
use tower::BoxError;
use tower::ServiceExt;
use tower_http::compression::predicate::NotForContentType;
use tower_http::compression::CompressionLayer;
use tower_http::compression::DefaultPredicate;
use tower_http::compression::Predicate;
use tower_http::trace::MakeSpan;
use tower_http::trace::TraceLayer;
use tower_service::Service;
Expand Down Expand Up @@ -189,7 +192,11 @@ where
.route(&configuration.server.health_check_path, get(health_check))
.layer(Extension(service_factory))
.layer(cors)
.layer(CompressionLayer::new()); // To compress response body
// Compress the response body, except for multipart responses such as with `@defer`.
// This is a work-around for https://github.com/apollographql/router/issues/1572
.layer(CompressionLayer::new().compress_when(
DefaultPredicate::new().and(NotForContentType::const_new("multipart/")),
));

let listener = configuration.server.listen.clone();
Ok(ListenAddrAndRouter(listener, route))
Expand Down Expand Up @@ -2622,7 +2629,7 @@ Content-Type: application/json\r
"variables": {"first": 2_u32},
}))
.await;
assert_compressed(&response, true);
assert_compressed(&response, false);
let status = response.status().as_u16();
assert_eq!(status, 200);
let counter: GraphQLResponseCounter = response.extensions_mut().remove().unwrap();
Expand Down

0 comments on commit 18cc4fa

Please sign in to comment.