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)
| 95 | // Unmarshal Copied from https://github.com/grpc/grpc-go/blob/d2e836604b36400a54fbf04af495d12b38fa1e3a/encoding/proto/proto.go#L69-L81 |
| 96 | // but without releasing the buffer |
| 97 | func (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 | |
| 129 | func messageV2Of(v any) proto.Message { |
| 130 | switch v := v.(type) { |