-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compression for deferred responses (#2986)
We replace tower-http's `CompressionLayer` with a custom stream transformation. This is necessary because tower-http uses async-compression, which buffers data until the end of the stream to then write it, ensuring a better compression. This is incompatible with the multipart protocol for `@defer`, which requires chunks to be sent as soon as possible. So we need to compress them independently. This extracts parts of the codec module of async-compression, which so far is not public, and makes a streaming wrapper above it that flushes the compressed data on every response in the stream. This is expected to be temporary, as we have in flight PRs for async-compression: - Nullus157/async-compression#155 - Nullus157/async-compression#178 With Nullus157/async-compression#150 we might be able to at least remove the vendored code
- Loading branch information
Showing
20 changed files
with
1,090 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### Fix compression for deferred responses ([Issue #1572](https://github.com/apollographql/router/issues/1572)) | ||
|
||
We replace tower-http's `CompressionLayer` with a custom stream transformation. This is necessary because tower-http uses async-compression, which buffers data until the end of the stream to then write it, ensuring a better compression. This is incompatible with the multipart protocol for `@defer`, which requires chunks to be sent as soon as possible. So we need to compress them independently. | ||
|
||
This extracts parts of the codec module of async-compression, which so far is not public, and makes a streaming wrapper above it that flushes the compressed data on every response in the stream. | ||
|
||
By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/2986 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
apollo-router/src/axum_factory/compression/codec/brotli/encoder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// All code from this module is extracted from https://github.com/Nemo157/async-compression and is under MIT or Apache-2 licence | ||
// it will be removed when we find a long lasting solution to https://github.com/Nemo157/async-compression/issues/154 | ||
use std::fmt; | ||
use std::io::Error; | ||
use std::io::ErrorKind; | ||
use std::io::Result; | ||
|
||
use brotli::enc::backward_references::BrotliEncoderParams; | ||
use brotli::enc::encode::BrotliEncoderCompressStream; | ||
use brotli::enc::encode::BrotliEncoderCreateInstance; | ||
use brotli::enc::encode::BrotliEncoderHasMoreOutput; | ||
use brotli::enc::encode::BrotliEncoderIsFinished; | ||
use brotli::enc::encode::BrotliEncoderOperation; | ||
use brotli::enc::encode::BrotliEncoderStateStruct; | ||
use brotli::enc::StandardAlloc; | ||
|
||
use crate::axum_factory::compression::codec::Encode; | ||
use crate::axum_factory::compression::util::PartialBuffer; | ||
|
||
pub(crate) struct BrotliEncoder { | ||
state: BrotliEncoderStateStruct<StandardAlloc>, | ||
} | ||
|
||
impl BrotliEncoder { | ||
pub(crate) fn new(params: BrotliEncoderParams) -> Self { | ||
let mut state = BrotliEncoderCreateInstance(StandardAlloc::default()); | ||
state.params = params; | ||
Self { state } | ||
} | ||
|
||
fn encode( | ||
&mut self, | ||
input: &mut PartialBuffer<impl AsRef<[u8]>>, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
op: BrotliEncoderOperation, | ||
) -> Result<()> { | ||
let in_buf = input.unwritten(); | ||
let out_buf = output.unwritten_mut(); | ||
|
||
let mut input_len = 0; | ||
let mut output_len = 0; | ||
|
||
if BrotliEncoderCompressStream( | ||
&mut self.state, | ||
op, | ||
&mut in_buf.len(), | ||
in_buf, | ||
&mut input_len, | ||
&mut out_buf.len(), | ||
out_buf, | ||
&mut output_len, | ||
&mut None, | ||
&mut |_, _, _, _| (), | ||
) <= 0 | ||
{ | ||
return Err(Error::new(ErrorKind::Other, "brotli error")); | ||
} | ||
|
||
input.advance(input_len); | ||
output.advance(output_len); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Encode for BrotliEncoder { | ||
fn encode( | ||
&mut self, | ||
input: &mut PartialBuffer<impl AsRef<[u8]>>, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
) -> Result<()> { | ||
self.encode( | ||
input, | ||
output, | ||
BrotliEncoderOperation::BROTLI_OPERATION_PROCESS, | ||
) | ||
} | ||
|
||
fn flush( | ||
&mut self, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
) -> Result<bool> { | ||
self.encode( | ||
&mut PartialBuffer::new(&[][..]), | ||
output, | ||
BrotliEncoderOperation::BROTLI_OPERATION_FLUSH, | ||
)?; | ||
|
||
Ok(BrotliEncoderHasMoreOutput(&self.state) == 0) | ||
} | ||
|
||
fn finish( | ||
&mut self, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
) -> Result<bool> { | ||
self.encode( | ||
&mut PartialBuffer::new(&[][..]), | ||
output, | ||
BrotliEncoderOperation::BROTLI_OPERATION_FINISH, | ||
)?; | ||
|
||
Ok(BrotliEncoderIsFinished(&self.state) == 1) | ||
} | ||
} | ||
|
||
impl fmt::Debug for BrotliEncoder { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("BrotliEncoder") | ||
.field("compress", &"<no debug>") | ||
.finish() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
apollo-router/src/axum_factory/compression/codec/brotli/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// All code from this module is extracted from https://github.com/Nemo157/async-compression and is under MIT or Apache-2 licence | ||
// it will be removed when we find a long lasting solution to https://github.com/Nemo157/async-compression/issues/154 | ||
mod encoder; | ||
|
||
pub(crate) use self::encoder::BrotliEncoder; |
46 changes: 46 additions & 0 deletions
46
apollo-router/src/axum_factory/compression/codec/deflate/encoder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// All code from this module is extracted from https://github.com/Nemo157/async-compression and is under MIT or Apache-2 licence | ||
// it will be removed when we find a long lasting solution to https://github.com/Nemo157/async-compression/issues/154 | ||
use std::io::Result; | ||
|
||
use flate2::Compression; | ||
|
||
use crate::axum_factory::compression::codec::Encode; | ||
use crate::axum_factory::compression::codec::FlateEncoder; | ||
use crate::axum_factory::compression::util::PartialBuffer; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct DeflateEncoder { | ||
inner: FlateEncoder, | ||
} | ||
|
||
impl DeflateEncoder { | ||
pub(crate) fn new(level: Compression) -> Self { | ||
Self { | ||
inner: FlateEncoder::new(level, false), | ||
} | ||
} | ||
} | ||
|
||
impl Encode for DeflateEncoder { | ||
fn encode( | ||
&mut self, | ||
input: &mut PartialBuffer<impl AsRef<[u8]>>, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
) -> Result<()> { | ||
self.inner.encode(input, output) | ||
} | ||
|
||
fn flush( | ||
&mut self, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
) -> Result<bool> { | ||
self.inner.flush(output) | ||
} | ||
|
||
fn finish( | ||
&mut self, | ||
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, | ||
) -> Result<bool> { | ||
self.inner.finish(output) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
apollo-router/src/axum_factory/compression/codec/deflate/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// All code from this module is extracted from https://github.com/Nemo157/async-compression and is under MIT or Apache-2 licence | ||
// it will be removed when we find a long lasting solution to https://github.com/Nemo157/async-compression/issues/154 | ||
mod encoder; | ||
|
||
pub(crate) use self::encoder::DeflateEncoder; |
Oops, something went wrong.