From 7de290b553488d548e775701624a98cdd62b407c Mon Sep 17 00:00:00 2001 From: boyce Date: Wed, 28 Oct 2020 11:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node/node.go | 2 +- service/module.go | 4 ++-- service/service.go | 2 +- util/timer/timer.go | 8 ++++---- util/timewheel/timewheel.go | 25 ++++++++++++++++--------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/node/node.go b/node/node.go index ee2d587..05d8dee 100644 --- a/node/node.go +++ b/node/node.go @@ -44,7 +44,7 @@ func usage(val interface{}) error{ 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] `) console.PrintDefaults() diff --git a/service/module.go b/service/module.go index 3ce48cc..b360729 100644 --- a/service/module.go +++ b/service/module.go @@ -128,11 +128,11 @@ func (slf *Module) ReleaseModule(moduleId int64){ pModule.self.OnRelease() log.Debug("Release module %s.",slf.GetModuleName()) for pTimer,_ := range pModule.mapActiveTimer { - pTimer.Stop() + pTimer.Close() } for pCron,_ := range pModule.mapActiveCron { - pCron.Stop() + pCron.Close() } delete(slf.child,moduleId) diff --git a/service/service.go b/service/service.go index dbd4412..869cf13 100644 --- a/service/service.go +++ b/service/service.go @@ -139,7 +139,7 @@ func (slf *Service) Run() { analyzer = nil } case t := <- slf.dispatcher.ChanTimer: - if t.IsStop() == false { + if t.IsClose() == false { if slf.profiler != nil { analyzer = slf.profiler.Push(fmt.Sprintf("Timer_%s", t.AdditionData.(*timer.Timer).GetFunctionName())) } diff --git a/util/timer/timer.go b/util/timer/timer.go index 1fbddff..40c2ae3 100644 --- a/util/timer/timer.go +++ b/util/timer/timer.go @@ -28,8 +28,8 @@ type Timer struct { name string } -func (t *Timer) Stop() { - t.t.Stop() +func (t *Timer) Close() { + t.t.Close() t.cb = nil } @@ -78,9 +78,9 @@ type Cron struct { t *Timer } -func (c *Cron) Stop() { +func (c *Cron) Close() { if c.t != nil { - c.t.Stop() + c.t.Close() } } diff --git a/util/timewheel/timewheel.go b/util/timewheel/timewheel.go index 1da8b02..969a834 100644 --- a/util/timewheel/timewheel.go +++ b/util/timewheel/timewheel.go @@ -100,26 +100,32 @@ type stNode struct { type Timer struct { stNode expireTicks int64 //到期滴答数 - isClose int32 //是否已经关闭0表示开启状态,1表示关闭 + end int32 //是否已经关闭0表示开启状态,1表示关闭 + bClose bool //是否关闭 C chan *Timer //定时器管道 AdditionData interface{} //定时器附加数据 } //停止停时器 -func (timer *Timer) Stop(){ +func (timer *Timer) Close(){ + timer.bClose = true //将关闭标志设为1关闭状态 - if atomic.SwapInt32(&timer.isClose,1) == 0 { + if atomic.SwapInt32(&timer.end,1) == 0 { chanStopTimer<-timer } } //定时器是否已经停止 -func (timer *Timer) IsStop() bool { - return atomic.LoadInt32(&timer.isClose) != 0 +func (timer *Timer) IsClose() bool { + return timer.bClose +} + +func (timer *Timer) IsEnd() bool{ + return atomic.LoadInt32(&timer.end) !=0 } func (timer *Timer) doTimeout(){ - if atomic.SwapInt32(&timer.isClose,1) != 0 { + if atomic.SwapInt32(&timer.end,1) != 0 { return } timer.prev = nil @@ -244,7 +250,8 @@ func GetNow() int64 { //创建定时器 ticks表示多少个ticks单位到期, additionData定时器附带数据, c到时通知的channel func (t *timeWheel) newTimer(ticks int64,additionData interface{},c chan *Timer) *Timer{ pTimer := timerPool.Get().(*Timer) - pTimer.isClose = 0 + pTimer.end = 0 + pTimer.bClose = false pTimer.C = c pTimer.AdditionData = additionData pTimer.expireTicks = ticks+t.currentTicks @@ -330,7 +337,7 @@ func (t *timeWheel) TickOneFrame(){ for currTimer := slot.timer.next;currTimer!=slot.timer; { nextTimer = currTimer.next //如果当前定时器已经停止,不做任何处理.否则放入到定时器的channel - if currTimer.IsStop() == true { + if currTimer.IsEnd() == true { currTimer = nextTimer continue } @@ -384,7 +391,7 @@ func (t *timeWheel) cascade(wheelIndex int) { nextTimer:=currentTimer.next //如果到时,直接送到channel if currentTimer.expireTicks<= t.currentTicks { - if currentTimer.IsStop() == false { + if currentTimer.IsEnd() == false { currentTimer.doTimeout() } }else{//否则重新添加,会加到下一级轮中