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