优化计数器

This commit is contained in:
刘祥超
2023-07-15 11:08:25 +08:00
parent 2079b0ebee
commit 1fdce3ef7e
4 changed files with 38 additions and 14 deletions

View File

@@ -3,10 +3,11 @@
package counters
import (
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
"github.com/cespare/xxhash"
"github.com/iwind/TeaGo/Tea"
"runtime"
"sync"
"time"
)
@@ -17,6 +18,7 @@ type Counter struct {
gcTicker *time.Ticker
gcIndex int
gcLocker sync.Mutex
}
// NewCounter create new counter
@@ -24,6 +26,8 @@ func NewCounter() *Counter {
var count = runtime.NumCPU() * 4
if count < 8 {
count = 8
} else if count > 128 {
count = 128
}
var itemMaps = []map[uint64]*Item{}
@@ -45,10 +49,7 @@ func (this *Counter) WithGC() *Counter {
if this.gcTicker != nil {
return this
}
this.gcTicker = time.NewTicker(10 * time.Second)
if Tea.IsTesting() {
this.gcTicker = time.NewTicker(1 * time.Second)
}
this.gcTicker = time.NewTicker(1 * time.Second)
go func() {
for range this.gcTicker.C {
this.GC()
@@ -141,6 +142,7 @@ func (this *Counter) TotalItems() int {
// GC garbage expired items
func (this *Counter) GC() {
this.gcLocker.Lock()
var gcIndex = this.gcIndex
this.gcIndex++
@@ -148,11 +150,15 @@ func (this *Counter) GC() {
this.gcIndex = 0
}
this.gcLocker.Unlock()
var currentTime = fasttime.Now().Unix()
this.locker.RLock(gcIndex)
var itemMap = this.itemMaps[gcIndex]
var expiredKeys = []uint64{}
for key, item := range itemMap {
if item.IsExpired() {
if item.IsExpired(currentTime) {
expiredKeys = append(expiredKeys, key)
}
}