fix xid
Some checks failed
codeql / analyze (go) (push) Failing after 3m16s
build / test (push) Failing after 1m29s
build / lint (push) Failing after 2m35s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-08-24 12:29:47 +03:00
parent c069636bf3
commit 6bb29d8d0e
3 changed files with 41 additions and 59 deletions

2
go.mod
View File

@ -2,4 +2,4 @@ module go.unistack.org/micro-wrapper-requestid/v4
go 1.20 go 1.20
require go.unistack.org/micro/v4 v4.0.1 require go.unistack.org/micro/v4 v4.0.7

2
go.sum
View File

@ -1,2 +1,4 @@
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo= go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs= go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=

View File

@ -6,6 +6,7 @@ import (
"go.unistack.org/micro/v4/client" "go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/server" "go.unistack.org/micro/v4/server"
"go.unistack.org/micro/v4/util/id" "go.unistack.org/micro/v4/util/id"
) )
@ -17,32 +18,45 @@ var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
// DefaultMetadataFunc wil be used if user not provide own func to fill metadata // DefaultMetadataFunc wil be used if user not provide own func to fill metadata
var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) { var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
imd, ok := metadata.FromIncomingContext(ctx) var xid string
if !ok { var err error
imd = metadata.New(1) var ook, iok bool
}
omd, ok := metadata.FromOutgoingContext(ctx) if _, ok := ctx.Value(XRequestIDKey).(string); !ok {
if !ok { xid, err = id.New()
omd = metadata.New(1)
}
v, iok := imd.Get(DefaultMetadataKey)
if iok {
if _, ook := omd.Get(DefaultMetadataKey); ook {
return ctx, nil
}
}
if !iok {
uid, err := id.New()
if err != nil { if err != nil {
return ctx, err return ctx, err
} }
v = uid ctx = context.WithValue(ctx, XRequestIDKey, xid)
} }
imd.Set(DefaultMetadataKey, v)
omd.Set(DefaultMetadataKey, v) imd, ok := metadata.FromIncomingContext(ctx)
ctx = context.WithValue(ctx, XRequestIDKey, v) if !ok {
ctx = metadata.NewIncomingContext(ctx, imd) imd = metadata.New(1)
ctx = metadata.NewOutgoingContext(ctx, omd) imd.Set(DefaultMetadataKey, xid)
} else if _, ok = imd.Get(DefaultMetadataKey); !ok {
imd.Set(DefaultMetadataKey, xid)
} else {
iok = true
}
omd, ok := metadata.FromOutgoingContext(ctx)
if !ok {
omd = metadata.New(1)
omd.Set(DefaultMetadataKey, xid)
} else if _, ok = omd.Get(DefaultMetadataKey); !ok {
omd.Set(DefaultMetadataKey, xid)
} else {
ook = true
}
if !iok {
ctx = metadata.NewIncomingContext(ctx, imd)
}
if !ook {
ctx = metadata.NewOutgoingContext(ctx, omd)
}
return ctx, nil return ctx, nil
} }
@ -71,7 +85,7 @@ func NewClientCallWrapper() client.CallWrapper {
} }
} }
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error {
var err error var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil { if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err return err
@ -79,7 +93,7 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{},
return w.Client.Call(ctx, req, rsp, opts...) return w.Client.Call(ctx, req, rsp, opts...)
} }
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
var err error var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil { if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return nil, err return nil, err
@ -87,14 +101,6 @@ func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client
return w.Client.Stream(ctx, req, opts...) 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 { func NewServerHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc { return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error { return func(ctx context.Context, req server.Request, rsp interface{}) error {
@ -106,29 +112,3 @@ func NewServerHandlerWrapper() server.HandlerWrapper {
} }
} }
} }
func NewServerSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Message) error {
var err error
imd, ok := metadata.FromIncomingContext(ctx)
if !ok {
imd = metadata.New(1)
}
omd, ok := metadata.FromOutgoingContext(ctx)
if !ok {
omd = metadata.New(1)
}
if id, ok := msg.Header()[DefaultMetadataKey]; ok {
imd.Set(DefaultMetadataKey, id)
omd.Set(DefaultMetadataKey, id)
ctx = context.WithValue(ctx, XRequestIDKey, id)
ctx = metadata.NewIncomingContext(ctx, imd)
ctx = metadata.NewOutgoingContext(ctx, omd)
} else if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err
}
return fn(ctx, msg)
}
}
}