mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
remove cache of existing user hashes
This commit is contained in:
parent
1a10f7c4da
commit
4624de091b
@ -1,24 +0,0 @@
|
||||
package collect
|
||||
|
||||
type SizedQueue struct {
|
||||
elements []interface{}
|
||||
nextPos int
|
||||
}
|
||||
|
||||
func NewSizedQueue(size int) *SizedQueue {
|
||||
return &SizedQueue{
|
||||
elements: make([]interface{}, size),
|
||||
nextPos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts a new element into the queue and pop out the first element if queue is full.
|
||||
func (this *SizedQueue) Put(element interface{}) interface{} {
|
||||
res := this.elements[this.nextPos]
|
||||
this.elements[this.nextPos] = element
|
||||
this.nextPos++
|
||||
if this.nextPos == len(this.elements) {
|
||||
this.nextPos = 0
|
||||
}
|
||||
return res
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package collect_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/collect"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
|
||||
func TestSizedQueue(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
queue := collect.NewSizedQueue(2)
|
||||
assert.Pointer(queue.Put(1)).IsNil()
|
||||
assert.Pointer(queue.Put(2)).IsNil()
|
||||
assert.Int(queue.Put(3).(int)).Equals(1)
|
||||
}
|
@ -4,7 +4,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/collect"
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||
)
|
||||
@ -34,7 +33,7 @@ type idEntry struct {
|
||||
id *vmess.ID
|
||||
userIdx int
|
||||
lastSec Timestamp
|
||||
hashes *collect.SizedQueue
|
||||
lastSecRemoval Timestamp
|
||||
}
|
||||
|
||||
type UserSet interface {
|
||||
@ -67,21 +66,24 @@ func NewTimedUserSet() UserSet {
|
||||
|
||||
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
|
||||
var hashValue [16]byte
|
||||
var hashValueRemoval [16]byte
|
||||
idHash := IDHash(entry.id.Bytes())
|
||||
for entry.lastSec <= nowSec {
|
||||
idHash.Write(entry.lastSec.Bytes())
|
||||
idHash.Sum(hashValue[:0])
|
||||
idHash.Reset()
|
||||
|
||||
hash2Remove := entry.hashes.Put(hashValue)
|
||||
idHash.Write(entry.lastSecRemoval.Bytes())
|
||||
idHash.Sum(hashValueRemoval[:0])
|
||||
idHash.Reset()
|
||||
|
||||
us.access.Lock()
|
||||
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
|
||||
if hash2Remove != nil {
|
||||
delete(us.userHash, hash2Remove.([16]byte))
|
||||
}
|
||||
delete(us.userHash, hashValueRemoval)
|
||||
us.access.Unlock()
|
||||
|
||||
entry.lastSec++
|
||||
entry.lastSecRemoval++
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,7 +106,7 @@ func (us *TimedUserSet) AddUser(user *vmess.User) error {
|
||||
id: user.ID,
|
||||
userIdx: idx,
|
||||
lastSec: Timestamp(nowSec - cacheDurationSec),
|
||||
hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
|
||||
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
|
||||
}
|
||||
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
|
||||
us.ids = append(us.ids, entry)
|
||||
@ -113,7 +115,7 @@ func (us *TimedUserSet) AddUser(user *vmess.User) error {
|
||||
id: alterid,
|
||||
userIdx: idx,
|
||||
lastSec: Timestamp(nowSec - cacheDurationSec),
|
||||
hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
|
||||
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
|
||||
}
|
||||
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
|
||||
us.ids = append(us.ids, entry)
|
||||
|
Loading…
Reference in New Issue
Block a user