2021-10-26 23:47:53 +03:00
|
|
|
package requestid // import "go.unistack.org/micro-wrapper-requestid/v3"
|
2020-12-16 14:05:53 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/textproto"
|
|
|
|
|
2021-10-26 23:47:53 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
"go.unistack.org/micro/v3/util/id"
|
2020-12-16 14:05:53 +03:00
|
|
|
)
|
|
|
|
|
2023-08-23 16:30:47 +03:00
|
|
|
var XRequestIDKey struct{}
|
|
|
|
|
2022-12-18 20:06:49 +03:00
|
|
|
// DefaultMetadataKey contains metadata key
|
|
|
|
var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2022-12-18 20:06:49 +03:00
|
|
|
// DefaultMetadataFunc wil be used if user not provide own func to fill metadata
|
|
|
|
var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
|
|
|
|
imd, ok := metadata.FromIncomingContext(ctx)
|
2021-09-06 18:21:01 +03:00
|
|
|
if !ok {
|
2022-12-18 20:06:49 +03:00
|
|
|
imd = metadata.New(1)
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2022-12-18 20:06:49 +03:00
|
|
|
omd, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
omd = metadata.New(1)
|
|
|
|
}
|
|
|
|
v, iok := imd.Get(DefaultMetadataKey)
|
|
|
|
if iok {
|
|
|
|
if _, ook := omd.Get(DefaultMetadataKey); ook {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
2022-12-18 20:06:49 +03:00
|
|
|
if !iok {
|
|
|
|
uid, err := id.New()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
v = uid
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2022-12-18 20:06:49 +03:00
|
|
|
imd.Set(DefaultMetadataKey, v)
|
|
|
|
omd.Set(DefaultMetadataKey, v)
|
2023-08-23 16:32:14 +03:00
|
|
|
ctx = context.WithValue(ctx, XRequestIDKey, v)
|
2022-12-18 20:06:49 +03:00
|
|
|
ctx = metadata.NewIncomingContext(ctx, imd)
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
2023-08-23 16:30:47 +03:00
|
|
|
|
2023-08-23 16:32:14 +03:00
|
|
|
return ctx, nil
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2020-12-16 14:05:53 +03:00
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWrapper() client.Wrapper {
|
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
handler := &wrapper{
|
|
|
|
Client: c,
|
|
|
|
}
|
|
|
|
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
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ctx, addr, req, rsp, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
var err error
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
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
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
|
2020-12-17 01:57:53 +03:00
|
|
|
func (w *wrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
2020-12-16 14:05:53 +03:00
|
|
|
var err error
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-12-17 01:57:53 +03:00
|
|
|
return w.Client.Publish(ctx, msg, opts...)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(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
|
2022-12-18 20:06:49 +03:00
|
|
|
imd, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
imd = metadata.New(1)
|
|
|
|
}
|
|
|
|
omd, ok := metadata.FromOutgoingContext(ctx)
|
2021-02-18 17:12:24 +03:00
|
|
|
if !ok {
|
2022-12-18 20:06:49 +03:00
|
|
|
omd = metadata.New(1)
|
2021-02-18 17:12:24 +03:00
|
|
|
}
|
2022-12-18 20:06:49 +03:00
|
|
|
if id, ok := msg.Header()[DefaultMetadataKey]; ok {
|
|
|
|
imd.Set(DefaultMetadataKey, id)
|
|
|
|
omd.Set(DefaultMetadataKey, id)
|
2023-08-24 09:25:03 +03:00
|
|
|
ctx = context.WithValue(ctx, XRequestIDKey, id)
|
2022-12-18 20:06:49 +03:00
|
|
|
ctx = metadata.NewIncomingContext(ctx, imd)
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
|
|
|
} else if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ctx, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|