sing-box/log/id.go
2022-07-12 17:44:12 +08:00

24 lines
407 B
Go

package log
import (
"context"
"math/rand"
"github.com/sagernet/sing/common/random"
)
func init() {
random.InitializeSeed()
}
type idKey struct{}
func ContextWithNewID(ctx context.Context) context.Context {
return context.WithValue(ctx, (*idKey)(nil), rand.Uint32())
}
func IDFromContext(ctx context.Context) (uint32, bool) {
id, loaded := ctx.Value((*idKey)(nil)).(uint32)
return id, loaded
}