| 1 |
// Copyright 2020 Mostyn Bramley-Moore. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package zstd is a wrapper for using github.com/klauspost/compress/zstd |
| 16 |
// with gRPC. |
| 17 |
package zstd |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"errors" |
| 22 |
"io" |
| 23 |
"runtime" |
| 24 |
"sync" |
| 25 |
|
| 26 |
"github.com/klauspost/compress/zstd" |
| 27 |
"google.golang.org/grpc/encoding" |
| 28 |
) |
| 29 |
|
| 30 |
const Name = "zstd" |
| 31 |
|
| 32 |
var encoderOptions = []zstd.EOption{ |
| 33 |
// The default zstd window size is 8MB, which is much larger than the |
| 34 |
// typical RPC message and wastes a bunch of memory. |
| 35 |
zstd.WithWindowSize(512 * 1024), |
| 36 |
} |
| 37 |
|
| 38 |
var decoderOptions = []zstd.DOption{ |
| 39 |
// If the decoder concurrency level is not 1, we would need to call |
| 40 |
// Close() to avoid leaking resources when the object is released |
| 41 |
// from compressor.decoderPool. |
| 42 |
zstd.WithDecoderConcurrency(1), |
| 43 |
} |
| 44 |
|
| 45 |
// We will set a finalizer on these objects, so when the go-grpc code is |
| 46 |
// finished with them, they will be added back to compressor.decoderPool. |
| 47 |
type decoderWrapper struct { |
| 48 |
*zstd.Decoder |
| 49 |
} |
| 50 |
|
| 51 |
type compressor struct { |
| 52 |
encoder *zstd.Encoder |
| 53 |
decoderPool sync.Pool // To hold *zstd.Decoder's. |
| 54 |
} |
| 55 |
|
| 56 |
func PretendInit(clobbering bool) { |
| 57 |
if !clobbering && encoding.GetCompressor(Name) != nil { |
| 58 |
return |
| 59 |
} |
| 60 |
|
| 61 |
enc, _ := zstd.NewWriter(nil, encoderOptions...) |
| 62 |
c := &compressor{ |
| 63 |
encoder: enc, |
| 64 |
} |
| 65 |
encoding.RegisterCompressor(c) |
| 66 |
} |
| 67 |
|
| 68 |
var ErrNotInUse = errors.New("SetLevel ineffective because another zstd compressor has been registered") |
| 69 |
|
| 70 |
// SetLevel updates the registered compressor to use a particular compression |
| 71 |
// level. NOTE: this function must only be called from an init function, and |
| 72 |
// is not threadsafe. |
| 73 |
func SetLevel(level zstd.EncoderLevel) error { |
| 74 |
c, ok := encoding.GetCompressor(Name).(*compressor) |
| 75 |
if !ok { |
| 76 |
return ErrNotInUse |
| 77 |
} |
| 78 |
|
| 79 |
enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(level)) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
|
| 84 |
c.encoder = enc |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
func (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) { |
| 89 |
return &zstdWriteCloser{ |
| 90 |
enc: c.encoder, |
| 91 |
writer: w, |
| 92 |
}, nil |
| 93 |
} |
| 94 |
|
| 95 |
type zstdWriteCloser struct { |
| 96 |
enc *zstd.Encoder |
| 97 |
writer io.Writer // Compressed data will be written here. |
| 98 |
buf bytes.Buffer // Buffer uncompressed data here, compress on Close. |
| 99 |
} |
| 100 |
|
| 101 |
func (z *zstdWriteCloser) Write(p []byte) (int, error) { |
| 102 |
return z.buf.Write(p) |
| 103 |
} |
| 104 |
|
| 105 |
func (z *zstdWriteCloser) Close() error { |
| 106 |
compressed := z.enc.EncodeAll(z.buf.Bytes(), nil) |
| 107 |
_, err := io.Copy(z.writer, bytes.NewReader(compressed)) |
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
func (c *compressor) Decompress(r io.Reader) (io.Reader, error) { |
| 112 |
var err error |
| 113 |
var found bool |
| 114 |
var decoder *zstd.Decoder |
| 115 |
|
| 116 |
// Note: avoid the use of zstd.Decoder.DecodeAll here, since |
| 117 |
// malicious payloads could DoS us with a decompression bomb. |
| 118 |
|
| 119 |
decoder, found = c.decoderPool.Get().(*zstd.Decoder) |
| 120 |
if !found { |
| 121 |
decoder, err = zstd.NewReader(r, decoderOptions...) |
| 122 |
if err != nil { |
| 123 |
return nil, err |
| 124 |
} |
| 125 |
} else { |
| 126 |
err = decoder.Reset(r) |
| 127 |
if err != nil { |
| 128 |
c.decoderPool.Put(decoder) |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
wrapper := &decoderWrapper{Decoder: decoder} |
| 134 |
runtime.SetFinalizer(wrapper, func(dw *decoderWrapper) { |
| 135 |
err := dw.Reset(nil) |
| 136 |
if err == nil { |
| 137 |
c.decoderPool.Put(dw.Decoder) |
| 138 |
} |
| 139 |
}) |
| 140 |
|
| 141 |
return wrapper, nil |
| 142 |
} |
| 143 |
|
| 144 |
func (c *compressor) Name() string { |
| 145 |
return Name |
| 146 |
} |