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

Method Unmarshal

pkg/cortexpb/codec.go:97–127  ·  view source on GitHub ↗

Unmarshal Copied from https://github.com/grpc/grpc-go/blob/d2e836604b36400a54fbf04af495d12b38fa1e3a/encoding/proto/proto.go#L69-L81 but without releasing the buffer

(data mem.BufferSlice, v any)

Source from the content-addressed store, hash-verified

95// Unmarshal Copied from https://github.com/grpc/grpc-go/blob/d2e836604b36400a54fbf04af495d12b38fa1e3a/encoding/proto/proto.go#L69-L81
96// but without releasing the buffer
97func (c *cortexCodec) Unmarshal(data mem.BufferSlice, v any) error {
98 vv := messageV2Of(v)
99 if vv == nil {
100 return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
101 }
102
103 // To be safe, we avoid automatically releasing the buffer used to unmarshal the message.
104 // Additionally, we avoid using a pooled byte slice unless the message implements ReleasableMessage.
105 // This mimics the behavior of gRPC versions 1.65.0 and earlier.
106 rm, ok := v.(ReleasableMessage)
107 bufferPool := c.defaultBufferPool
108
109 if !ok {
110 bufferPool = c.noOpBufferPool
111 }
112
113 buf := data.MaterializeToBuffer(bufferPool)
114 err := proto.Unmarshal(buf.ReadOnlyData(), vv)
115
116 if err != nil {
117 defer buf.Free()
118 return err
119 }
120
121 // If v implements ReleasableMessage interface, we add the buff to be freed later when the request is no longer being used
122 if rm != nil {
123 rm.RegisterBuffer(buf)
124 }
125
126 return err
127}
128
129func messageV2Of(v any) proto.Message {
130 switch v := v.(type) {

Calls 3

messageV2OfFunction · 0.85
FreeMethod · 0.80
RegisterBufferMethod · 0.65

Tested by 1