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

Function decompressFromBuffer

pkg/util/http.go:233–259  ·  view source on GitHub ↗
(buffer *bytes.Buffer, maxSize int, compression CompressionType, sp opentracing.Span)

Source from the content-addressed store, hash-verified

231}
232
233func decompressFromBuffer(buffer *bytes.Buffer, maxSize int, compression CompressionType, sp opentracing.Span) ([]byte, error) {
234 if len(buffer.Bytes()) > maxSize {
235 return nil, fmt.Errorf(messageSizeLargerErrFmt, len(buffer.Bytes()), maxSize)
236 }
237 switch compression {
238 case NoCompression:
239 return buffer.Bytes(), nil
240 case RawSnappy:
241 if sp != nil {
242 sp.LogFields(otlog.String("event", "util.ParseProtoRequest[decompress]"),
243 otlog.Int("size", len(buffer.Bytes())))
244 }
245 size, err := snappy.DecodedLen(buffer.Bytes())
246 if err != nil {
247 return nil, err
248 }
249 if size > maxSize {
250 return nil, fmt.Errorf(messageSizeLargerErrFmt, size, maxSize)
251 }
252 body, err := snappy.Decode(nil, buffer.Bytes())
253 if err != nil {
254 return nil, err
255 }
256 return body, nil
257 }
258 return nil, nil
259}
260
261// tryBufferFromReader attempts to cast the reader to a `*bytes.Buffer` this is possible when using httpgrpc.
262// If it fails it will return nil and false.

Callers 2

decompressRequestFunction · 0.85
decompressFromReaderFunction · 0.85

Calls 4

LogFieldsMethod · 0.80
BytesMethod · 0.65
StringMethod · 0.65
DecodeMethod · 0.65

Tested by

no test coverage detected