优化系统goroutine使用,减少goroutine数量,增加goman查看goroutine数量指令

This commit is contained in:
刘祥超
2021-12-08 15:17:45 +08:00
parent 24fbd740b5
commit 1279f0d394
48 changed files with 469 additions and 146 deletions

View File

@@ -1,7 +1,6 @@
package ttlcache
import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
"time"
)
@@ -19,7 +18,6 @@ type Cache struct {
maxItems int
gcPieceIndex int
ticker *utils.Ticker
}
func NewCache(opt ...OptionInterface) *Cache {
@@ -56,13 +54,8 @@ func NewCache(opt ...OptionInterface) *Cache {
cache.pieces = append(cache.pieces, NewPiece(maxItems/countPieces))
}
// start timer
go func() {
cache.ticker = utils.NewTicker(5 * time.Second)
for cache.ticker.Next() {
cache.GC()
}
}()
// Add to manager
SharedManager.Add(cache)
return cache
}
@@ -149,12 +142,10 @@ func (this *Cache) Clean() {
}
func (this *Cache) Destroy() {
SharedManager.Remove(this)
this.isDestroyed = true
if this.ticker != nil {
this.ticker.Stop()
this.ticker = nil
}
for _, piece := range this.pieces {
piece.Destroy()
}

View File

@@ -123,13 +123,18 @@ func TestCache_GC(t *testing.T) {
func TestCache_GC2(t *testing.T) {
runtime.GOMAXPROCS(1)
cache := NewCache()
cache1 := NewCache(NewPiecesOption(32))
for i := 0; i < 1_000_000; i++ {
cache.Write(strconv.Itoa(i), i, time.Now().Unix()+int64(rands.Int(0, 100)))
cache1.Write(strconv.Itoa(i), i, time.Now().Unix()+int64(rands.Int(0, 10)))
}
cache2 := NewCache(NewPiecesOption(5))
for i := 0; i < 1_000_000; i++ {
cache2.Write(strconv.Itoa(i), i, time.Now().Unix()+int64(rands.Int(0, 10)))
}
for i := 0; i < 100; i++ {
t.Log(cache.Count(), "items")
t.Log(cache1.Count(), "items", cache2.Count(), "items")
time.Sleep(1 * time.Second)
}
}

View File

@@ -0,0 +1,53 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ttlcache
import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"sync"
"time"
)
var SharedManager = NewManager()
type Manager struct {
ticker *time.Ticker
locker sync.Mutex
cacheMap map[*Cache]bool
}
func NewManager() *Manager {
var manager = &Manager{
ticker: time.NewTicker(3 * time.Second),
cacheMap: map[*Cache]bool{},
}
goman.New(func() {
manager.init()
})
return manager
}
func (this *Manager) init() {
for range this.ticker.C {
this.locker.Lock()
for cache := range this.cacheMap {
cache.GC()
}
this.locker.Unlock()
}
}
func (this *Manager) Add(cache *Cache) {
this.locker.Lock()
this.cacheMap[cache] = true
this.locker.Unlock()
}
func (this *Manager) Remove(cache *Cache) {
this.locker.Lock()
delete(this.cacheMap, cache)
this.locker.Unlock()
}