2015-09-07 17:48:19 -04:00
|
|
|
package core
|
|
|
|
|
2015-09-08 07:18:55 -04:00
|
|
|
import (
|
2015-09-14 12:19:17 -04:00
|
|
|
"time"
|
2015-09-16 15:13:13 -04:00
|
|
|
|
|
|
|
v2hash "github.com/v2ray/v2ray-core/hash"
|
2015-09-14 12:19:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
updateIntervalSec = 10
|
|
|
|
cacheDurationSec = 120
|
2015-09-08 07:18:55 -04:00
|
|
|
)
|
|
|
|
|
2015-09-14 13:03:31 -04:00
|
|
|
type UserSet interface {
|
|
|
|
AddUser(user User) error
|
2015-09-14 19:32:55 -04:00
|
|
|
GetUser(timeHash []byte) (*ID, int64, bool)
|
2015-09-14 13:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type TimedUserSet struct {
|
2015-09-14 12:19:17 -04:00
|
|
|
validUserIds []ID
|
2015-09-14 19:32:55 -04:00
|
|
|
userHashes map[string]indexTimePair
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexTimePair struct {
|
2015-09-15 18:06:22 -04:00
|
|
|
index int
|
|
|
|
timeSec int64
|
2015-09-14 12:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type hashEntry struct {
|
|
|
|
hash string
|
|
|
|
timeSec int64
|
2015-09-08 07:18:55 -04:00
|
|
|
}
|
|
|
|
|
2015-09-14 13:03:31 -04:00
|
|
|
func NewTimedUserSet() UserSet {
|
|
|
|
vuSet := new(TimedUserSet)
|
2015-09-12 16:11:54 -04:00
|
|
|
vuSet.validUserIds = make([]ID, 0, 16)
|
2015-09-14 19:32:55 -04:00
|
|
|
vuSet.userHashes = make(map[string]indexTimePair)
|
2015-09-14 12:19:17 -04:00
|
|
|
|
|
|
|
go vuSet.updateUserHash(time.Tick(updateIntervalSec * time.Second))
|
2015-09-08 07:18:55 -04:00
|
|
|
return vuSet
|
|
|
|
}
|
|
|
|
|
2015-09-14 13:03:31 -04:00
|
|
|
func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
|
2015-09-14 12:19:17 -04:00
|
|
|
now := time.Now().UTC()
|
|
|
|
lastSec := now.Unix() - cacheDurationSec
|
|
|
|
|
2015-09-15 07:00:42 -04:00
|
|
|
hash2Remove := make(chan hashEntry, cacheDurationSec*3*len(us.validUserIds))
|
2015-09-14 16:45:55 -04:00
|
|
|
lastSec2Remove := now.Unix()
|
2015-09-16 15:13:13 -04:00
|
|
|
idHash := v2hash.NewTimeHash(v2hash.HMACHash{})
|
2015-09-14 12:19:17 -04:00
|
|
|
for {
|
|
|
|
now := <-tick
|
|
|
|
nowSec := now.UTC().Unix()
|
2015-09-15 18:06:22 -04:00
|
|
|
|
|
|
|
remove2Sec := nowSec - cacheDurationSec
|
2015-09-14 12:19:17 -04:00
|
|
|
if remove2Sec > lastSec2Remove {
|
|
|
|
for lastSec2Remove+1 < remove2Sec {
|
|
|
|
entry := <-hash2Remove
|
|
|
|
lastSec2Remove = entry.timeSec
|
|
|
|
delete(us.userHashes, entry.hash)
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
|
|
|
|
for lastSec < nowSec+cacheDurationSec {
|
|
|
|
for idx, id := range us.validUserIds {
|
2015-09-16 15:13:13 -04:00
|
|
|
idHash := idHash.Hash(id.Bytes, lastSec)
|
2015-09-14 16:00:03 -04:00
|
|
|
hash2Remove <- hashEntry{string(idHash), lastSec}
|
2015-09-14 19:32:55 -04:00
|
|
|
us.userHashes[string(idHash)] = indexTimePair{idx, lastSec}
|
2015-09-14 12:19:17 -04:00
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
lastSec++
|
|
|
|
}
|
2015-09-14 12:19:17 -04:00
|
|
|
}
|
2015-09-08 07:18:55 -04:00
|
|
|
}
|
|
|
|
|
2015-09-14 13:03:31 -04:00
|
|
|
func (us *TimedUserSet) AddUser(user User) error {
|
2015-09-08 07:18:55 -04:00
|
|
|
id := user.Id
|
|
|
|
us.validUserIds = append(us.validUserIds, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-14 19:32:55 -04:00
|
|
|
func (us TimedUserSet) GetUser(userHash []byte) (*ID, int64, bool) {
|
|
|
|
pair, found := us.userHashes[string(userHash)]
|
2015-09-08 07:18:55 -04:00
|
|
|
if found {
|
2015-09-14 19:32:55 -04:00
|
|
|
return &us.validUserIds[pair.index], pair.timeSec, true
|
2015-09-08 07:18:55 -04:00
|
|
|
}
|
2015-09-14 19:32:55 -04:00
|
|
|
return nil, 0, false
|
2015-09-07 17:48:19 -04:00
|
|
|
}
|