MCPcopy Create free account
hub / github.com/cortexproject/cortex / decompress

Function decompress

pkg/util/grpcencoding/encoding_test.go:151–175  ·  view source on GitHub ↗

This function was copied from: https://github.com/grpc/grpc-go/blob/70c52915099a3b30848d0cb22e2f8951dd5aed7f/rpc_util.go#L765

(compressor encoding.Compressor, d []byte, maxReceiveMessageSize int)

Source from the content-addressed store, hash-verified

149
150// This function was copied from: https://github.com/grpc/grpc-go/blob/70c52915099a3b30848d0cb22e2f8951dd5aed7f/rpc_util.go#L765
151func decompress(compressor encoding.Compressor, d []byte, maxReceiveMessageSize int) ([]byte, int, error) {
152 dcReader, err := compressor.Decompress(bytes.NewReader(d))
153 if err != nil {
154 return nil, 0, err
155 }
156 if sizer, ok := compressor.(interface {
157 DecompressedSize(compressedBytes []byte) int
158 }); ok {
159 if size := sizer.DecompressedSize(d); size >= 0 {
160 if size > maxReceiveMessageSize {
161 return nil, size, nil
162 }
163 // size is used as an estimate to size the buffer, but we
164 // will read more data if available.
165 // +MinRead so ReadFrom will not reallocate if size is correct.
166 buf := bytes.NewBuffer(make([]byte, 0, size+bytes.MinRead))
167 bytesRead, err := buf.ReadFrom(io.LimitReader(dcReader, int64(maxReceiveMessageSize)+1))
168 return buf.Bytes(), int(bytesRead), err
169 }
170 }
171 // Read from LimitReader with limit max+1. So if the underlying
172 // reader is over limit, the result will be bigger than max.
173 d, err = io.ReadAll(io.LimitReader(dcReader, int64(maxReceiveMessageSize)+1))
174 return d, len(d), err
175}

Callers 1

BenchmarkDecompressFunction · 0.70

Calls 3

BytesMethod · 0.95
DecompressedSizeMethod · 0.80
DecompressMethod · 0.45

Tested by

no test coverage detected