You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/userset.go

87 lines
1.8 KiB

9 years ago
package core
import (
"time"
v2hash "github.com/v2ray/v2ray-core/hash"
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
9 years ago
type UserSet interface {
AddUser(user User) error
GetUser(timeHash []byte) (*ID, int64, bool)
9 years ago
}
type TimedUserSet struct {
validUserIds []ID
userHashes map[string]indexTimePair
}
type indexTimePair struct {
9 years ago
index int
timeSec int64
}
type hashEntry struct {
hash string
timeSec int64
}
9 years ago
func NewTimedUserSet() UserSet {
vuSet := new(TimedUserSet)
vuSet.validUserIds = make([]ID, 0, 16)
vuSet.userHashes = make(map[string]indexTimePair)
go vuSet.updateUserHash(time.Tick(updateIntervalSec * time.Second))
return vuSet
}
9 years ago
func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
now := time.Now().UTC()
lastSec := now.Unix() - cacheDurationSec
hash2Remove := make(chan hashEntry, cacheDurationSec*3*len(us.validUserIds))
lastSec2Remove := now.Unix()
idHash := v2hash.NewTimeHash(v2hash.HMACHash{})
for {
now := <-tick
nowSec := now.UTC().Unix()
9 years ago
remove2Sec := nowSec - cacheDurationSec
if remove2Sec > lastSec2Remove {
for lastSec2Remove+1 < remove2Sec {
entry := <-hash2Remove
lastSec2Remove = entry.timeSec
delete(us.userHashes, entry.hash)
}
}
9 years ago
for lastSec < nowSec+cacheDurationSec {
for idx, id := range us.validUserIds {
idHash := idHash.Hash(id.Bytes, lastSec)
hash2Remove <- hashEntry{string(idHash), lastSec}
us.userHashes[string(idHash)] = indexTimePair{idx, lastSec}
}
9 years ago
lastSec++
}
}
}
9 years ago
func (us *TimedUserSet) AddUser(user User) error {
id := user.Id
us.validUserIds = append(us.validUserIds, id)
return nil
}
func (us TimedUserSet) GetUser(userHash []byte) (*ID, int64, bool) {
pair, found := us.userHashes[string(userHash)]
if found {
return &us.validUserIds[pair.index], pair.timeSec, true
}
return nil, 0, false
9 years ago
}