mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-02-20 23:47:21 -05:00
lock protected user validator
This commit is contained in:
parent
6847139ae3
commit
0327113fb8
@ -28,10 +28,11 @@ type UserValidator interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TimedUserValidator struct {
|
type TimedUserValidator struct {
|
||||||
|
sync.RWMutex
|
||||||
|
running bool
|
||||||
validUsers []*User
|
validUsers []*User
|
||||||
userHash map[[16]byte]*indexTimePair
|
userHash map[[16]byte]*indexTimePair
|
||||||
ids []*idEntry
|
ids []*idEntry
|
||||||
access sync.RWMutex
|
|
||||||
hasher IDHash
|
hasher IDHash
|
||||||
cancel *signal.CancelSignal
|
cancel *signal.CancelSignal
|
||||||
}
|
}
|
||||||
@ -45,9 +46,9 @@ func NewTimedUserValidator(hasher IDHash) UserValidator {
|
|||||||
tus := &TimedUserValidator{
|
tus := &TimedUserValidator{
|
||||||
validUsers: make([]*User, 0, 16),
|
validUsers: make([]*User, 0, 16),
|
||||||
userHash: make(map[[16]byte]*indexTimePair, 512),
|
userHash: make(map[[16]byte]*indexTimePair, 512),
|
||||||
access: sync.RWMutex{},
|
|
||||||
ids: make([]*idEntry, 0, 512),
|
ids: make([]*idEntry, 0, 512),
|
||||||
hasher: hasher,
|
hasher: hasher,
|
||||||
|
running: true,
|
||||||
cancel: signal.NewCloseSignal(),
|
cancel: signal.NewCloseSignal(),
|
||||||
}
|
}
|
||||||
go tus.updateUserHash(updateIntervalSec * time.Second)
|
go tus.updateUserHash(updateIntervalSec * time.Second)
|
||||||
@ -55,9 +56,21 @@ func NewTimedUserValidator(hasher IDHash) UserValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *TimedUserValidator) Release() {
|
func (this *TimedUserValidator) Release() {
|
||||||
|
if !this.running {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.cancel.Cancel()
|
this.cancel.Cancel()
|
||||||
<-this.cancel.WaitForDone()
|
<-this.cancel.WaitForDone()
|
||||||
|
|
||||||
|
this.Lock()
|
||||||
|
defer this.Unlock()
|
||||||
|
|
||||||
|
if !this.running {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.running = false
|
||||||
this.validUsers = nil
|
this.validUsers = nil
|
||||||
this.userHash = nil
|
this.userHash = nil
|
||||||
this.ids = nil
|
this.ids = nil
|
||||||
@ -78,10 +91,10 @@ func (this *TimedUserValidator) generateNewHashes(nowSec Timestamp, idx int, ent
|
|||||||
idHash.Sum(hashValueRemoval[:0])
|
idHash.Sum(hashValueRemoval[:0])
|
||||||
idHash.Reset()
|
idHash.Reset()
|
||||||
|
|
||||||
this.access.Lock()
|
this.Lock()
|
||||||
this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
|
this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
|
||||||
delete(this.userHash, hashValueRemoval)
|
delete(this.userHash, hashValueRemoval)
|
||||||
this.access.Unlock()
|
this.Unlock()
|
||||||
|
|
||||||
entry.lastSec++
|
entry.lastSec++
|
||||||
entry.lastSecRemoval++
|
entry.lastSecRemoval++
|
||||||
@ -134,8 +147,12 @@ func (this *TimedUserValidator) Add(user *User) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *TimedUserValidator) Get(userHash []byte) (*User, Timestamp, bool) {
|
func (this *TimedUserValidator) Get(userHash []byte) (*User, Timestamp, bool) {
|
||||||
defer this.access.RUnlock()
|
defer this.RUnlock()
|
||||||
this.access.RLock()
|
this.RLock()
|
||||||
|
|
||||||
|
if !this.running {
|
||||||
|
return nil, 0, false
|
||||||
|
}
|
||||||
var fixedSizeHash [16]byte
|
var fixedSizeHash [16]byte
|
||||||
copy(fixedSizeHash[:], userHash)
|
copy(fixedSizeHash[:], userHash)
|
||||||
pair, found := this.userHash[fixedSizeHash]
|
pair, found := this.userHash[fixedSizeHash]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user