-
Notifications
You must be signed in to change notification settings - Fork 816
Support Snappy compression for gRPC #2940
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
270a06d
Support Snappy compression for gRPC
Wing924 5df98c9
changelog
Wing924 835a6d1
Merge branch 'master' into support_snappy_grpc
Wing924 5dd90d3
fix
Wing924 c775a23
Merge branch 'master' into support_snappy_grpc
Wing924 6cae329
fix
Wing924 87bcb12
fix
Wing924 0f7a209
fix
Wing924 97bd88d
fix
Wing924 0978fec
Merge branch 'master' into support_snappy_grpc
Wing924 2f15674
fix
Wing924 47d77a0
Fix
Wing924 6b4b2bd
Merge branch 'support_snappy_grpc' of github.com:Wing924/cortex into …
Wing924 fee4259
Merge branch 'master' into support_snappy_grpc
Wing924 88d34a5
fix
Wing924 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
package snappy | ||
pstibrany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
"github.com/golang/snappy" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
// Name is the name registered for the snappy compressor. | ||
const Name = "snappy" | ||
|
||
func init() { | ||
encoding.RegisterCompressor(newCompressor()) | ||
} | ||
|
||
type compressor struct { | ||
pracucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
writersPool sync.Pool | ||
Wing924 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readersPool sync.Pool | ||
} | ||
|
||
func newCompressor() *compressor { | ||
c := &compressor{} | ||
c.readersPool = sync.Pool{ | ||
New: func() interface{} { | ||
return snappy.NewReader(nil) | ||
}, | ||
} | ||
c.writersPool = sync.Pool{ | ||
New: func() interface{} { | ||
return snappy.NewBufferedWriter(nil) | ||
}, | ||
} | ||
return c | ||
} | ||
|
||
func (c *compressor) Name() string { | ||
return Name | ||
} | ||
|
||
func (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) { | ||
wr := c.writersPool.Get().(*snappy.Writer) | ||
wr.Reset(w) | ||
return writeCloser{wr, &c.writersPool}, nil | ||
} | ||
|
||
func (c *compressor) Decompress(r io.Reader) (io.Reader, error) { | ||
dr := c.readersPool.Get().(*snappy.Reader) | ||
dr.Reset(r) | ||
return reader{dr, &c.readersPool}, nil | ||
} | ||
|
||
type writeCloser struct { | ||
*snappy.Writer | ||
pstibrany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pool *sync.Pool | ||
} | ||
|
||
func (w writeCloser) Close() error { | ||
defer func() { | ||
Wing924 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
w.Writer.Reset(nil) | ||
w.pool.Put(w.Writer) | ||
}() | ||
|
||
if w.Writer != nil { | ||
return w.Writer.Close() | ||
} | ||
return nil | ||
} | ||
|
||
type reader struct { | ||
reader *snappy.Reader | ||
pool *sync.Pool | ||
} | ||
|
||
func (r reader) Read(p []byte) (n int, err error) { | ||
n, err = r.reader.Read(p) | ||
if err == io.EOF { | ||
r.reader.Reset(nil) | ||
r.pool.Put(r.reader) | ||
} | ||
return n, err | ||
} |
This file contains hidden or 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,71 @@ | ||
package snappy | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSnappy(t *testing.T) { | ||
c := newCompressor() | ||
assert.Equal(t, "snappy", c.Name()) | ||
|
||
tests := []struct { | ||
test string | ||
input string | ||
}{ | ||
{"empty", ""}, | ||
{"short", "hello world"}, | ||
{"long", strings.Repeat("123456789", 1024)}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.test, func(t *testing.T) { | ||
var buf bytes.Buffer | ||
// Compress | ||
w, err := c.Compress(&buf) | ||
require.NoError(t, err) | ||
n, err := w.Write([]byte(test.input)) | ||
require.NoError(t, err) | ||
assert.Len(t, test.input, n) | ||
err = w.Close() | ||
require.NoError(t, err) | ||
// Decompress | ||
r, err := c.Decompress(&buf) | ||
require.NoError(t, err) | ||
out, err := ioutil.ReadAll(r) | ||
require.NoError(t, err) | ||
assert.Equal(t, test.input, string(out)) | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkSnappyCompress(b *testing.B) { | ||
data := []byte(strings.Repeat("123456789", 1024)) | ||
c := newCompressor() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
w, _ := c.Compress(ioutil.Discard) | ||
_, _ = w.Write(data) | ||
_ = w.Close() | ||
} | ||
} | ||
|
||
func BenchmarkSnappyDecompress(b *testing.B) { | ||
data := []byte(strings.Repeat("123456789", 1024)) | ||
c := newCompressor() | ||
var buf bytes.Buffer | ||
w, _ := c.Compress(&buf) | ||
_, _ = w.Write(data) | ||
reader := bytes.NewReader(buf.Bytes()) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
r, _ := c.Decompress(reader) | ||
_, _ = ioutil.ReadAll(r) | ||
_, _ = reader.Seek(0, io.SeekStart) | ||
} | ||
} |
This file contains hidden or 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
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.