id.go 407 B

1234567891011121314151617181920212223
  1. package log
  2. import (
  3. "context"
  4. "math/rand"
  5. "github.com/sagernet/sing/common/random"
  6. )
  7. func init() {
  8. random.InitializeSeed()
  9. }
  10. type idKey struct{}
  11. func ContextWithNewID(ctx context.Context) context.Context {
  12. return context.WithValue(ctx, (*idKey)(nil), rand.Uint32())
  13. }
  14. func IDFromContext(ctx context.Context) (uint32, bool) {
  15. id, loaded := ctx.Value((*idKey)(nil)).(uint32)
  16. return id, loaded
  17. }