|
|
@@ -1,12 +1,16 @@
|
|
|
-package session
|
|
|
+// Package session provides functions for sessions of incoming requests.
|
|
|
+package session // import "v2ray.com/core/common/session"
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"math/rand"
|
|
|
)
|
|
|
|
|
|
+// ID of a session.
|
|
|
type ID uint32
|
|
|
|
|
|
+// NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
|
|
|
+// The generated ID will never be 0.
|
|
|
func NewID() ID {
|
|
|
for {
|
|
|
id := ID(rand.Uint32())
|
|
|
@@ -22,10 +26,12 @@ const (
|
|
|
idSessionKey sessionKey = iota
|
|
|
)
|
|
|
|
|
|
+// ContextWithID returns a new context with the given ID.
|
|
|
func ContextWithID(ctx context.Context, id ID) context.Context {
|
|
|
return context.WithValue(ctx, idSessionKey, id)
|
|
|
}
|
|
|
|
|
|
+// IDFromContext returns ID in this context, or 0 if not contained.
|
|
|
func IDFromContext(ctx context.Context) ID {
|
|
|
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
|
|
return id
|