diff --git a/go.mod b/go.mod index 7c1ec2e..f625f7c 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,12 @@ module go.unistack.org/micro-wrapper-metadata/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.3 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e // indirect - google.golang.org/grpc v1.57.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + github.com/golang/protobuf v1.5.4 // 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 c605039..3b5cd63 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,31 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +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.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= 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= +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.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +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.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e h1:NumxXLPfHSndr3wBBdeKiVHjGVFzi9RX2HwwQke94iY= google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= +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.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw= google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= +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.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +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/metadata.go b/metadata.go index bb69743..1727400 100644 --- a/metadata.go +++ b/metadata.go @@ -8,56 +8,26 @@ import ( "go.unistack.org/micro/v3/server" ) -type wrapper struct { - client.Client +var DefaultMetadataKeys = []string{"x-request-id"} + +type hook struct { keys []string } -func NewClientWrapper(keys ...string) client.Wrapper { - return func(c client.Client) client.Client { - handler := &wrapper{ - Client: c, - keys: keys, - } - return handler - } +func NewHook(keys ...string) *hook { + return &hook{keys: keys} } -func NewClientCallWrapper(keys ...string) 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 { - if keys == nil { - return fn(ctx, addr, req, rsp, opts) - } - if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { - omd, ook := metadata.FromOutgoingContext(ctx) - if !ook || omd == nil { - omd = metadata.New(len(imd)) - } - for _, k := range keys { - if v, ok := imd.Get(k); ok && v != "" { - omd.Set(k, v) - } - } - if !ook { - ctx = metadata.NewOutgoingContext(ctx, omd) - } - } - return fn(ctx, addr, req, rsp, opts) - } - } -} - -func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - if w.keys == nil { - return w.Client.Call(ctx, req, rsp, opts...) +func metadataCopy(ctx context.Context, keys []string) context.Context { + if keys == nil { + return ctx } if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { omd, ook := metadata.FromOutgoingContext(ctx) if !ook || omd == nil { - omd = metadata.New(len(imd)) + omd = metadata.New(len(keys)) } - for _, k := range w.keys { + for _, k := range keys { if v, ok := imd.Get(k); ok && v != "" { omd.Set(k, v) } @@ -66,51 +36,41 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, ctx = metadata.NewOutgoingContext(ctx, omd) } } - return w.Client.Call(ctx, req, rsp, opts...) + return ctx } -func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { - if w.keys == nil { - return w.Client.Stream(ctx, req, opts...) +func (w *hook) ClientCall(next client.FuncCall) client.FuncCall { + return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { + return next(metadataCopy(ctx, w.keys), req, rsp, opts...) } - if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { - omd, ook := metadata.FromOutgoingContext(ctx) - if !ook || omd == nil { - omd = metadata.New(len(imd)) - } - for _, k := range w.keys { - if v, ok := imd.Get(k); ok && v != "" { - omd.Set(k, v) - } - } - if !ook { - ctx = metadata.NewOutgoingContext(ctx, omd) - } - } - return w.Client.Stream(ctx, req, opts...) } -func NewServerHandlerWrapper(keys ...string) server.HandlerWrapper { - return func(fn server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - if keys == nil { - return fn(ctx, req, rsp) - } - if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { - omd, ook := metadata.FromOutgoingContext(ctx) - if !ook || omd == nil { - omd = metadata.New(len(imd)) - } - for _, k := range keys { - if v, ok := imd.Get(k); ok && v != "" { - omd.Set(k, v) - } - } - if !ook { - ctx = metadata.NewOutgoingContext(ctx, omd) - } - } - return fn(ctx, req, rsp) - } +func (w *hook) ClientStream(next client.FuncStream) client.FuncStream { + return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { + return next(metadataCopy(ctx, w.keys), req, opts...) + } +} + +func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish { + return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error { + return next(metadataCopy(ctx, w.keys), msg, opts...) + } +} + +func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish { + return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) error { + return next(metadataCopy(ctx, w.keys), msgs, opts...) + } +} + +func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler { + return func(ctx context.Context, req server.Request, rsp interface{}) error { + return next(metadataCopy(ctx, w.keys), req, rsp) + } +} + +func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler { + return func(ctx context.Context, msg server.Message) error { + return next(metadataCopy(ctx, w.keys), msg) } }