优化定时器

This commit is contained in:
boyce
2020-10-28 11:30:22 +08:00
parent 019989fe2c
commit 7de290b553
5 changed files with 24 additions and 17 deletions

View File

@@ -44,7 +44,7 @@ func usage(val interface{}) error{
return nil return nil
} }
fmt.Fprintf(os.Stderr, `orgin version: orgin/2.12.20201024 fmt.Fprintf(os.Stderr, `orgin version: orgin/2.13.20201028
Usage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060] Usage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]
`) `)
console.PrintDefaults() console.PrintDefaults()

View File

@@ -128,11 +128,11 @@ func (slf *Module) ReleaseModule(moduleId int64){
pModule.self.OnRelease() pModule.self.OnRelease()
log.Debug("Release module %s.",slf.GetModuleName()) log.Debug("Release module %s.",slf.GetModuleName())
for pTimer,_ := range pModule.mapActiveTimer { for pTimer,_ := range pModule.mapActiveTimer {
pTimer.Stop() pTimer.Close()
} }
for pCron,_ := range pModule.mapActiveCron { for pCron,_ := range pModule.mapActiveCron {
pCron.Stop() pCron.Close()
} }
delete(slf.child,moduleId) delete(slf.child,moduleId)

View File

@@ -139,7 +139,7 @@ func (slf *Service) Run() {
analyzer = nil analyzer = nil
} }
case t := <- slf.dispatcher.ChanTimer: case t := <- slf.dispatcher.ChanTimer:
if t.IsStop() == false { if t.IsClose() == false {
if slf.profiler != nil { if slf.profiler != nil {
analyzer = slf.profiler.Push(fmt.Sprintf("Timer_%s", t.AdditionData.(*timer.Timer).GetFunctionName())) analyzer = slf.profiler.Push(fmt.Sprintf("Timer_%s", t.AdditionData.(*timer.Timer).GetFunctionName()))
} }

View File

@@ -28,8 +28,8 @@ type Timer struct {
name string name string
} }
func (t *Timer) Stop() { func (t *Timer) Close() {
t.t.Stop() t.t.Close()
t.cb = nil t.cb = nil
} }
@@ -78,9 +78,9 @@ type Cron struct {
t *Timer t *Timer
} }
func (c *Cron) Stop() { func (c *Cron) Close() {
if c.t != nil { if c.t != nil {
c.t.Stop() c.t.Close()
} }
} }

View File

@@ -100,26 +100,32 @@ type stNode struct {
type Timer struct { type Timer struct {
stNode stNode
expireTicks int64 //到期滴答数 expireTicks int64 //到期滴答数
isClose int32 //是否已经关闭0表示开启状态,1表示关闭 end int32 //是否已经关闭0表示开启状态,1表示关闭
bClose bool //是否关闭
C chan *Timer //定时器管道 C chan *Timer //定时器管道
AdditionData interface{} //定时器附加数据 AdditionData interface{} //定时器附加数据
} }
//停止停时器 //停止停时器
func (timer *Timer) Stop(){ func (timer *Timer) Close(){
timer.bClose = true
//将关闭标志设为1关闭状态 //将关闭标志设为1关闭状态
if atomic.SwapInt32(&timer.isClose,1) == 0 { if atomic.SwapInt32(&timer.end,1) == 0 {
chanStopTimer<-timer chanStopTimer<-timer
} }
} }
//定时器是否已经停止 //定时器是否已经停止
func (timer *Timer) IsStop() bool { func (timer *Timer) IsClose() bool {
return atomic.LoadInt32(&timer.isClose) != 0 return timer.bClose
}
func (timer *Timer) IsEnd() bool{
return atomic.LoadInt32(&timer.end) !=0
} }
func (timer *Timer) doTimeout(){ func (timer *Timer) doTimeout(){
if atomic.SwapInt32(&timer.isClose,1) != 0 { if atomic.SwapInt32(&timer.end,1) != 0 {
return return
} }
timer.prev = nil timer.prev = nil
@@ -244,7 +250,8 @@ func GetNow() int64 {
//创建定时器 ticks表示多少个ticks单位到期, additionData定时器附带数据, c到时通知的channel //创建定时器 ticks表示多少个ticks单位到期, additionData定时器附带数据, c到时通知的channel
func (t *timeWheel) newTimer(ticks int64,additionData interface{},c chan *Timer) *Timer{ func (t *timeWheel) newTimer(ticks int64,additionData interface{},c chan *Timer) *Timer{
pTimer := timerPool.Get().(*Timer) pTimer := timerPool.Get().(*Timer)
pTimer.isClose = 0 pTimer.end = 0
pTimer.bClose = false
pTimer.C = c pTimer.C = c
pTimer.AdditionData = additionData pTimer.AdditionData = additionData
pTimer.expireTicks = ticks+t.currentTicks pTimer.expireTicks = ticks+t.currentTicks
@@ -330,7 +337,7 @@ func (t *timeWheel) TickOneFrame(){
for currTimer := slot.timer.next;currTimer!=slot.timer; { for currTimer := slot.timer.next;currTimer!=slot.timer; {
nextTimer = currTimer.next nextTimer = currTimer.next
//如果当前定时器已经停止,不做任何处理.否则放入到定时器的channel //如果当前定时器已经停止,不做任何处理.否则放入到定时器的channel
if currTimer.IsStop() == true { if currTimer.IsEnd() == true {
currTimer = nextTimer currTimer = nextTimer
continue continue
} }
@@ -384,7 +391,7 @@ func (t *timeWheel) cascade(wheelIndex int) {
nextTimer:=currentTimer.next nextTimer:=currentTimer.next
//如果到时,直接送到channel //如果到时,直接送到channel
if currentTimer.expireTicks<= t.currentTicks { if currentTimer.expireTicks<= t.currentTicks {
if currentTimer.IsStop() == false { if currentTimer.IsEnd() == false {
currentTimer.doTimeout() currentTimer.doTimeout()
} }
}else{//否则重新添加,会加到下一级轮中 }else{//否则重新添加,会加到下一级轮中