Vasiliy Tolstov
1fbc056dd4
* 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>
48 lines
943 B
Go
48 lines
943 B
Go
package proto
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/micro/go-micro/v2/codec"
|
|
"github.com/oxtoacart/bpool"
|
|
)
|
|
|
|
// create buffer pool with 16 instances each preallocated with 256 bytes
|
|
var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
|
|
|
type Marshaler struct{}
|
|
|
|
func (Marshaler) Marshal(v interface{}) ([]byte, error) {
|
|
pb, ok := v.(proto.Message)
|
|
if !ok {
|
|
return nil, codec.ErrInvalidMessage
|
|
}
|
|
|
|
// looks not good, but allows to reuse underlining bytes
|
|
buf := bufferPool.Get()
|
|
pbuf := proto.NewBuffer(buf.Bytes())
|
|
defer func() {
|
|
bufferPool.Put(bytes.NewBuffer(pbuf.Bytes()))
|
|
}()
|
|
|
|
if err := pbuf.Marshal(pb); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pbuf.Bytes(), nil
|
|
}
|
|
|
|
func (Marshaler) Unmarshal(data []byte, v interface{}) error {
|
|
pb, ok := v.(proto.Message)
|
|
if !ok {
|
|
return codec.ErrInvalidMessage
|
|
}
|
|
|
|
return proto.Unmarshal(data, pb)
|
|
}
|
|
|
|
func (Marshaler) String() string {
|
|
return "proto"
|
|
}
|