mirror of https://github.com/v2ray/v2ray-core
simplify validity map
parent
5df2a1c6e6
commit
526c4558ca
|
@ -2,7 +2,7 @@ package collect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/serial"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
@ -19,20 +19,17 @@ type entry struct {
|
||||||
type ValidityMap struct {
|
type ValidityMap struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
cache map[string]Validity
|
cache map[string]Validity
|
||||||
cleanupIntervalSec int
|
opCount int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewValidityMap(cleanupIntervalSec int) *ValidityMap {
|
func NewValidityMap(cleanupIntervalSec int) *ValidityMap {
|
||||||
instance := &ValidityMap{
|
instance := &ValidityMap{
|
||||||
cache: make(map[string]Validity),
|
cache: make(map[string]Validity),
|
||||||
cleanupIntervalSec: cleanupIntervalSec,
|
|
||||||
}
|
}
|
||||||
go instance.cleanup()
|
|
||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ValidityMap) cleanup() {
|
func (this *ValidityMap) cleanup() {
|
||||||
for range time.Tick(time.Duration(this.cleanupIntervalSec) * time.Second) {
|
|
||||||
entry2Remove := make([]entry, 0, 128)
|
entry2Remove := make([]entry, 0, 128)
|
||||||
this.RLock()
|
this.RLock()
|
||||||
for key, value := range this.cache {
|
for key, value := range this.cache {
|
||||||
|
@ -52,13 +49,17 @@ func (this *ValidityMap) cleanup() {
|
||||||
this.Unlock()
|
this.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ValidityMap) Set(key serial.String, value Validity) {
|
func (this *ValidityMap) Set(key serial.String, value Validity) {
|
||||||
this.Lock()
|
this.Lock()
|
||||||
this.cache[key.String()] = value
|
this.cache[key.String()] = value
|
||||||
this.Unlock()
|
this.Unlock()
|
||||||
|
opCount := atomic.AddInt32(&this.opCount, 1)
|
||||||
|
if opCount > 1000 {
|
||||||
|
atomic.StoreInt32(&this.opCount, 0)
|
||||||
|
go this.cleanup()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ValidityMap) Get(key serial.String) Validity {
|
func (this *ValidityMap) Get(key serial.String) Validity {
|
||||||
|
|
Loading…
Reference in New Issue