2022-06-30 21:27:56 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
2022-07-01 19:34:02 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing/common/random"
|
2022-06-30 21:27:56 +08:00
|
|
|
)
|
|
|
|
|
2022-07-01 19:34:02 +08:00
|
|
|
func init() {
|
|
|
|
random.InitializeSeed()
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:17:29 +08:00
|
|
|
type idKey struct{}
|
2022-07-01 19:34:02 +08:00
|
|
|
|
2022-07-12 15:17:29 +08:00
|
|
|
func ContextWithNewID(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, (*idKey)(nil), rand.Uint32())
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:17:29 +08:00
|
|
|
func IDFromContext(ctx context.Context) (uint32, bool) {
|
|
|
|
id, loaded := ctx.Value((*idKey)(nil)).(uint32)
|
|
|
|
return id, loaded
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|