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
|
|
|
)
|
|
|
|
|
2021-09-06 18:21:01 +03:00
|
|
|
// MetadataKey contains metadata key
|
|
|
|
var MetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2021-09-06 18:21:01 +03:00
|
|
|
// MetadataFunc wil be used if user not provide own func to fill metadata
|
|
|
|
var MetadataFunc = func(ctx context.Context) (context.Context, error) {
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = metadata.New(1)
|
|
|
|
}
|
|
|
|
if _, ok = md.Get(MetadataKey); ok {
|
2020-12-16 14:05:53 +03:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2021-09-06 18:21:01 +03:00
|
|
|
uid, err := id.New()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
md.Set(MetadataKey, uid)
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
return ctx, nil
|
|
|
|
}
|
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
|
|
|
|
if ctx, err = MetadataFunc(ctx); err != nil {
|
|
|
|
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
|
|
|
|
if ctx, err = MetadataFunc(ctx); err != nil {
|
|
|
|
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
|
|
|
|
if ctx, err = MetadataFunc(ctx); err != nil {
|
|
|
|
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
|
|
|
|
if ctx, err = MetadataFunc(ctx); err != nil {
|
|
|
|
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
|
|
|
|
if ctx, err = MetadataFunc(ctx); err != nil {
|
|
|
|
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
|
2021-02-18 17:12:24 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = metadata.New(1)
|
|
|
|
}
|
2020-12-16 14:05:53 +03:00
|
|
|
if id, ok := msg.Header()[MetadataKey]; ok {
|
2021-02-18 17:12:24 +03:00
|
|
|
md.Set(MetadataKey, id)
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
2020-12-16 14:05:53 +03:00
|
|
|
} else if ctx, err = MetadataFunc(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ctx, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|