diff options
Diffstat (limited to 'vendor/google.golang.org/grpc/codec.go')
-rw-r--r-- | vendor/google.golang.org/grpc/codec.go | 88 |
1 files changed, 17 insertions, 71 deletions
diff --git a/vendor/google.golang.org/grpc/codec.go b/vendor/google.golang.org/grpc/codec.go index 905b048..1297765 100644 --- a/vendor/google.golang.org/grpc/codec.go +++ b/vendor/google.golang.org/grpc/codec.go | |||
@@ -19,86 +19,32 @@ | |||
19 | package grpc | 19 | package grpc |
20 | 20 | ||
21 | import ( | 21 | import ( |
22 | "math" | 22 | "google.golang.org/grpc/encoding" |
23 | "sync" | 23 | _ "google.golang.org/grpc/encoding/proto" // to register the Codec for "proto" |
24 | |||
25 | "github.com/golang/protobuf/proto" | ||
26 | ) | 24 | ) |
27 | 25 | ||
26 | // baseCodec contains the functionality of both Codec and encoding.Codec, but | ||
27 | // omits the name/string, which vary between the two and are not needed for | ||
28 | // anything besides the registry in the encoding package. | ||
29 | type baseCodec interface { | ||
30 | Marshal(v interface{}) ([]byte, error) | ||
31 | Unmarshal(data []byte, v interface{}) error | ||
32 | } | ||
33 | |||
34 | var _ baseCodec = Codec(nil) | ||
35 | var _ baseCodec = encoding.Codec(nil) | ||
36 | |||
28 | // Codec defines the interface gRPC uses to encode and decode messages. | 37 | // Codec defines the interface gRPC uses to encode and decode messages. |
29 | // Note that implementations of this interface must be thread safe; | 38 | // Note that implementations of this interface must be thread safe; |
30 | // a Codec's methods can be called from concurrent goroutines. | 39 | // a Codec's methods can be called from concurrent goroutines. |
40 | // | ||
41 | // Deprecated: use encoding.Codec instead. | ||
31 | type Codec interface { | 42 | type Codec interface { |
32 | // Marshal returns the wire format of v. | 43 | // Marshal returns the wire format of v. |
33 | Marshal(v interface{}) ([]byte, error) | 44 | Marshal(v interface{}) ([]byte, error) |
34 | // Unmarshal parses the wire format into v. | 45 | // Unmarshal parses the wire format into v. |
35 | Unmarshal(data []byte, v interface{}) error | 46 | Unmarshal(data []byte, v interface{}) error |
36 | // String returns the name of the Codec implementation. The returned | 47 | // String returns the name of the Codec implementation. This is unused by |
37 | // string will be used as part of content type in transmission. | 48 | // gRPC. |
38 | String() string | 49 | String() string |
39 | } | 50 | } |
40 | |||
41 | // protoCodec is a Codec implementation with protobuf. It is the default codec for gRPC. | ||
42 | type protoCodec struct { | ||
43 | } | ||
44 | |||
45 | type cachedProtoBuffer struct { | ||
46 | lastMarshaledSize uint32 | ||
47 | proto.Buffer | ||
48 | } | ||
49 | |||
50 | func capToMaxInt32(val int) uint32 { | ||
51 | if val > math.MaxInt32 { | ||
52 | return uint32(math.MaxInt32) | ||
53 | } | ||
54 | return uint32(val) | ||
55 | } | ||
56 | |||
57 | func (p protoCodec) marshal(v interface{}, cb *cachedProtoBuffer) ([]byte, error) { | ||
58 | protoMsg := v.(proto.Message) | ||
59 | newSlice := make([]byte, 0, cb.lastMarshaledSize) | ||
60 | |||
61 | cb.SetBuf(newSlice) | ||
62 | cb.Reset() | ||
63 | if err := cb.Marshal(protoMsg); err != nil { | ||
64 | return nil, err | ||
65 | } | ||
66 | out := cb.Bytes() | ||
67 | cb.lastMarshaledSize = capToMaxInt32(len(out)) | ||
68 | return out, nil | ||
69 | } | ||
70 | |||
71 | func (p protoCodec) Marshal(v interface{}) ([]byte, error) { | ||
72 | cb := protoBufferPool.Get().(*cachedProtoBuffer) | ||
73 | out, err := p.marshal(v, cb) | ||
74 | |||
75 | // put back buffer and lose the ref to the slice | ||
76 | cb.SetBuf(nil) | ||
77 | protoBufferPool.Put(cb) | ||
78 | return out, err | ||
79 | } | ||
80 | |||
81 | func (p protoCodec) Unmarshal(data []byte, v interface{}) error { | ||
82 | cb := protoBufferPool.Get().(*cachedProtoBuffer) | ||
83 | cb.SetBuf(data) | ||
84 | v.(proto.Message).Reset() | ||
85 | err := cb.Unmarshal(v.(proto.Message)) | ||
86 | cb.SetBuf(nil) | ||
87 | protoBufferPool.Put(cb) | ||
88 | return err | ||
89 | } | ||
90 | |||
91 | func (protoCodec) String() string { | ||
92 | return "proto" | ||
93 | } | ||
94 | |||
95 | var ( | ||
96 | protoBufferPool = &sync.Pool{ | ||
97 | New: func() interface{} { | ||
98 | return &cachedProtoBuffer{ | ||
99 | Buffer: proto.Buffer{}, | ||
100 | lastMarshaledSize: 16, | ||
101 | } | ||
102 | }, | ||
103 | } | ||
104 | ) | ||