feat: add gls
This commit is contained in:
27
pkg/requestid/requestid.go
Normal file
27
pkg/requestid/requestid.go
Normal file
@@ -0,0 +1,27 @@
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user