Skip to content

Commit

Permalink
Unset gw once closed (nytimes#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshartig authored and jprobinson committed Mar 14, 2017
1 parent fb35337 commit 3b0c485
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (w *GzipResponseWriter) Close() error {

err := w.gw.Close()
gzipWriterPools[w.index].Put(w.gw)
w.gw = nil
return err
}

Expand Down
25 changes: 25 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,31 @@ func TestGzipHandlerContentLength(t *testing.T) {
assert.NotEqual(t, b, body)
}

func TestGzipDoubleClose(t *testing.T) {
// reset the pool for the default compression so we can make sure duplicates
// aren't added back by double close
addLevelPool(gzip.DefaultCompression)

handler := GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// call close here and it'll get called again interally by
// NewGzipLevelHandler's handler defer
w.Write([]byte("test"))
w.(io.Closer).Close()
}))

r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)

// the second close shouldn't have added the same writer
// so we pull out 2 writers from the pool and make sure they're different
w1 := gzipWriterPools[poolIndex(gzip.DefaultCompression)].Get()
w2 := gzipWriterPools[poolIndex(gzip.DefaultCompression)].Get()
// assert.NotEqual looks at the value and not the address, so we use regular ==
assert.False(t, w1 == w2)
}

// --------------------------------------------------------------------

func BenchmarkGzipHandler_S2k(b *testing.B) { benchmark(b, false, 2048) }
Expand Down

0 comments on commit 3b0c485

Please sign in to comment.