diff --git a/common/collect/sized_queue.go b/common/collect/sized_queue.go deleted file mode 100644 index 6386ecef5..000000000 --- a/common/collect/sized_queue.go +++ /dev/null @@ -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 -} diff --git a/common/collect/sized_queue_test.go b/common/collect/sized_queue_test.go deleted file mode 100644 index 2456095ff..000000000 --- a/common/collect/sized_queue_test.go +++ /dev/null @@ -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) -} diff --git a/proxy/vmess/protocol/userset.go b/proxy/vmess/protocol/userset.go index b36df0a5a..2f12289c8 100644 --- a/proxy/vmess/protocol/userset.go +++ b/proxy/vmess/protocol/userset.go @@ -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" ) @@ -31,10 +30,10 @@ func (this Timestamp) HashBytes() []byte { } type idEntry struct { - id *vmess.ID - userIdx int - lastSec Timestamp - hashes *collect.SizedQueue + id *vmess.ID + userIdx int + lastSec Timestamp + 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++ } } @@ -101,19 +103,19 @@ func (us *TimedUserSet) AddUser(user *vmess.User) error { nowSec := time.Now().Unix() entry := &idEntry{ - id: user.ID, - userIdx: idx, - lastSec: Timestamp(nowSec - cacheDurationSec), - hashes: collect.NewSizedQueue(2*cacheDurationSec + 1), + id: user.ID, + userIdx: idx, + lastSec: Timestamp(nowSec - cacheDurationSec), + lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3), } us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry) us.ids = append(us.ids, entry) for _, alterid := range user.AlterIDs { entry := &idEntry{ - id: alterid, - userIdx: idx, - lastSec: Timestamp(nowSec - cacheDurationSec), - hashes: collect.NewSizedQueue(2*cacheDurationSec + 1), + id: alterid, + userIdx: idx, + lastSec: Timestamp(nowSec - cacheDurationSec), + lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3), } us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry) us.ids = append(us.ids, entry)