Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
28ca015b0e | |||
af79e6adc5 | |||
85fff3f75b |
12
go.mod
12
go.mod
@ -1,5 +1,13 @@
|
|||||||
module go.unistack.org/micro-wrapper-metadata/v4
|
module go.unistack.org/micro-wrapper-metadata/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require go.unistack.org/micro/v4 v4.0.17
|
require go.unistack.org/micro/v3 v3.10.66
|
||||||
|
|
||||||
|
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
33
go.sum
@ -1,2 +1,31 @@
|
|||||||
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
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=
|
||||||
|
117
metadata.go
117
metadata.go
@ -3,87 +3,74 @@ package wrappermetadata
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/client"
|
"go.unistack.org/micro/v3/client"
|
||||||
"go.unistack.org/micro/v4/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"go.unistack.org/micro/v4/options"
|
"go.unistack.org/micro/v3/server"
|
||||||
"go.unistack.org/micro/v4/server"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type wrapper struct {
|
var DefaultMetadataKeys = []string{"x-request-id"}
|
||||||
client.Client
|
|
||||||
exclude []string
|
type hook struct {
|
||||||
|
keys []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientWrapper(exclude ...string) client.Wrapper {
|
func NewHook(keys ...string) *hook {
|
||||||
return func(c client.Client) client.Client {
|
return &hook{keys: keys}
|
||||||
handler := &wrapper{
|
|
||||||
Client: c,
|
|
||||||
exclude: exclude,
|
|
||||||
}
|
|
||||||
return handler
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientCallWrapper(exclude ...string) client.CallWrapper {
|
func metadataCopy(ctx context.Context, keys []string) context.Context {
|
||||||
return func(fn client.CallFunc) client.CallFunc {
|
if keys == nil {
|
||||||
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
return ctx
|
||||||
if exclude == 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))
|
|
||||||
}
|
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, exclude...), false))
|
|
||||||
}
|
|
||||||
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.exclude == 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(imd))
|
omd = metadata.New(len(keys))
|
||||||
|
}
|
||||||
|
for _, k := range keys {
|
||||||
|
if v, ok := imd.Get(k); ok && v != "" {
|
||||||
|
omd.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ook {
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
}
|
}
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, w.exclude...), false))
|
|
||||||
}
|
}
|
||||||
return w.Client.Call(ctx, req, rsp, opts...)
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
|
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall {
|
||||||
if w.exclude == nil {
|
return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||||
return w.Client.Stream(ctx, req, opts...)
|
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))
|
|
||||||
}
|
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, w.exclude...), false))
|
|
||||||
}
|
|
||||||
return w.Client.Stream(ctx, req, opts...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerHandlerWrapper(exclude ...string) server.HandlerWrapper {
|
func (w *hook) ClientStream(next client.FuncStream) client.FuncStream {
|
||||||
return func(fn server.HandlerFunc) server.HandlerFunc {
|
return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
||||||
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
return next(metadataCopy(ctx, w.keys), req, opts...)
|
||||||
if exclude == nil {
|
}
|
||||||
return fn(ctx, req, rsp)
|
}
|
||||||
}
|
|
||||||
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish {
|
||||||
omd, ook := metadata.FromOutgoingContext(ctx)
|
return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
||||||
if !ook || omd == nil {
|
return next(metadataCopy(ctx, w.keys), msg, opts...)
|
||||||
omd = metadata.New(len(imd))
|
}
|
||||||
}
|
}
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, exclude...), false))
|
|
||||||
}
|
func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish {
|
||||||
return fn(ctx, req, rsp)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user