Skip to content
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

Expose compression quality on the CompressionLayer #333

Merged
merged 9 commits into from
Mar 22, 2023

Conversation

rafaelcaricio
Copy link
Contributor

Motivation

Fixes #245

Solution

Allows setting the compression quality level for responses. The ticket suggested to use .level(the_level), but after reading the full context I've settled with .quality(the_level) as it represents better the underlying feature.

Copy link
Contributor

@erebe erebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

Thank you for the PR :)
I am not a maintainer, but I took a look at it.

Overall, it looks good to me.
I am just nitpicking of the type name of Level, which I think should be more comprehensive into CompressionLevel for the reader.

tower-http/src/compression_utils.rs Outdated Show resolved Hide resolved
@rafaelcaricio
Copy link
Contributor Author

@erebe Thank you for the review! I've applied your suggestion ;)

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks :)

@davidpdrsn
Copy link
Member

@rafaelcaricio do you have time to look into the CI error?

@rafaelcaricio
Copy link
Contributor Author

@davidpdrsn Thank you for the review. Yes, I do have time to fix this today.

Should I ping you here when it is done or just let it be, and you will cycle back when you get some time in the future?

@davidpdrsn
Copy link
Member

I should get a GitHub notification when you push.

@rafaelcaricio
Copy link
Contributor Author

My trial to fix this caused way more errors. Honestly, I don't know what to do here. I need some guidance.

@erebe
Copy link
Contributor

erebe commented Mar 19, 2023

Hello @rafaelcaricio ,

The code/patch below works, the issue come down to CompressionLevel type being needed by both the compression and the decompression code, so it must be visible in all the cases.

I had to make the module compression_utils pub to fix it, it should cause no harm as everything inside is declared pub(crate) (but I am no expert in that). If you don't like it, you need to move CompressionLevel in its own module (i.e: compresion_common)

P.s: to apply the patch at root of the repo, patch -p1 < the_patch_below.patch

From 7e8888bbaf4e72492bda839943e8f30584156b7f Mon Sep 17 00:00:00 2001
From: Romain GERARD <erebe@erebe.eu>
Date: Sun, 19 Mar 2023 13:50:37 +0100
Subject: [PATCH] fix visibility

---
 tower-http/src/compression_utils.rs | 47 +++++++++--------------------
 tower-http/src/lib.rs               |  2 +-
 2 files changed, 16 insertions(+), 33 deletions(-)

diff --git a/tower-http/src/compression_utils.rs b/tower-http/src/compression_utils.rs
index 34f1c36..7b28937 100644
--- a/tower-http/src/compression_utils.rs
+++ b/tower-http/src/compression_utils.rs
@@ -15,14 +15,6 @@ use std::{
 use tokio::io::AsyncRead;
 use tokio_util::io::{poll_read_buf, StreamReader};
 
-#[cfg(any(
-    feature = "compression-br",
-    feature = "compression-deflate",
-    feature = "compression-gzip",
-    feature = "compression-zstd",
-))]
-use async_compression::Level as AsyncCompressionLevel;
-
 #[derive(Debug, Clone, Copy)]
 pub(crate) struct AcceptEncoding {
     pub(crate) gzip: bool,
@@ -345,18 +337,9 @@ where
 pub(crate) const SENTINEL_ERROR_CODE: i32 = -837459418;
 
 /// Level of compression data should be compressed with.
-#[cfg_attr(
-    not(any(
-        feature = "compression-br",
-        feature = "compression-deflate",
-        feature = "compression-gzip",
-        feature = "compression-zstd",
-    )),
-    allow(dead_code)
-)]
 #[non_exhaustive]
 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
-pub(crate) enum CompressionLevel {
+pub enum CompressionLevel {
     /// Fastest quality of compression, usually produces bigger size.
     Fastest,
     /// Best quality of compression, usually produces the smallest size.
@@ -370,27 +353,27 @@ pub(crate) enum CompressionLevel {
     Precise(u32),
 }
 
-#[cfg(any(
-    feature = "compression-br",
-    feature = "compression-deflate",
-    feature = "compression-gzip",
-    feature = "compression-zstd",
-))]
-pub use self::CompressionLevel;
-
 impl Default for CompressionLevel {
     fn default() -> Self {
         CompressionLevel::Default
     }
 }
 
+#[cfg(any(
+    feature = "compression-br",
+    feature = "compression-gzip",
+    feature = "compression-deflate",
+    feature = "compression-zstd"
+))]
+use async_compression::Level as AsyncCompressionLevel;
+
+#[cfg(any(
+    feature = "compression-br",
+    feature = "compression-gzip",
+    feature = "compression-deflate",
+    feature = "compression-zstd"
+))]
 impl CompressionLevel {
-    #[cfg(any(
-        feature = "compression-br",
-        feature = "compression-deflate",
-        feature = "compression-gzip",
-        feature = "compression-zstd",
-    ))]
     pub(crate) fn into_async_compression(self) -> AsyncCompressionLevel {
         match self {
             CompressionLevel::Fastest => AsyncCompressionLevel::Fastest,
diff --git a/tower-http/src/lib.rs b/tower-http/src/lib.rs
index e6c4186..d0ac34d 100644
--- a/tower-http/src/lib.rs
+++ b/tower-http/src/lib.rs
@@ -279,7 +279,7 @@ mod content_encoding;
     feature = "decompression-gzip",
     feature = "decompression-zstd",
 ))]
-mod compression_utils;
+pub mod compression_utils;
 
 #[cfg(feature = "map-response-body")]
 pub mod map_response_body;
-- 
2.39.2

@rafaelcaricio
Copy link
Contributor Author

@erebe Thank you for the guidance here. It solved the issues with CI. 🥳

@@ -279,7 +279,7 @@ mod content_encoding;
feature = "decompression-gzip",
feature = "decompression-zstd",
))]
mod compression_utils;
pub mod compression_utils;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this made public?

Copy link
Contributor

@erebe erebe Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompressionLevel is exposed publicly/in the api interface and is declared in this module.

You can take a look at my previous comment.

The code/patch below works, the issue come down to CompressionLevel type being needed by both the compression and the decompression code, so it must be visible in all the cases.

I had to make the module compression_utils pub to fix it, it should cause no harm as everything inside is declared pub(crate) (but I am no expert in that). If you don't like it, you need to move CompressionLevel in its own module (i.e: compresion_common)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it makes sense that it must be public, but I don't think the module should be. You can maybe put a pub use at the crate root, there's already LatencyUnit there that I think only a few middleware use. cc @davidpdrsn

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did make the change suggested here (pub use) instead of making the whole module public.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I saw, I get notified for pushes ;)

@jplatte jplatte merged commit 4ebe811 into tower-rs:master Mar 22, 2023
@rafaelcaricio rafaelcaricio deleted the expose-compression-quality branch March 22, 2023 14:13
gezihuzi pushed a commit to gezihuzi/tower-http that referenced this pull request Jul 19, 2023
Allows setting the compression quality level for responses.
@i18n-now
Copy link

i18n-now commented Apr 30, 2024

Can I set different compression levels for different compression algorithms?
for example .gzip_with_quality(xxx)

@jplatte
Copy link
Collaborator

jplatte commented Apr 30, 2024

@i18n-now please open a new issue about that.

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.

Expose compression level on CompressionLayer
5 participants