Compare commits

..

1 Commits
v3 ... master

Author SHA1 Message Date
da95cc0762 rework
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 16:34:50 +03:00
3 changed files with 89 additions and 85 deletions

12
go.mod
View File

@ -1,13 +1,5 @@
module go.unistack.org/micro-wrapper-metadata/v3 module go.unistack.org/micro-wrapper-metadata/v4
go 1.20 go 1.20
require go.unistack.org/micro/v3 v3.10.66 require go.unistack.org/micro/v4 v4.0.17
require (
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
)

33
go.sum
View File

@ -1,31 +1,2 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
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=

View File

@ -3,31 +3,62 @@ package wrappermetadata
import ( import (
"context" "context"
"go.unistack.org/micro/v3/client" "go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v3/server" "go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/server"
) )
var DefaultMetadataKeys = []string{"x-request-id"} type wrapper struct {
client.Client
type hook struct {
keys []string keys []string
} }
func NewHook(keys ...string) *hook { func NewClientWrapper(keys ...string) client.Wrapper {
return &hook{keys: keys} return func(c client.Client) client.Client {
handler := &wrapper{
Client: c,
keys: keys,
}
return handler
}
} }
func metadataCopy(ctx context.Context, keys []string) context.Context { func NewClientCallWrapper(keys ...string) client.CallWrapper {
if keys == nil { return func(fn client.CallFunc) client.CallFunc {
return ctx 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 ...options.Option) error {
if w.keys == nil {
return w.Client.Call(ctx, req, rsp, opts...)
} }
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
omd, ook := metadata.FromOutgoingContext(ctx) omd, ook := metadata.FromOutgoingContext(ctx)
if !ook || omd == nil { if !ook || omd == nil {
omd = metadata.New(len(keys)) omd = metadata.New(len(imd))
} }
for _, k := range keys { for _, k := range w.keys {
if v, ok := imd.Get(k); ok && v != "" { if v, ok := imd.Get(k); ok && v != "" {
omd.Set(k, v) omd.Set(k, v)
} }
@ -36,41 +67,51 @@ func metadataCopy(ctx context.Context, keys []string) context.Context {
ctx = metadata.NewOutgoingContext(ctx, omd) ctx = metadata.NewOutgoingContext(ctx, omd)
} }
} }
return ctx return w.Client.Call(ctx, req, rsp, opts...)
} }
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall { func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { if w.keys == nil {
return next(metadataCopy(ctx, w.keys), req, rsp, opts...) return w.Client.Stream(ctx, req, 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 (w *hook) ClientStream(next client.FuncStream) client.FuncStream { func NewServerHandlerWrapper(keys ...string) server.HandlerWrapper {
return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { return func(fn server.HandlerFunc) server.HandlerFunc {
return next(metadataCopy(ctx, w.keys), req, opts...) return func(ctx context.Context, req server.Request, rsp interface{}) error {
} if keys == nil {
} return fn(ctx, req, rsp)
}
func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish { if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error { omd, ook := metadata.FromOutgoingContext(ctx)
return next(metadataCopy(ctx, w.keys), msg, opts...) if !ook || omd == nil {
} omd = metadata.New(len(imd))
} }
for _, k := range keys {
func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish { if v, ok := imd.Get(k); ok && v != "" {
return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) error { omd.Set(k, v)
return next(metadataCopy(ctx, w.keys), msgs, opts...) }
} }
} if !ook {
ctx = metadata.NewOutgoingContext(ctx, omd)
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) return fn(ctx, 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)
} }
} }