-
Notifications
You must be signed in to change notification settings - Fork 128
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
Refactor out gzipwriter interface #106
Open
whs
wants to merge
3
commits into
nytimes:master
Choose a base branch
from
wongnai:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package writer | ||
|
||
import "io" | ||
|
||
type GzipWriter interface { | ||
Close() error | ||
Flush() error | ||
Write(p []byte) (int, error) | ||
} | ||
|
||
type GzipWriterFactory = func(writer io.Writer, level int) GzipWriter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package stdlib | ||
whs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"compress/gzip" | ||
"github.com/NYTimes/gziphandler/writer" | ||
"io" | ||
"sync" | ||
) | ||
|
||
// gzipWriterPools stores a sync.Pool for each compression level for reuse of | ||
// gzip.Writers. Use poolIndex to covert a compression level to an index into | ||
// gzipWriterPools. | ||
var gzipWriterPools [gzip.BestCompression - gzip.BestSpeed + 2]*sync.Pool | ||
|
||
func init() { | ||
for i := gzip.BestSpeed; i <= gzip.BestCompression; i++ { | ||
addLevelPool(i) | ||
} | ||
addLevelPool(gzip.DefaultCompression) | ||
} | ||
|
||
// poolIndex maps a compression level to its index into gzipWriterPools. It | ||
// assumes that level is a valid gzip compression level. | ||
func poolIndex(level int) int { | ||
// gzip.DefaultCompression == -1, so we need to treat it special. | ||
if level == gzip.DefaultCompression { | ||
return gzip.BestCompression - gzip.BestSpeed + 1 | ||
} | ||
return level - gzip.BestSpeed | ||
} | ||
|
||
func addLevelPool(level int) { | ||
gzipWriterPools[poolIndex(level)] = &sync.Pool{ | ||
New: func() interface{} { | ||
// NewWriterLevel only returns error on a bad level, we are guaranteeing | ||
// that this will be a valid level so it is okay to ignore the returned | ||
// error. | ||
w, _ := gzip.NewWriterLevel(nil, level) | ||
return w | ||
}, | ||
} | ||
} | ||
|
||
type pooledWriter struct { | ||
*gzip.Writer | ||
index int | ||
} | ||
|
||
func (pw *pooledWriter) Close() error { | ||
err := pw.Writer.Close() | ||
gzipWriterPools[pw.index].Put(pw.Writer) | ||
pw.Writer = nil | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing (to match the original gzip.go line 243): pw.Writer = nil This will allow to be future-proof against double-close or use-after-close. |
||
} | ||
|
||
func NewWriter(w io.Writer, level int) writer.GzipWriter { | ||
index := poolIndex(level) | ||
gzw := gzipWriterPools[index].Get().(*gzip.Writer) | ||
gzw.Reset(w) | ||
return &pooledWriter{ | ||
Writer: gzw, | ||
index: index, | ||
} | ||
} | ||
|
||
func ImplementationInfo() string { | ||
return "compress/zlib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package stdlib | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
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) | ||
|
||
w := bytes.NewBufferString("") | ||
writer := NewWriter(w, gzip.DefaultCompression) | ||
writer.Close() | ||
|
||
// 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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add documentation above.
Add a link to the default implementation.