Implement customizable minimum body size for compress middleware
This commit is contained in:
parent
8f0832d340
commit
07a3c37a23
13 changed files with 134 additions and 9 deletions
|
@ -104,6 +104,7 @@ type CircuitBreaker struct {
|
|||
// Compress holds the compress configuration.
|
||||
type Compress struct {
|
||||
ExcludedContentTypes []string `json:"excludedContentTypes,omitempty" toml:"excludedContentTypes,omitempty" yaml:"excludedContentTypes,omitempty" export:"true"`
|
||||
MinResponseBodyBytes int `json:"minResponseBodyBytes,omitempty" toml:"minResponseBodyBytes,omitempty" yaml:"minResponseBodyBytes,omitempty" export:"true"`
|
||||
}
|
||||
|
||||
// +k8s:deepcopy-gen=true
|
||||
|
|
|
@ -126,7 +126,7 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||
"traefik.http.middlewares.Middleware16.retry.initialinterval": "1s",
|
||||
"traefik.http.middlewares.Middleware17.stripprefix.prefixes": "foobar, fiibar",
|
||||
"traefik.http.middlewares.Middleware18.stripprefixregex.regex": "foobar, fiibar",
|
||||
"traefik.http.middlewares.Middleware19.compress": "true",
|
||||
"traefik.http.middlewares.Middleware19.compress.minresponsebodybytes": "42",
|
||||
"traefik.http.middlewares.Middleware20.plugin.tomato.aaa": "foo1",
|
||||
"traefik.http.middlewares.Middleware20.plugin.tomato.bbb": "foo2",
|
||||
"traefik.http.routers.Router0.entrypoints": "foobar, fiibar",
|
||||
|
@ -454,7 +454,9 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||
},
|
||||
},
|
||||
"Middleware19": {
|
||||
Compress: &dynamic.Compress{},
|
||||
Compress: &dynamic.Compress{
|
||||
MinResponseBodyBytes: 42,
|
||||
},
|
||||
},
|
||||
"Middleware2": {
|
||||
Buffering: &dynamic.Buffering{
|
||||
|
@ -932,7 +934,9 @@ func TestEncodeConfiguration(t *testing.T) {
|
|||
},
|
||||
},
|
||||
"Middleware19": {
|
||||
Compress: &dynamic.Compress{},
|
||||
Compress: &dynamic.Compress{
|
||||
MinResponseBodyBytes: 42,
|
||||
},
|
||||
},
|
||||
"Middleware2": {
|
||||
Buffering: &dynamic.Buffering{
|
||||
|
@ -1270,7 +1274,7 @@ func TestEncodeConfiguration(t *testing.T) {
|
|||
"traefik.HTTP.Middlewares.Middleware17.StripPrefix.Prefixes": "foobar, fiibar",
|
||||
"traefik.HTTP.Middlewares.Middleware17.StripPrefix.ForceSlash": "true",
|
||||
"traefik.HTTP.Middlewares.Middleware18.StripPrefixRegex.Regex": "foobar, fiibar",
|
||||
"traefik.HTTP.Middlewares.Middleware19.Compress": "true",
|
||||
"traefik.HTTP.Middlewares.Middleware19.Compress.MinResponseBodyBytes": "42",
|
||||
"traefik.HTTP.Middlewares.Middleware20.Plugin.tomato.aaa": "foo1",
|
||||
"traefik.HTTP.Middlewares.Middleware20.Plugin.tomato.bbb": "foo2",
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ type compress struct {
|
|||
next http.Handler
|
||||
name string
|
||||
excludes []string
|
||||
minSize int
|
||||
}
|
||||
|
||||
// New creates a new compress middleware.
|
||||
|
@ -39,7 +40,12 @@ func New(ctx context.Context, next http.Handler, conf dynamic.Compress, name str
|
|||
excludes = append(excludes, mediaType)
|
||||
}
|
||||
|
||||
return &compress{next: next, name: name, excludes: excludes}, nil
|
||||
minSize := gzhttp.DefaultMinSize
|
||||
if conf.MinResponseBodyBytes > 0 {
|
||||
minSize = conf.MinResponseBodyBytes
|
||||
}
|
||||
|
||||
return &compress{next: next, name: name, excludes: excludes, minSize: minSize}, nil
|
||||
}
|
||||
|
||||
func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
|
@ -64,7 +70,7 @@ func (c *compress) gzipHandler(ctx context.Context) http.Handler {
|
|||
wrapper, err := gzhttp.NewWrapper(
|
||||
gzhttp.ExceptContentTypes(c.excludes),
|
||||
gzhttp.CompressionLevel(gzip.DefaultCompression),
|
||||
gzhttp.MinSize(gzhttp.DefaultMinSize))
|
||||
gzhttp.MinSize(c.minSize))
|
||||
if err != nil {
|
||||
log.FromContext(ctx).Error(err)
|
||||
}
|
||||
|
|
|
@ -305,6 +305,57 @@ func TestIntegrationShouldCompress(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMinResponseBodyBytes(t *testing.T) {
|
||||
fakeBody := generateBytes(100000)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
minResponseBodyBytes int
|
||||
expectedCompression bool
|
||||
}{
|
||||
{
|
||||
name: "should compress",
|
||||
expectedCompression: true,
|
||||
},
|
||||
{
|
||||
name: "should not compress",
|
||||
minResponseBodyBytes: 100001,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if _, err := rw.Write(fakeBody); err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
|
||||
handler, err := New(context.Background(), next, dynamic.Compress{MinResponseBodyBytes: test.minResponseBodyBytes}, "testing")
|
||||
require.NoError(t, err)
|
||||
|
||||
rw := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rw, req)
|
||||
|
||||
if test.expectedCompression {
|
||||
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
||||
assert.NotEqualValues(t, rw.Body.Bytes(), fakeBody)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
||||
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCompress(b *testing.B) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
|
|
@ -196,7 +196,7 @@ func Test_buildConfiguration(t *testing.T) {
|
|||
"traefik/http/middlewares/Middleware02/buffering/retryExpression": "foobar",
|
||||
"traefik/http/middlewares/Middleware02/buffering/maxRequestBodyBytes": "42",
|
||||
"traefik/http/middlewares/Middleware02/buffering/memRequestBodyBytes": "42",
|
||||
"traefik/http/middlewares/Middleware05/compress": "",
|
||||
"traefik/http/middlewares/Middleware05/compress/minResponseBodyBytes": "42",
|
||||
"traefik/http/middlewares/Middleware18/retry/attempts": "42",
|
||||
"traefik/http/middlewares/Middleware19/stripPrefix/prefixes/0": "foobar",
|
||||
"traefik/http/middlewares/Middleware19/stripPrefix/prefixes/1": "foobar",
|
||||
|
@ -395,7 +395,9 @@ func Test_buildConfiguration(t *testing.T) {
|
|||
},
|
||||
},
|
||||
"Middleware05": {
|
||||
Compress: &dynamic.Compress{},
|
||||
Compress: &dynamic.Compress{
|
||||
MinResponseBodyBytes: 42,
|
||||
},
|
||||
},
|
||||
"Middleware08": {
|
||||
ForwardAuth: &dynamic.ForwardAuth{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue