Skip to content

Commit 33a1648

Browse files
committed
vtprotocodec: user codecV2 for grpc client/server
Switch to use codecV2 backed by the default buffer pool to handle all Marshaling/Unmarshaling by grpc clients/servers. The default buffer pool is already setup here. ``` server/util/grpc_client/grpc_client.go 254: experimental.WithBufferPool(mem.DefaultBufferPool()), server/util/grpc_server/grpc_server.go 240: experimental.BufferPool(mem.DefaultBufferPool()), ``` This does not affect the manual Marshaling/Unmarshaling that we does via `server/util/proto.{Marshal,Unmarshal}()`. These should still use V1 for the time being.
1 parent b270096 commit 33a1648

File tree

1 file changed

+4
-27
lines changed

1 file changed

+4
-27
lines changed

server/util/vtprotocodec/vtprotocodec.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package vtprotocodec
22

33
import (
4-
"fmt"
5-
64
"github.com/buildbuddy-io/buildbuddy/server/util/proto"
75

86
"google.golang.org/grpc/encoding"
@@ -13,30 +11,10 @@ import (
1311

1412
const Name = "proto"
1513

16-
// vtprotoCodec represents a codec able to encode and decode vt enabled
17-
// proto messages.
18-
type vtprotoCodec struct{}
19-
20-
func (vtprotoCodec) Marshal(v any) ([]byte, error) {
21-
vv, ok := v.(proto.Message)
22-
if !ok {
23-
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
24-
}
25-
return proto.Marshal(vv)
26-
}
27-
28-
func (vtprotoCodec) Unmarshal(data []byte, v any) error {
29-
vv, ok := v.(proto.Message)
30-
if !ok {
31-
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
32-
}
33-
return proto.Unmarshal(data, vv)
34-
}
35-
36-
func (vtprotoCodec) Name() string {
37-
return Name
38-
}
39-
14+
// vtprotoCodecV2 implements encoding.CodecV2 and uses vtproto and default buffer pool
15+
// to encode/decode proto messages. The implementation is heavily inspired by
16+
// https://github.com/planetscale/vtprotobuf/pull/138
17+
// and https://github.com/vitessio/vitess/pull/16790.
4018
type vtprotoCodecV2 struct {
4119
fallback encoding.CodecV2
4220
}
@@ -80,7 +58,6 @@ func (c *vtprotoCodecV2) Unmarshal(data mem.BufferSlice, v any) error {
8058
// RegisterCodec registers the vtprotoCodec to encode/decode proto messages with
8159
// all gRPC clients and servers.
8260
func Register() {
83-
// encoding.RegisterCodec(vtprotoCodec{})
8461
encoding.RegisterCodecV2(&vtprotoCodecV2{
8562
fallback: encoding.GetCodecV2("proto"),
8663
})

0 commit comments

Comments
 (0)