sing-box/log/id.go
2022-07-01 19:37:41 +08:00

34 lines
474 B
Go

package log
import (
"context"
"math/rand"
"github.com/sagernet/sing/common/random"
)
func init() {
random.InitializeSeed()
}
var idType = (*idContext)(nil)
type idContext struct {
context.Context
id uint32
}
func (c *idContext) Value(key any) any {
if key == idType {
return c
}
return c.Context.Value(key)
}
func ContextWithID(ctx context.Context) context.Context {
if ctx.Value(idType) != nil {
return ctx
}
return &idContext{ctx, rand.Uint32()}
}