xid fix
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m40s
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 5s
codeql / analyze (go) (pull_request) Failing after 3m9s
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m40s
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 5s
codeql / analyze (go) (pull_request) Failing after 3m9s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
c1fa0f991d
commit
30cc426c91
52
requestid.go
52
requestid.go
@ -17,32 +17,44 @@ 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 {
|
||||||
|
imd = metadata.New(1)
|
||||||
|
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)
|
ctx = metadata.NewIncomingContext(ctx, imd)
|
||||||
|
}
|
||||||
|
if !ook {
|
||||||
ctx = metadata.NewOutgoingContext(ctx, omd)
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
|
}
|
||||||
|
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user