28 lines
605 B
Go
28 lines
605 B
Go
package requestid
|
||
|
||
import (
|
||
"github.com/jtolds/gls"
|
||
)
|
||
|
||
// requestIDKey 是 gls 中存储 request_id 的 key
|
||
var requestIDKey = gls.GenSym()
|
||
|
||
// glsMgr 是 gls 管理器
|
||
var glsMgr = gls.NewContextManager()
|
||
|
||
// SetRequestID 在 gls 中设置 request_id,并在 fn 执行期间保持有效
|
||
func SetRequestID(requestID string, fn func()) {
|
||
glsMgr.SetValues(gls.Values{requestIDKey: requestID}, fn)
|
||
}
|
||
|
||
// GetRequestID 从 gls 中获取当前 goroutine 的 request_id
|
||
func GetRequestID() string {
|
||
val, ok := glsMgr.GetValue(requestIDKey)
|
||
if !ok {
|
||
return ""
|
||
}
|
||
reqID, _ := val.(string)
|
||
return reqID
|
||
}
|
||
|