minimize allocations (#1472)
* server: minimize allocations on re-register Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * server: stop old instance before Init() Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * client/grpc: fix allocations in protobuf marshal Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * codec/json: fix allocations in protobuf marshal Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * remove stop from init Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * codec/grpc: expose MaxMessageSize Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * codec: use buffer pool Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * metadata: minimize reallocations Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * util/wrapper: use metadata helper Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * registry/cache: move logs to debug level Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * server: move logs to debug level Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * server: cache service only when Advertise is ip addr Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * server: use metadata.Copy Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
1a06552877
commit
e0f64db92c
19
codec.go
19
codec.go
@ -11,6 +11,7 @@ import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/micro/go-micro/v2/codec"
|
||||
"github.com/micro/go-micro/v2/codec/bytes"
|
||||
"github.com/oxtoacart/bpool"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/encoding"
|
||||
)
|
||||
@ -23,6 +24,9 @@ type wrapCodec struct{ encoding.Codec }
|
||||
var jsonpbMarshaler = &jsonpb.Marshaler{}
|
||||
var useNumber bool
|
||||
|
||||
// create buffer pool with 16 instances each preallocated with 256 bytes
|
||||
var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
||||
|
||||
var (
|
||||
defaultGRPCCodecs = map[string]encoding.Codec{
|
||||
"application/json": jsonCodec{},
|
||||
@ -106,14 +110,19 @@ func (bytesCodec) Name() string {
|
||||
}
|
||||
|
||||
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
if pb, ok := v.(proto.Message); ok {
|
||||
s, err := jsonpbMarshaler.MarshalToString(pb)
|
||||
|
||||
return []byte(s), err
|
||||
}
|
||||
if b, ok := v.(*bytes.Frame); ok {
|
||||
return b.Data, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user