forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snappy.go
46 lines (36 loc) · 1.07 KB
/
snappy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package grpcclient
import (
"flag"
"io"
"github.com/golang/snappy"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
)
var (
compression = flag.String("grpc_compression", "", "how to compress gRPC, default: nothing, supported: snappy")
)
// SnappyCompressor is a gRPC compressor using the Snappy algorithm.
type SnappyCompressor struct{}
// Name is "snappy"
func (s SnappyCompressor) Name() string {
return "snappy"
}
// Compress wraps with a SnappyReader
func (s SnappyCompressor) Compress(w io.Writer) (io.WriteCloser, error) {
return snappy.NewBufferedWriter(w), nil
}
// Decompress wraps with a SnappyReader
func (s SnappyCompressor) Decompress(r io.Reader) (io.Reader, error) {
return snappy.NewReader(r), nil
}
func appendCompression(opts []grpc.DialOption) ([]grpc.DialOption, error) {
if *compression == "snappy" {
compression := grpc.WithDefaultCallOptions(grpc.UseCompressor("snappy"))
opts = append(opts, compression)
}
return opts, nil
}
func init() {
encoding.RegisterCompressor(SnappyCompressor{})
RegisterGRPCDialOptions(appendCompression)
}