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

Revert "Avoid possible memory leak in compress middleware" #924

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (cw *compressResponseWriter) Push(target string, opts *http.PushOptions) er
}

func (cw *compressResponseWriter) Close() error {
if c, ok := cw.w.(io.WriteCloser); ok {
if c, ok := cw.writer().(io.WriteCloser); ok {
return c.Close()
}
return errors.New("chi/middleware: io.WriteCloser is unavailable on the writer")
Expand Down
46 changes: 2 additions & 44 deletions middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ func TestCompressor(t *testing.T) {
return w
})

var sideEffect int
compressor.SetEncoder("test", func(w io.Writer, _ int) io.Writer {
return newSideEffectWriter(w, &sideEffect)
})

if len(compressor.encoders) != 2 {
t.Errorf("nop and test encoders should be stored in the encoders map")
if len(compressor.encoders) != 1 {
t.Errorf("nop encoder should be stored in the encoders map")
}

r.Use(compressor.Handler)
Expand All @@ -52,11 +47,6 @@ func TestCompressor(t *testing.T) {
w.Write([]byte("textstring"))
})

r.Get("/getimage", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write([]byte("textstring"))
})

ts := httptest.NewServer(r)
defer ts.Close()

Expand Down Expand Up @@ -103,12 +93,6 @@ func TestCompressor(t *testing.T) {
acceptedEncodings: []string{"nop, gzip, deflate"},
expectedEncoding: "nop",
},
{
name: "test is used and side effect is cleared after close",
path: "/getimage",
acceptedEncodings: []string{"test"},
expectedEncoding: "",
},
}

for _, tc := range tests {
Expand All @@ -123,10 +107,7 @@ func TestCompressor(t *testing.T) {
}

})
}

if sideEffect > 1 {
t.Errorf("side effect should be cleared after close")
}
}

Expand Down Expand Up @@ -236,26 +217,3 @@ func decodeResponseBody(t *testing.T, resp *http.Response) string {

return string(respBody)
}

type (
sideEffectWriter struct {
w io.Writer
s *int
}
)

func newSideEffectWriter(w io.Writer, sideEffect *int) io.Writer {
*sideEffect = *sideEffect + 1

return &sideEffectWriter{w: w, s: sideEffect}
}

func (w *sideEffectWriter) Write(p []byte) (n int, err error) {
return w.w.Write(p)
}

func (w *sideEffectWriter) Close() error {
*w.s = *w.s - 1

return nil
}