2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-03-21 12:40:58 +03:00
|
|
|
"encoding/json"
|
2019-06-03 18:44:43 +01:00
|
|
|
"fmt"
|
2019-06-17 20:05:58 +01:00
|
|
|
"strings"
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2019-07-04 14:06:29 +03:00
|
|
|
b "bytes"
|
|
|
|
|
2019-07-04 16:43:36 +08:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
2019-06-03 18:44:43 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-07-27 13:22:00 +01:00
|
|
|
"github.com/micro/go-micro/v3/codec"
|
|
|
|
"github.com/micro/go-micro/v3/codec/bytes"
|
2020-04-08 12:50:19 +03:00
|
|
|
"github.com/oxtoacart/bpool"
|
2019-06-17 20:05:58 +01:00
|
|
|
"google.golang.org/grpc"
|
2019-06-18 18:51:52 +01:00
|
|
|
"google.golang.org/grpc/encoding"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type jsonCodec struct{}
|
|
|
|
type protoCodec struct{}
|
|
|
|
type bytesCodec struct{}
|
|
|
|
type wrapCodec struct{ encoding.Codec }
|
|
|
|
|
2019-07-08 10:32:10 +08:00
|
|
|
var jsonpbMarshaler = &jsonpb.Marshaler{}
|
2020-03-21 12:40:58 +03:00
|
|
|
var useNumber bool
|
2019-07-08 10:32:10 +08:00
|
|
|
|
2020-04-08 12:50:19 +03:00
|
|
|
// create buffer pool with 16 instances each preallocated with 256 bytes
|
|
|
|
var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
var (
|
|
|
|
defaultGRPCCodecs = map[string]encoding.Codec{
|
|
|
|
"application/json": jsonCodec{},
|
|
|
|
"application/proto": protoCodec{},
|
|
|
|
"application/protobuf": protoCodec{},
|
|
|
|
"application/octet-stream": protoCodec{},
|
2019-08-27 08:13:58 +01:00
|
|
|
"application/grpc": protoCodec{},
|
2019-06-03 18:44:43 +01:00
|
|
|
"application/grpc+json": jsonCodec{},
|
|
|
|
"application/grpc+proto": protoCodec{},
|
|
|
|
"application/grpc+bytes": bytesCodec{},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18)
|
|
|
|
func UseNumber() {
|
2020-03-21 12:40:58 +03:00
|
|
|
useNumber = true
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w wrapCodec) String() string {
|
|
|
|
return w.Codec.Name()
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:05:58 +01:00
|
|
|
func (w wrapCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
b, ok := v.(*bytes.Frame)
|
|
|
|
if ok {
|
|
|
|
return b.Data, nil
|
|
|
|
}
|
|
|
|
return w.Codec.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w wrapCodec) Unmarshal(data []byte, v interface{}) error {
|
|
|
|
b, ok := v.(*bytes.Frame)
|
|
|
|
if ok {
|
|
|
|
b.Data = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w.Codec.Unmarshal(data, v)
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
|
2020-02-13 14:57:21 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *bytes.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case proto.Message:
|
|
|
|
return proto.Marshal(m)
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
2020-02-13 14:57:21 +03:00
|
|
|
return nil, fmt.Errorf("failed to marshal: %v is not type of *bytes.Frame or proto.Message", v)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-02-13 14:57:21 +03:00
|
|
|
m, ok := v.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to unmarshal: %v is not type of proto.Message", v)
|
|
|
|
}
|
|
|
|
return proto.Unmarshal(data, m)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (protoCodec) Name() string {
|
|
|
|
return "proto"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
b, ok := v.(*[]byte)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v)
|
|
|
|
}
|
|
|
|
return *b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
|
|
|
|
b, ok := v.(*[]byte)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v)
|
|
|
|
}
|
|
|
|
*b = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bytesCodec) Name() string {
|
|
|
|
return "bytes"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
2020-03-10 15:21:43 +00:00
|
|
|
if b, ok := v.(*bytes.Frame); ok {
|
|
|
|
return b.Data, nil
|
|
|
|
}
|
2020-04-08 12:50:19 +03:00
|
|
|
|
|
|
|
if pb, ok := v.(proto.Message); ok {
|
|
|
|
buf := bufferPool.Get()
|
|
|
|
defer bufferPool.Put(buf)
|
|
|
|
if err := jsonpbMarshaler.Marshal(buf, pb); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
return json.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
|
2020-01-07 18:37:34 +00:00
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-10 15:21:43 +00:00
|
|
|
if b, ok := v.(*bytes.Frame); ok {
|
|
|
|
b.Data = data
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-04 16:43:36 +08:00
|
|
|
if pb, ok := v.(proto.Message); ok {
|
2019-07-04 14:06:29 +03:00
|
|
|
return jsonpb.Unmarshal(b.NewReader(data), pb)
|
2019-07-04 16:43:36 +08:00
|
|
|
}
|
2020-03-21 12:40:58 +03:00
|
|
|
|
|
|
|
dec := json.NewDecoder(b.NewReader(data))
|
|
|
|
if useNumber {
|
|
|
|
dec.UseNumber()
|
|
|
|
}
|
|
|
|
return dec.Decode(v)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonCodec) Name() string {
|
|
|
|
return "json"
|
|
|
|
}
|
2019-06-17 20:05:58 +01:00
|
|
|
|
|
|
|
type grpcCodec struct {
|
|
|
|
// headers
|
2019-06-18 18:51:52 +01:00
|
|
|
id string
|
|
|
|
target string
|
|
|
|
method string
|
2019-06-17 20:05:58 +01:00
|
|
|
endpoint string
|
|
|
|
|
|
|
|
s grpc.ClientStream
|
|
|
|
c encoding.Codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
|
|
|
md, err := g.s.Header()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if m == nil {
|
|
|
|
m = new(codec.Message)
|
|
|
|
}
|
|
|
|
if m.Header == nil {
|
2020-02-24 17:15:20 +03:00
|
|
|
m.Header = make(map[string]string, len(md))
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
for k, v := range md {
|
|
|
|
m.Header[k] = strings.Join(v, ",")
|
|
|
|
}
|
|
|
|
m.Id = g.id
|
|
|
|
m.Target = g.target
|
|
|
|
m.Method = g.method
|
|
|
|
m.Endpoint = g.endpoint
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) ReadBody(v interface{}) error {
|
2019-06-18 18:51:52 +01:00
|
|
|
if f, ok := v.(*bytes.Frame); ok {
|
|
|
|
return g.s.RecvMsg(f)
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
2019-06-18 18:51:52 +01:00
|
|
|
return g.s.RecvMsg(v)
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) Write(m *codec.Message, v interface{}) error {
|
|
|
|
// if we don't have a body
|
2019-06-18 18:51:52 +01:00
|
|
|
if v != nil {
|
|
|
|
return g.s.SendMsg(v)
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
// write the body using the framing codec
|
2019-12-05 16:10:49 +00:00
|
|
|
return g.s.SendMsg(&bytes.Frame{Data: m.Body})
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) Close() error {
|
|
|
|
return g.s.CloseSend()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcCodec) String() string {
|
|
|
|
return g.c.Name()
|
|
|
|
}
|