Skip to content

Commit

Permalink
if compression level is not set (0), then use the default compression…
Browse files Browse the repository at this point in the history
… level (-1)
  • Loading branch information
rnishtala-sumo committed Jan 14, 2025
1 parent 621dda0 commit 2e1137c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
17 changes: 9 additions & 8 deletions config/configcompression/compressiontype.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ type CompressionParams struct {
}

const (
TypeGzip Type = "gzip"
TypeZlib Type = "zlib"
TypeDeflate Type = "deflate"
TypeSnappy Type = "snappy"
TypeZstd Type = "zstd"
TypeLz4 Type = "lz4"
typeNone Type = "none"
typeEmpty Type = ""
TypeGzip Type = "gzip"
TypeZlib Type = "zlib"
TypeDeflate Type = "deflate"
TypeSnappy Type = "snappy"
TypeZstd Type = "zstd"
TypeLz4 Type = "lz4"
typeNone Type = "none"
typeEmpty Type = ""
DefaultCompressionLevel = zlib.DefaultCompression
)

// IsCompressed returns false if CompressionType is nil, none, or empty.
Expand Down
3 changes: 0 additions & 3 deletions config/confighttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ README](../configtls/README.md).
- `level`: Configure compression level for `compression` type
- The following are valid combinations of `compression` and `level`
- `gzip`
- NoCompression: `0`
- BestSpeed: `1`
- BestCompression: `9`
- DefaultCompression: `-1`
- `zlib`
- NoCompression: `0`
- BestSpeed: `1`
- BestCompression: `9`
- DefaultCompression: `-1`
- `deflate`
- NoCompression: `0`
- BestSpeed: `1`
- BestCompression: `9`
- DefaultCompression: `-1`
Expand Down
26 changes: 16 additions & 10 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ func TestHTTPClientCompression(t *testing.T) {
{
name: "ValidGzip",
encoding: configcompression.TypeGzip,
level: gzip.BestSpeed,
level: gzip.DefaultCompression,
reqBody: compressedGzipBody.Bytes(),
shouldError: false,
},
{
name: "ValidGzip-DefaultLevel",
encoding: configcompression.TypeGzip,
reqBody: compressedGzipBody.Bytes(),
shouldError: false,
},
Expand All @@ -81,14 +87,14 @@ func TestHTTPClientCompression(t *testing.T) {
{
name: "ValidZlib",
encoding: configcompression.TypeZlib,
level: gzip.BestSpeed,
level: gzip.DefaultCompression,
reqBody: compressedZlibBody.Bytes(),
shouldError: false,
},
{
name: "ValidDeflate",
encoding: configcompression.TypeDeflate,
level: gzip.BestSpeed,
level: gzip.DefaultCompression,
reqBody: compressedDeflateBody.Bytes(),
shouldError: false,
},
Expand All @@ -101,7 +107,7 @@ func TestHTTPClientCompression(t *testing.T) {
{
name: "InvalidSnappy",
encoding: configcompression.TypeSnappy,
level: gzip.BestSpeed,
level: gzip.DefaultCompression,
reqBody: compressedSnappyBody.Bytes(),
shouldError: true,
},
Expand All @@ -121,7 +127,7 @@ func TestHTTPClientCompression(t *testing.T) {
{
name: "InvalidLz4",
encoding: configcompression.TypeLz4,
level: gzip.BestSpeed,
level: gzip.DefaultCompression,
reqBody: compressedLz4Body.Bytes(),
shouldError: true,
},
Expand Down Expand Up @@ -347,7 +353,7 @@ func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
require.NoError(t, err, "failed to create request to test handler")

client := srv.Client()
compressionParams := newCompressionParams(gzip.BestSpeed)
compressionParams := newCompressionParams(gzip.DefaultCompression)
client.Transport, err = newCompressRoundTripper(http.DefaultTransport, configcompression.TypeGzip, compressionParams)
require.NoError(t, err)
res, err := client.Do(req)
Expand All @@ -368,7 +374,7 @@ func TestHTTPContentCompressionCopyError(t *testing.T) {
require.NoError(t, err)

client := srv.Client()
compressionParams := newCompressionParams(gzip.BestSpeed)
compressionParams := newCompressionParams(gzip.DefaultCompression)
client.Transport, err = newCompressRoundTripper(http.DefaultTransport, configcompression.TypeGzip, compressionParams)
require.NoError(t, err)
_, err = client.Do(req)
Expand All @@ -393,7 +399,7 @@ func TestHTTPContentCompressionRequestBodyCloseError(t *testing.T) {
require.NoError(t, err)

client := srv.Client()
compressionParams := newCompressionParams(gzip.BestSpeed)
compressionParams := newCompressionParams(gzip.DefaultCompression)
client.Transport, err = newCompressRoundTripper(http.DefaultTransport, configcompression.TypeGzip, compressionParams)
require.NoError(t, err)
_, err = client.Do(req)
Expand Down Expand Up @@ -495,7 +501,7 @@ func TestDecompressorAvoidDecompressionBomb(t *testing.T) {

func compressGzip(tb testing.TB, body []byte) *bytes.Buffer {
var buf bytes.Buffer
gw, _ := gzip.NewWriterLevel(&buf, gzip.BestSpeed)
gw, _ := gzip.NewWriterLevel(&buf, gzip.DefaultCompression)
_, err := gw.Write(body)
require.NoError(tb, err)
require.NoError(tb, gw.Close())
Expand All @@ -504,7 +510,7 @@ func compressGzip(tb testing.TB, body []byte) *bytes.Buffer {

func compressZlib(tb testing.TB, body []byte) *bytes.Buffer {
var buf bytes.Buffer
zw, _ := zlib.NewWriterLevel(&buf, zlib.BestSpeed)
zw, _ := zlib.NewWriterLevel(&buf, zlib.DefaultCompression)
_, err := zw.Write(body)
require.NoError(tb, err)
require.NoError(tb, zw.Close())
Expand Down
4 changes: 4 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
// Compress the body using specified compression methods if non-empty string is provided.
// Supporting gzip, zlib, deflate, snappy, and zstd; none is treated as uncompressed.
if hcs.Compression.IsCompressed() {
// If the compression level is not set, use the default level.
if hcs.CompressionParams.Level == 0 {
hcs.CompressionParams.Level = configcompression.DefaultCompressionLevel
}
clientTransport, err = newCompressRoundTripper(clientTransport, hcs.Compression, hcs.CompressionParams)
if err != nil {
return nil, err
Expand Down

0 comments on commit 2e1137c

Please sign in to comment.