github.com/sagernet/sing-box@v1.9.0-rc.20/log/id.go (about) 1 package log 2 3 import ( 4 "context" 5 "math/rand" 6 "time" 7 8 "github.com/sagernet/sing/common/random" 9 ) 10 11 func init() { 12 random.InitializeSeed() 13 } 14 15 type idKey struct{} 16 17 type ID struct { 18 ID uint32 19 CreatedAt time.Time 20 } 21 22 func ContextWithNewID(ctx context.Context) context.Context { 23 return context.WithValue(ctx, (*idKey)(nil), ID{ 24 ID: rand.Uint32(), 25 CreatedAt: time.Now(), 26 }) 27 } 28 29 func IDFromContext(ctx context.Context) (ID, bool) { 30 id, loaded := ctx.Value((*idKey)(nil)).(ID) 31 return id, loaded 32 }