mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 16:15:39 +08:00
33 lines
512 B
Go
33 lines
512 B
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/sagernet/sing/common/random"
|
|
)
|
|
|
|
func init() {
|
|
random.InitializeSeed()
|
|
}
|
|
|
|
type idKey struct{}
|
|
|
|
type ID struct {
|
|
ID uint32
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func ContextWithNewID(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, (*idKey)(nil), ID{
|
|
ID: rand.Uint32(),
|
|
CreatedAt: time.Now(),
|
|
})
|
|
}
|
|
|
|
func IDFromContext(ctx context.Context) (ID, bool) {
|
|
id, loaded := ctx.Value((*idKey)(nil)).(ID)
|
|
return id, loaded
|
|
}
|