sing-box/log/id.go

24 lines
407 B
Go
Raw Permalink 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()
}
2022-07-12 07:17:29 +00:00
type idKey struct{}
2022-07-01 11:34:02 +00:00
2022-07-12 07:17:29 +00:00
func ContextWithNewID(ctx context.Context) context.Context {
return context.WithValue(ctx, (*idKey)(nil), rand.Uint32())
2022-06-30 13:27:56 +00:00
}
2022-07-12 07:17:29 +00:00
func IDFromContext(ctx context.Context) (uint32, bool) {
id, loaded := ctx.Value((*idKey)(nil)).(uint32)
return id, loaded
2022-06-30 13:27:56 +00:00
}