-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(codec): add compression API, standard algorithms, and global reg… #2648
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| pub(crate) mod compression; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| use bytes::Buf; | ||
| use bytes::BufMut; | ||
|
|
||
| #[cfg(feature = "deflate")] | ||
| mod deflate; | ||
| #[cfg(feature = "gzip")] | ||
| mod gzip; | ||
| #[cfg(feature = "zstd")] | ||
| mod zstd; | ||
|
|
||
| pub(crate) mod registry; | ||
|
|
||
| /// A trait for compressing outgoing gRPC payloads. | ||
| pub trait Compressor: Send + Sync + 'static { | ||
| /// The canonical gRPC content coding name (e.g., "gzip"). | ||
| fn name(&self) -> &'static str; | ||
|
|
||
| /// Compress data from `source` into `destination`. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an `io::Error` if compression fails. Implementations should gracefully | ||
| /// handle constrained `destination` buffers by returning an error rather than panicking | ||
| /// (e.g., by verifying `destination.remaining_mut()` is sufficient before writing). | ||
| fn compress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe if we return a |
||
| } | ||
|
|
||
| /// A trait for decompressing incoming gRPC payloads. | ||
| pub trait Decompressor: Send + Sync + 'static { | ||
| /// The canonical gRPC content coding name (e.g., "gzip"). | ||
| fn name(&self) -> &'static str; | ||
|
|
||
| /// Decompress data from `source` into `destination`. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an `io::Error` if decompression fails. Implementations should gracefully | ||
| /// handle constrained `destination` buffers by returning an error rather than panicking | ||
| /// (e.g., by verifying `destination.remaining_mut()` is sufficient before writing). | ||
| fn decompress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (The same decision would apply here I believe.) |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| use std::io::Write; | ||
|
|
||
| use bytes::Buf; | ||
| use bytes::BufMut; | ||
| use flate2::Compression as FlateCompression; | ||
| use flate2::write::ZlibDecoder; | ||
| use flate2::write::ZlibEncoder; | ||
|
|
||
| use crate::codec::compression::Compressor; | ||
| use crate::codec::compression::Decompressor; | ||
|
|
||
| /// A deflate compression implementation. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct Deflate { | ||
| level: FlateCompression, | ||
| } | ||
|
|
||
| impl Deflate { | ||
| /// Creates a new deflate compression implementation. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| level: FlateCompression::new(6), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for Deflate { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl Compressor for Deflate { | ||
| fn name(&self) -> &'static str { | ||
| "deflate" | ||
| } | ||
|
|
||
| fn compress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String> { | ||
| let mut encoder = ZlibEncoder::new(destination.writer(), self.level); | ||
| while source.has_remaining() { | ||
| let chunk = source.chunk(); | ||
| encoder.write_all(chunk).map_err(|e| e.to_string())?; | ||
| source.advance(chunk.len()); | ||
| } | ||
| encoder.finish().map_err(|e| e.to_string())?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Decompressor for Deflate { | ||
| fn name(&self) -> &'static str { | ||
| "deflate" | ||
| } | ||
|
|
||
| fn decompress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String> { | ||
| let mut decoder = ZlibDecoder::new(destination.writer()); | ||
| while source.has_remaining() { | ||
| let chunk = source.chunk(); | ||
| decoder.write_all(chunk).map_err(|e| e.to_string())?; | ||
| source.advance(chunk.len()); | ||
| } | ||
| decoder.finish().map_err(|e| e.to_string())?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use bytes::Bytes; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn deflate_compress_decompress() { | ||
| let compressor = Deflate::new(); | ||
| let data = Bytes::from_static(b"hello world"); | ||
| let mut compressed = Vec::new(); | ||
| compressor | ||
| .compress(&mut data.clone(), &mut compressed) | ||
| .unwrap(); | ||
|
|
||
| assert_ne!(compressed.as_slice(), data); | ||
| let mut decompressed = Vec::new(); | ||
| compressor | ||
| .decompress(&mut compressed.as_slice(), &mut decompressed) | ||
| .unwrap(); | ||
| assert_eq!(data, decompressed.as_slice()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| use std::io::Write; | ||
|
|
||
| use bytes::Buf; | ||
| use bytes::BufMut; | ||
| use flate2::Compression as FlateCompression; | ||
| use flate2::write::GzDecoder; | ||
| use flate2::write::GzEncoder; | ||
|
|
||
| use crate::codec::compression::Compressor; | ||
| use crate::codec::compression::Decompressor; | ||
|
|
||
| /// A gzip compression implementation. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct Gzip { | ||
| level: FlateCompression, | ||
| } | ||
|
|
||
| impl Gzip { | ||
| /// Creates a new gzip compression implementation. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| level: FlateCompression::new(6), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for Gzip { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl Compressor for Gzip { | ||
| fn name(&self) -> &'static str { | ||
| "gzip" | ||
| } | ||
|
|
||
| fn compress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String> { | ||
| let mut encoder = GzEncoder::new(destination.writer(), self.level); | ||
| while source.has_remaining() { | ||
| let chunk = source.chunk(); | ||
| encoder.write_all(chunk).map_err(|e| e.to_string())?; | ||
| source.advance(chunk.len()); | ||
| } | ||
| encoder.finish().map_err(|e| e.to_string())?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Decompressor for Gzip { | ||
| fn name(&self) -> &'static str { | ||
| "gzip" | ||
| } | ||
|
|
||
| fn decompress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String> { | ||
| let mut decoder = GzDecoder::new(destination.writer()); | ||
| while source.has_remaining() { | ||
| let chunk = source.chunk(); | ||
| decoder.write_all(chunk).map_err(|e| e.to_string())?; | ||
| source.advance(chunk.len()); | ||
| } | ||
| decoder.finish().map_err(|e| e.to_string())?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use bytes::Bytes; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn gzip_compress_decompress() { | ||
| let compressor = Gzip::new(); | ||
| let data = Bytes::from_static(b"hello world"); | ||
| let mut compressed = Vec::new(); | ||
| compressor | ||
| .compress(&mut data.clone(), &mut compressed) | ||
| .unwrap(); | ||
|
|
||
| assert_ne!(compressed.as_slice(), data); | ||
| let mut decompressed = Vec::new(); | ||
| compressor | ||
| .decompress(&mut compressed.as_slice(), &mut decompressed) | ||
| .unwrap(); | ||
| assert_eq!(data, decompressed.as_slice()); | ||
|
sauravzg marked this conversation as resolved.
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems grpc-C++ supports deflate? But I don't think anything supports zstd:
https://github.com/grpc/grpc/blob/dc5bf65c0eae98dd47cf0ba4a8f2468b7708958b/include/grpc/impl/compression_types.h#L63
It might even be best to remove deflate for now unless we have an interop test to confirm they work together.