优化系统goroutine使用,减少goroutine数量,增加goman查看goroutine数量指令
This commit is contained in:
@@ -2,7 +2,6 @@ package expires
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ItemMap = map[int64]bool
|
||||
@@ -12,14 +11,19 @@ type List struct {
|
||||
itemsMap map[int64]int64 // itemId => timestamp
|
||||
|
||||
locker sync.Mutex
|
||||
ticker *time.Ticker
|
||||
|
||||
gcCallback func(itemId int64)
|
||||
}
|
||||
|
||||
func NewList() *List {
|
||||
return &List{
|
||||
var list = &List{
|
||||
expireMap: map[int64]ItemMap{},
|
||||
itemsMap: map[int64]int64{},
|
||||
}
|
||||
|
||||
SharedManager.Add(list)
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func (this *List) Add(itemId int64, expiresAt int64) {
|
||||
@@ -56,33 +60,15 @@ func (this *List) GC(timestamp int64, callback func(itemId int64)) {
|
||||
itemMap := this.gcItems(timestamp)
|
||||
this.locker.Unlock()
|
||||
|
||||
for itemId := range itemMap {
|
||||
callback(itemId)
|
||||
if callback != nil {
|
||||
for itemId := range itemMap {
|
||||
callback(itemId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *List) StartGC(callback func(itemId int64)) {
|
||||
this.ticker = time.NewTicker(1 * time.Second)
|
||||
lastTimestamp := int64(0)
|
||||
for range this.ticker.C {
|
||||
timestamp := time.Now().Unix()
|
||||
if lastTimestamp == 0 {
|
||||
lastTimestamp = timestamp - 3600
|
||||
}
|
||||
|
||||
if timestamp >= lastTimestamp {
|
||||
for i := lastTimestamp; i <= timestamp; i++ {
|
||||
this.GC(i, callback)
|
||||
}
|
||||
} else {
|
||||
for i := timestamp; i <= lastTimestamp; i++ {
|
||||
this.GC(i, callback)
|
||||
}
|
||||
}
|
||||
|
||||
// 这样做是为了防止系统时钟突变
|
||||
lastTimestamp = timestamp
|
||||
}
|
||||
func (this *List) OnGC(callback func(itemId int64)) {
|
||||
this.gcCallback = callback
|
||||
}
|
||||
|
||||
func (this *List) removeItem(itemId int64) {
|
||||
|
||||
@@ -63,11 +63,13 @@ func TestList_Start_GC(t *testing.T) {
|
||||
list.Add(7, time.Now().Unix()+6)
|
||||
list.Add(8, time.Now().Unix()+6)
|
||||
|
||||
list.OnGC(func(itemId int64) {
|
||||
t.Log("gc:", itemId, timeutil.Format("H:i:s"))
|
||||
time.Sleep(2 * time.Second)
|
||||
})
|
||||
|
||||
go func() {
|
||||
list.StartGC(func(itemId int64) {
|
||||
t.Log("gc:", itemId, timeutil.Format("H:i:s"))
|
||||
time.Sleep(2 * time.Second)
|
||||
})
|
||||
SharedManager.Add(list)
|
||||
}()
|
||||
|
||||
time.Sleep(20 * time.Second)
|
||||
|
||||
71
internal/utils/expires/manager.go
Normal file
71
internal/utils/expires/manager.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var SharedManager = NewManager()
|
||||
|
||||
type Manager struct {
|
||||
listMap map[*List]bool
|
||||
locker sync.Mutex
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func NewManager() *Manager {
|
||||
var manager = &Manager{
|
||||
listMap: map[*List]bool{},
|
||||
ticker: time.NewTicker(1 * time.Second),
|
||||
}
|
||||
goman.New(func() {
|
||||
manager.init()
|
||||
})
|
||||
return manager
|
||||
}
|
||||
|
||||
func (this *Manager) init() {
|
||||
var lastTimestamp = int64(0)
|
||||
for range this.ticker.C {
|
||||
timestamp := time.Now().Unix()
|
||||
if lastTimestamp == 0 {
|
||||
lastTimestamp = timestamp - 3600
|
||||
}
|
||||
|
||||
if timestamp >= lastTimestamp {
|
||||
for i := lastTimestamp; i <= timestamp; i++ {
|
||||
this.locker.Lock()
|
||||
for list := range this.listMap {
|
||||
list.GC(i, list.gcCallback)
|
||||
}
|
||||
this.locker.Unlock()
|
||||
}
|
||||
} else {
|
||||
for i := timestamp; i <= lastTimestamp; i++ {
|
||||
this.locker.Lock()
|
||||
for list := range this.listMap {
|
||||
list.GC(i, list.gcCallback)
|
||||
}
|
||||
this.locker.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// 这样做是为了防止系统时钟突变
|
||||
lastTimestamp = timestamp
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Manager) Add(list *List) {
|
||||
this.locker.Lock()
|
||||
this.listMap[list] = true
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
func (this *Manager) Remove(list *List) {
|
||||
this.locker.Lock()
|
||||
delete(this.listMap, list)
|
||||
this.locker.Unlock()
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package utils
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -15,7 +16,9 @@ var SharedFreeHoursManager = NewFreeHoursManager()
|
||||
|
||||
func init() {
|
||||
events.On(events.EventLoaded, func() {
|
||||
go SharedFreeHoursManager.Start()
|
||||
goman.New(func() {
|
||||
SharedFreeHoursManager.Start()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
package utils
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 定时运行某个函数
|
||||
// Every 定时运行某个函数
|
||||
func Every(duration time.Duration, f func(ticker *Ticker)) *Ticker {
|
||||
ticker := NewTicker(duration)
|
||||
go func() {
|
||||
goman.New(func() {
|
||||
for ticker.Next() {
|
||||
f(ticker)
|
||||
}
|
||||
}()
|
||||
})
|
||||
|
||||
return ticker
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -9,12 +10,12 @@ var unixTimeMilli = time.Now().UnixMilli()
|
||||
|
||||
func init() {
|
||||
ticker := time.NewTicker(200 * time.Millisecond)
|
||||
go func() {
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
unixTime = time.Now().Unix()
|
||||
unixTimeMilli = time.Now().UnixMilli()
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
// UnixTime 最快获取时间戳的方式,通常用在不需要特别精确时间戳的场景
|
||||
|
||||
Reference in New Issue
Block a user