From 0f3e56f6978e7d447840c7a58eabb93e0d828776 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 14 Nov 2022 14:52:12 +0300 Subject: [PATCH] allow to send server metadata via header Signed-off-by: Vasiliy Tolstov --- grpc.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/grpc.go b/grpc.go index e03c6ef..4af6571 100644 --- a/grpc.go +++ b/grpc.go @@ -283,7 +283,7 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err // get peer from context if p, ok := peer.FromContext(stream.Context()); ok { - md["Remote"] = p.Addr.String() + md.Set("Remote", p.Addr.String()) ctx = peer.NewContext(ctx, p) } @@ -388,7 +388,7 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea } // define the handler func fn := func(ctx context.Context, req server.Request, rsp interface{}) (err error) { - returnValues = function.Call([]reflect.Value{service.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)}) + returnValues = function.Call([]reflect.Value{service.rcvr, mtype.prepareContext(ctx), argv, reflect.ValueOf(rsp)}) // The return value for the method is an error. if rerr := returnValues[0].Interface(); rerr != nil { @@ -406,7 +406,13 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea statusCode := codes.OK statusDesc := "" // execute the handler - if appErr := fn(ctx, r, replyv.Interface()); appErr != nil { + appErr := fn(ctx, r, replyv.Interface()) + if outmd, ok := metadata.FromOutgoingContext(ctx); ok { + if err = stream.SendHeader(gmetadata.New(outmd)); err != nil { + return err + } + } + if appErr != nil { var errStatus *status.Status switch verr := appErr.(type) { case *errors.Error: @@ -526,7 +532,13 @@ func (g *grpcServer) processStream(ctx context.Context, stream grpc.ServerStream statusCode := codes.OK statusDesc := "" - if appErr := fn(ctx, r, ss); appErr != nil { + appErr := fn(ctx, r, ss) + if outmd, ok := metadata.FromOutgoingContext(ctx); ok { + if err := stream.SendHeader(gmetadata.New(outmd)); err != nil { + return err + } + } + if appErr != nil { var err error var errStatus *status.Status switch verr := appErr.(type) {