grpc-client/service/context-service.go

30 lines
709 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}