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

Use snappy compression for internal gRPC communication. #3368

Merged
merged 5 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion conn/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ func newPool(addr string) (*Pool, error) {
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(x.GrpcMaxSize),
grpc.MaxCallSendMsgSize(x.GrpcMaxSize)),
grpc.MaxCallSendMsgSize(x.GrpcMaxSize),
grpc.UseCompressor((snappyCompressor{}).Name())),
grpc.WithBackoffMaxDelay(time.Second),
grpc.WithInsecure())
if err != nil {
Expand Down
81 changes: 81 additions & 0 deletions conn/snappy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package conn

import (
"io"
"sync"

"github.com/golang/snappy"
"google.golang.org/grpc/encoding"
)

// NB: The encoding.Compressor implementation needs to be goroutine
// safe as multiple goroutines may be using the same compressor for
// different streams on the same connection.
var snappyWriterPool sync.Pool
var snappyReaderPool sync.Pool

type snappyWriter struct {
*snappy.Writer
}

func (w *snappyWriter) Close() error {
defer snappyWriterPool.Put(w)
return w.Writer.Close()
}

type snappyReader struct {
*snappy.Reader
}

func (r *snappyReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
if err == io.EOF {
snappyReaderPool.Put(r)
}
return n, err
}

type snappyCompressor struct {
}

func (snappyCompressor) Name() string {
return "snappy"
}

func (snappyCompressor) Compress(w io.Writer) (io.WriteCloser, error) {
sw, ok := snappyWriterPool.Get().(*snappyWriter)
if !ok {
sw = &snappyWriter{snappy.NewBufferedWriter(w)}
} else {
sw.Reset(w)
}
return sw, nil
}

func (snappyCompressor) Decompress(r io.Reader) (io.Reader, error) {
sr, ok := snappyReaderPool.Get().(*snappyReader)
if !ok {
sr = &snappyReader{snappy.NewReader(r)}
} else {
sr.Reset(r)
}
return sr, nil
}

func init() {
encoding.RegisterCompressor(snappyCompressor{})
}
15 changes: 15 additions & 0 deletions vendor/github.com/golang/snappy/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions vendor/github.com/golang/snappy/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/golang/snappy/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions vendor/github.com/golang/snappy/README

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading