Merge pull request #1350 from comwrg/refactor-validator

more readable for func `generateNewHashes` in `proxy/vmess/validator.go`
pull/1352/head
Victoria Raymond 6 years ago committed by GitHub
commit 59fa890332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -56,13 +56,14 @@ func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) { func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
var hashValue [16]byte var hashValue [16]byte
genEndSec := nowSec + cacheDurationSec
genHashForID := func(id *protocol.ID) { genHashForID := func(id *protocol.ID) {
idHash := v.hasher(id.Bytes()) idHash := v.hasher(id.Bytes())
lastSec := user.lastSec genBeginSec := user.lastSec
if lastSec < nowSec-cacheDurationSec*2 { if genBeginSec < nowSec-cacheDurationSec {
lastSec = nowSec - cacheDurationSec*2 genBeginSec = nowSec - cacheDurationSec
} }
for ts := lastSec; ts <= nowSec; ts++ { for ts := genBeginSec; ts <= genEndSec; ts++ {
common.Must2(idHash.Write(ts.Bytes(nil))) common.Must2(idHash.Write(ts.Bytes(nil)))
idHash.Sum(hashValue[:0]) idHash.Sum(hashValue[:0])
idHash.Reset() idHash.Reset()
@ -80,7 +81,7 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *
for _, id := range account.AlterIDs { for _, id := range account.AlterIDs {
genHashForID(id) genHashForID(id)
} }
user.lastSec = nowSec user.lastSec = genEndSec
} }
func (v *TimedUserValidator) removeExpiredHashes(expire uint32) { func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
@ -93,7 +94,7 @@ func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
func (v *TimedUserValidator) updateUserHash() { func (v *TimedUserValidator) updateUserHash() {
now := time.Now() now := time.Now()
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec) nowSec := protocol.Timestamp(now.Unix())
v.Lock() v.Lock()
defer v.Unlock() defer v.Unlock()
@ -118,7 +119,7 @@ func (v *TimedUserValidator) Add(u *protocol.MemoryUser) error {
lastSec: protocol.Timestamp(nowSec - cacheDurationSec), lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
} }
v.users = append(v.users, uu) v.users = append(v.users, uu)
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu) v.generateNewHashes(protocol.Timestamp(nowSec), uu)
return nil return nil
} }

Loading…
Cancel
Save