-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect_brotli.go
45 lines (36 loc) · 1.23 KB
/
connect_brotli.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
package brotli
import (
"connectrpc.com/connect"
"github.com/andybalholm/brotli"
)
const (
BestSpeed = brotli.BestSpeed
BestCompression = brotli.BestCompression
DefaultCompression = brotli.DefaultCompression
)
const Name = "br"
// WithCompression returns client and handler options for the brotli compression method
// using the default compression level.
func WithCompression() connect.Option {
return WithCompressionLevel(DefaultCompression)
}
// WithCompressionLevel returns client and handler options for the brotli compression
// method for your prefered compression level.
func WithCompressionLevel(level int) connect.Option {
d, c := brComp(level)
return compressorOption{
ClientOption: connect.WithAcceptCompression(Name, d, c),
HandlerOption: connect.WithCompression(Name, d, c),
}
}
func brComp(level int) (func() connect.Decompressor, func() connect.Compressor) {
d := func() connect.Decompressor { return &brrWrapper{brotli.NewReader(nil)} }
c := func() connect.Compressor { return brotli.NewWriterLevel(nil, level) }
return d, c
}
type compressorOption struct {
connect.ClientOption
connect.HandlerOption
}
type brrWrapper struct{ *brotli.Reader }
func (b *brrWrapper) Close() error { return b.Reset(nil) }