sing-box/log/id.go

34 lines
474 B
Go
Raw Normal View History

2022-06-30 13:27:56 +00:00
package log
import (
"context"
"math/rand"
2022-07-01 11:34:02 +00:00
"github.com/sagernet/sing/common/random"
2022-06-30 13:27:56 +00:00
)
2022-07-01 11:34:02 +00:00
func init() {
random.InitializeSeed()
}
var idType = (*idContext)(nil)
2022-06-30 13:27:56 +00:00
type idContext struct {
context.Context
id uint32
}
2022-07-01 11:34:02 +00:00
func (c *idContext) Value(key any) any {
if key == idType {
return c
}
return c.Context.Value(key)
}
2022-06-30 13:27:56 +00:00
func ContextWithID(ctx context.Context) context.Context {
2022-07-01 11:34:02 +00:00
if ctx.Value(idType) != nil {
return ctx
}
2022-06-30 13:27:56 +00:00
return &idContext{ctx, rand.Uint32()}
}