| 1 |
package yarpc |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"io" |
| 6 |
"sync" |
| 7 |
|
| 8 |
"github.com/gogo/protobuf/codec" |
| 9 |
"github.com/influxdata/yamux" |
| 10 |
"github.com/influxdata/yarpc/codes" |
| 11 |
"github.com/influxdata/yarpc/status" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
codecPool = &sync.Pool{ |
| 16 |
New: func() interface{} { |
| 17 |
return codec.New(1024) |
| 18 |
}, |
| 19 |
} |
| 20 |
) |
| 21 |
|
| 22 |
type pooledCodec struct{} |
| 23 |
|
| 24 |
var ( |
| 25 |
cd = &pooledCodec{} |
| 26 |
) |
| 27 |
|
| 28 |
func NewCodec() Codec { |
| 29 |
return cd |
| 30 |
} |
| 31 |
|
| 32 |
func (*pooledCodec) Marshal(v interface{}) ([]byte, error) { |
| 33 |
ci := codecPool.Get() |
| 34 |
c := ci.(codec.Codec) |
| 35 |
data, err := c.Marshal(v) |
| 36 |
|
| 37 |
dataCopy := append([]byte(nil), data...) |
| 38 |
codecPool.Put(ci) |
| 39 |
return dataCopy, err |
| 40 |
} |
| 41 |
|
| 42 |
func (*pooledCodec) Unmarshal(data []byte, v interface{}) error { |
| 43 |
ci := codecPool.Get() |
| 44 |
c := ci.(codec.Codec) |
| 45 |
err := c.Unmarshal(data, v) |
| 46 |
codecPool.Put(ci) |
| 47 |
return err |
| 48 |
} |
| 49 |
|
| 50 |
type Codec interface { |
| 51 |
Marshal(v interface{}) ([]byte, error) |
| 52 |
Unmarshal(data []byte, v interface{}) error |
| 53 |
} |
| 54 |
|
| 55 |
type parser struct { |
| 56 |
r io.Reader |
| 57 |
header [4]byte |
| 58 |
} |
| 59 |
|
| 60 |
func (p *parser) recvMsg() (msg []byte, err error) { |
| 61 |
if _, err := io.ReadFull(p.r, p.header[:]); err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
|
| 65 |
length := binary.BigEndian.Uint32(p.header[:]) |
| 66 |
if length == 0 { |
| 67 |
return nil, nil |
| 68 |
} |
| 69 |
|
| 70 |
msg = make([]byte, int(length)) |
| 71 |
if _, err := io.ReadFull(p.r, msg); err != nil { |
| 72 |
if err == io.EOF { |
| 73 |
err = io.ErrUnexpectedEOF |
| 74 |
} |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
return msg, nil |
| 78 |
} |
| 79 |
|
| 80 |
func encode(c Codec, msg interface{}) ([]byte, error) { |
| 81 |
var ( |
| 82 |
b []byte |
| 83 |
length uint |
| 84 |
) |
| 85 |
|
| 86 |
if msg != nil { |
| 87 |
var err error |
| 88 |
b, err = c.Marshal(msg) |
| 89 |
if err != nil { |
| 90 |
|
| 91 |
return nil, status.Errorf(codes.Internal, "rpc: error while marshaling %v", err) |
| 92 |
} |
| 93 |
length = uint(len(b)) |
| 94 |
} |
| 95 |
|
| 96 |
const ( |
| 97 |
sizeLen = 4 |
| 98 |
) |
| 99 |
|
| 100 |
var buf = make([]byte, sizeLen+length) |
| 101 |
binary.BigEndian.PutUint32(buf, uint32(length)) |
| 102 |
copy(buf[4:], b) |
| 103 |
|
| 104 |
return buf, nil |
| 105 |
} |
| 106 |
|
| 107 |
func decode(p *parser, c Codec, s *yamux.Stream, m interface{}) error { |
| 108 |
d, err := p.recvMsg() |
| 109 |
if err != nil { |
| 110 |
return err |
| 111 |
} |
| 112 |
|
| 113 |
if err := c.Unmarshal(d, m); err != nil { |
| 114 |
return status.Errorf(codes.Internal, "rpc: failed to unmarshal received message %v", err) |
| 115 |
} |
| 116 |
|
| 117 |
return nil |
| 118 |
} |