diff --git a/go.mod b/go.mod index dd66f18..460a954 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,11 @@ module go.unistack.org/micro-wrapper-requestid/v3 go 1.20 -require go.unistack.org/micro/v3 v3.10.49 +require go.unistack.org/micro/v3 v3.10.66 require ( - github.com/golang/protobuf v1.5.4 // indirect - golang.org/x/sys v0.18.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240308144416-29370a3891b7 // indirect - google.golang.org/grpc v1.62.1 // indirect + golang.org/x/sys v0.19.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect + google.golang.org/grpc v1.63.2 // indirect google.golang.org/protobuf v1.33.0 // indirect ) diff --git a/go.sum b/go.sum index 3407d55..2a91543 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,13 @@ -github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= -github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -go.unistack.org/micro/v3 v3.10.49 h1:eW+1OCH4ENjfL0Uaw3+Js9pXRB63+NuThfaozM+eWBs= -go.unistack.org/micro/v3 v3.10.49/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= -golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +go.unistack.org/micro/v3 v3.10.66 h1:tiG8HnyTC71IZWSC2qT/DmLhJinZJL9qvw+4Fvpm3d4= +go.unistack.org/micro/v3 v3.10.66/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240308144416-29370a3891b7 h1:em/y72n4XlYRtayY/cVj6pnVzHa//BDA1BdoO+z9mdE= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240308144416-29370a3891b7/go.mod h1:UCOku4NytXMJuLQE5VuqA5lX3PcHCBo8pxNyvkf4xBs= -google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= -google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= +google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/requestid.go b/requestid.go index 13d5603..9c8f915 100644 --- a/requestid.go +++ b/requestid.go @@ -81,78 +81,69 @@ var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) { return ctx, nil } -type wrapper struct { - client.Client -} +type Hook struct{} -func NewClientWrapper() client.Wrapper { - return func(c client.Client) client.Client { - handler := &wrapper{ - Client: c, +var Wrapper = &Hook{} + +func (w *Hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler { + return func(ctx context.Context, msg server.Message) error { + var err error + if xid, ok := msg.Header()[DefaultMetadataKey]; ok { + ctx = context.WithValue(ctx, XRequestIDKey{}, xid) } - return handler - } -} - -func NewClientCallWrapper() client.CallWrapper { - return func(fn client.CallFunc) client.CallFunc { - return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { - var err error - if ctx, err = DefaultMetadataFunc(ctx); err != nil { - return err - } - return fn(ctx, addr, req, rsp, opts) + if ctx, err = DefaultMetadataFunc(ctx); err != nil { + return err } + return next(ctx, msg) } } -func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - var err error - if ctx, err = DefaultMetadataFunc(ctx); err != nil { - return err - } - return w.Client.Call(ctx, req, rsp, opts...) -} - -func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { - var err error - if ctx, err = DefaultMetadataFunc(ctx); err != nil { - return nil, err - } - return w.Client.Stream(ctx, req, opts...) -} - -func (w *wrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error { - var err error - if ctx, err = DefaultMetadataFunc(ctx); err != nil { - return err - } - return w.Client.Publish(ctx, msg, opts...) -} - -func NewServerHandlerWrapper() server.HandlerWrapper { - return func(fn server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - var err error - if ctx, err = DefaultMetadataFunc(ctx); err != nil { - return err - } - return fn(ctx, req, rsp) +func (w *Hook) ServerHandler(next server.FuncHandler) server.FuncHandler { + return func(ctx context.Context, req server.Request, rsp interface{}) error { + var err error + if ctx, err = DefaultMetadataFunc(ctx); err != nil { + return err } + return next(ctx, req, rsp) } } -func NewServerSubscriberWrapper() server.SubscriberWrapper { - return func(fn server.SubscriberFunc) server.SubscriberFunc { - return func(ctx context.Context, msg server.Message) error { - var err error - if xid, ok := msg.Header()[DefaultMetadataKey]; ok { - ctx = context.WithValue(ctx, XRequestIDKey{}, xid) - } - if ctx, err = DefaultMetadataFunc(ctx); err != nil { - return err - } - return fn(ctx, msg) +func (w *Hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish { + return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) error { + var err error + if ctx, err = DefaultMetadataFunc(ctx); err != nil { + return err } + return next(ctx, msgs, opts...) + } +} + +func (w *Hook) ClientPublish(next client.FuncPublish) client.FuncPublish { + return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error { + var err error + if ctx, err = DefaultMetadataFunc(ctx); err != nil { + return err + } + return next(ctx, msg, opts...) + } +} + +func (w *Hook) ClientCall(next client.FuncCall) client.FuncCall { + return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { + var err error + if ctx, err = DefaultMetadataFunc(ctx); err != nil { + return err + } + return next(ctx, req, rsp, opts...) + } +} + +func (w *Hook) ClientStream(next client.FuncStream) client.FuncStream { + return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { + var err error + if ctx, err = DefaultMetadataFunc(ctx); err != nil { + return nil, err + } + return next(ctx, req, opts...) } }