sing-box/log/id.go

33 lines
512 B
Go
Raw Permalink Normal View History

2022-06-30 21:27:56 +08:00
package log
import (
"context"
"math/rand"
2023-03-29 13:07:08 +08:00
"time"
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
2023-03-29 13:07:08 +08:00
type ID struct {
ID uint32
CreatedAt time.Time
}
2022-07-12 15:17:29 +08:00
func ContextWithNewID(ctx context.Context) context.Context {
2023-03-29 13:07:08 +08:00
return context.WithValue(ctx, (*idKey)(nil), ID{
ID: rand.Uint32(),
CreatedAt: time.Now(),
})
2022-06-30 21:27:56 +08:00
}
2023-03-29 13:07:08 +08:00
func IDFromContext(ctx context.Context) (ID, bool) {
id, loaded := ctx.Value((*idKey)(nil)).(ID)
2022-07-12 15:17:29 +08:00
return id, loaded
2022-06-30 21:27:56 +08:00
}