Save marshalled empty response in streaming service

Signed-off-by: Jin Dong <djdongjin95@gmail.com>
This commit is contained in:
Jin Dong 2023-06-09 18:21:16 +00:00
parent 3065aa2aae
commit e26c97cb8b

View File

@ -31,7 +31,16 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
) )
var emptyResponse typeurl.Any
func init() { func init() {
// save marshalled empty response to avoid marshaling everytime
var err error
emptyResponse, err = typeurl.MarshalAny(&ptypes.Empty{})
if err != nil {
panic(err)
}
plugin.Register(&plugin.Registration{ plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin, Type: plugin.GRPCPlugin,
ID: "streaming", ID: "streaming",
@ -69,12 +78,6 @@ func (s *service) Stream(srv api.Streaming_StreamServer) error {
return err return err
} }
// TODO: Save this response to avoid marshaling everytime
response, err := typeurl.MarshalAny(&ptypes.Empty{})
if err != nil {
return err
}
cc := make(chan struct{}) cc := make(chan struct{})
ss := &serviceStream{ ss := &serviceStream{
s: srv, s: srv,
@ -87,7 +90,7 @@ func (s *service) Stream(srv api.Streaming_StreamServer) error {
} }
// Send response packet after registering stream // Send response packet after registering stream
if err := srv.Send(protobuf.FromAny(response)); err != nil { if err := srv.Send(protobuf.FromAny(emptyResponse)); err != nil {
return err return err
} }