Append time to session log
This commit is contained in:
17
log/id.go
17
log/id.go
@@ -3,6 +3,7 @@ package log
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing/common/random"
|
||||
)
|
||||
@@ -13,11 +14,19 @@ func init() {
|
||||
|
||||
type idKey struct{}
|
||||
|
||||
func ContextWithNewID(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, (*idKey)(nil), rand.Uint32())
|
||||
type ID struct {
|
||||
ID uint32
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func IDFromContext(ctx context.Context) (uint32, bool) {
|
||||
id, loaded := ctx.Value((*idKey)(nil)).(uint32)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user