30 lines
709 B
Go
30 lines
709 B
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"google.golang.org/grpc/metadata"
|
||
)
|
||
|
||
func ContextWithToken(token *string, reqid *string, timeout int) (context.Context, context.CancelFunc) {
|
||
if timeout == 0 {
|
||
timeout = 1
|
||
}
|
||
requestid := ""
|
||
if reqid != nil {
|
||
requestid = *reqid
|
||
}
|
||
accesstoken := ""
|
||
if token != nil {
|
||
accesstoken = *token
|
||
}
|
||
md := metadata.MD{}
|
||
md.Set("token", accesstoken) //这里的TOKENNAME无论大小写,获取时均为小写
|
||
md.Set("requestid", requestid)
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||
// ctx = context.WithValue(ctx, "requestid", requestid)
|
||
ctx = metadata.NewOutgoingContext(ctx, md)
|
||
return ctx, cancel
|
||
}
|