mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
35 lines
460 B
Go
35 lines
460 B
Go
|
package session
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"math/rand"
|
||
|
)
|
||
|
|
||
|
type ID uint32
|
||
|
|
||
|
func NewID() ID {
|
||
|
for {
|
||
|
id := ID(rand.Uint32())
|
||
|
if id != 0 {
|
||
|
return id
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type sessionKey int
|
||
|
|
||
|
const (
|
||
|
idSessionKey sessionKey = iota
|
||
|
)
|
||
|
|
||
|
func ContextWithID(ctx context.Context, id ID) context.Context {
|
||
|
return context.WithValue(ctx, idSessionKey, id)
|
||
|
}
|
||
|
|
||
|
func IDFromContext(ctx context.Context) ID {
|
||
|
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
||
|
return id
|
||
|
}
|
||
|
return 0
|
||
|
}
|