@@ -4,26 +4,27 @@ import (
44 "context"
55 "time"
66
7+ "github.com/pubgo/xerror"
78 "google.golang.org/grpc"
89 "google.golang.org/grpc/metadata"
910 "google.golang.org/grpc/peer"
1011
12+ "github.com/pubgo/lava/pkg/encoding"
1113 "github.com/pubgo/lava/types"
1214)
1315
1416func unaryInterceptor (middlewares []types.Middleware ) grpc.UnaryClientInterceptor {
15- var wrapperUnary = func (ctx context.Context , req types.Request , rsp func (response types.Response ) error ) error {
17+ var unaryWrapper = func (ctx context.Context , req types.Request , rsp func (response types.Response ) error ) error {
1618 var reqCtx = req .(* request )
1719 ctx = metadata .NewOutgoingContext (ctx , reqCtx .Header ())
1820 if err := reqCtx .invoker (ctx , reqCtx .method , reqCtx .req , reqCtx .reply , reqCtx .cc ); err != nil {
1921 return err
2022 }
21-
2223 return rsp (& response {req : reqCtx , resp : reqCtx .reply })
2324 }
2425
2526 for i := len (middlewares ) - 1 ; i >= 0 ; i -- {
26- wrapperUnary = middlewares [i ](wrapperUnary )
27+ unaryWrapper = middlewares [i ](unaryWrapper )
2728 }
2829
2930 return func (ctx context.Context , method string , req , reply interface {}, cc * grpc.ClientConn , invoker grpc.UnaryInvoker , opts ... grpc.CallOption ) (err error ) {
@@ -58,14 +59,17 @@ func unaryInterceptor(middlewares []types.Middleware) grpc.UnaryClientIntercepto
5859 if dur , err := time .ParseDuration (to [0 ]); err == nil {
5960 var cancel context.CancelFunc
6061 ctx , cancel = context .WithTimeout (ctx , dur )
61- _ = cancel
62+ defer cancel ()
6263 }
6364 }
6465
65- return wrapperUnary (
66- ctx ,
66+ var cdc = encoding .GetCdc (ct )
67+ xerror .Assert (cdc == nil , "contentType(%s) codec not found" , ct )
68+
69+ return unaryWrapper (ctx ,
6770 & request {
6871 ct : ct ,
72+ cdc : cdc ,
6973 header : md ,
7074 service : serviceFromMethod (method ),
7175 opts : opts ,
@@ -128,14 +132,17 @@ func streamInterceptor(middlewares []types.Middleware) grpc.StreamClientIntercep
128132 if dur , err := time .ParseDuration (to [0 ]); err == nil {
129133 var cancel context.CancelFunc
130134 ctx , cancel = context .WithTimeout (ctx , dur )
131- _ = cancel
135+ defer cancel ()
132136 }
133137 }
134138
135- return nil , wrapperStream (
136- ctx ,
139+ var cdc = encoding .GetCdc (ct )
140+ xerror .Assert (cdc == nil , "contentType(%s) codec not found" , ct )
141+
142+ return nil , wrapperStream (ctx ,
137143 & request {
138144 ct : ct ,
145+ cdc : cdc ,
139146 service : serviceFromMethod (method ),
140147 header : md ,
141148 opts : opts ,
@@ -144,10 +151,7 @@ func streamInterceptor(middlewares []types.Middleware) grpc.StreamClientIntercep
144151 method : method ,
145152 streamer : streamer ,
146153 },
147- func (rsp types.Response ) error {
148- resp = rsp .(* response ).stream
149- return nil
150- },
154+ func (rsp types.Response ) error { resp = rsp .(* response ).stream ; return nil },
151155 )
152156 }
153157}
0 commit comments