-
Notifications
You must be signed in to change notification settings - Fork 2
/
compressor.go
64 lines (52 loc) · 1.58 KB
/
compressor.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package compressor
import (
"context"
"github.com/americanas-go/log"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
)
// Register registers a new compressor plugin for grpc client.
func Register(ctx context.Context) ([]grpc.DialOption, []grpc.CallOption) {
o, err := NewOptions()
if err != nil {
return nil, nil
}
h := NewCompressorWithOptions(o)
return h.Register(ctx)
}
// Compressor represents compressor plugin for grpc client.
type Compressor struct {
options *Options
}
// NewCompressorWithOptions returns a new compressor plugin with options.
func NewCompressorWithOptions(options *Options) *Compressor {
return &Compressor{options: options}
}
// NewCompressorWithConfigPath returns a new compressor plugin with options from config path.
func NewCompressorWithConfigPath(path string) (*Compressor, error) {
o, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewCompressorWithOptions(o), nil
}
// NewCompressor returns a new compressor plugin with default options.
func NewCompressor() *Compressor {
o, err := NewOptions()
if err != nil {
log.Fatalf(err.Error())
}
return NewCompressorWithOptions(o)
}
// Register registers this compressor plugin for grpc client.
func (i *Compressor) Register(ctx context.Context) ([]grpc.DialOption, []grpc.CallOption) {
logger := log.FromContext(ctx)
logger.Debug("compressor successfully enabled in grpc client")
err := gzip.SetLevel(i.options.Level)
if err != nil {
logger.Fatalf("could not set level: %s", err.Error())
}
return nil, []grpc.CallOption{
grpc.UseCompressor(gzip.Name),
}
}