From 1eeffb2d05eeedba785f6e02596b9064d10edd7e Mon Sep 17 00:00:00 2001 From: boyce Date: Tue, 15 Dec 2020 18:38:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=A4=A7=E6=9C=8D=E5=8A=A1=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AE=B9=E7=BA=B3=E7=9A=84=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/service.go | 2 +- util/timer/heap.go | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/service/service.go b/service/service.go index 2fd98ab..ff45f33 100644 --- a/service/service.go +++ b/service/service.go @@ -15,7 +15,7 @@ import ( var closeSig chan bool -var timerDispatcherLen = 10000 +var timerDispatcherLen = 100000 type IService interface { Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) diff --git a/util/timer/heap.go b/util/timer/heap.go index 05ada2a..d796f1d 100644 --- a/util/timer/heap.go +++ b/util/timer/heap.go @@ -65,23 +65,26 @@ func StartTimer(minTimerInterval time.Duration,maxTimerNum int){ } func tickRoutine(minTimerInterval time.Duration){ + var bContinue bool for{ - time.Sleep(minTimerInterval) - tick() + bContinue = tick() + if bContinue == false { + time.Sleep(minTimerInterval) + } } } -func tick(){ +func tick() bool{ now := Now() timerHeapLock.Lock() if timerHeap.Len() <= 0 { // 没有任何定时器,立刻返回 timerHeapLock.Unlock() - return + return false } nextFireTime := timerHeap.timers[0].fireTime if nextFireTime.After(now) { // 没有到时间的定时器,返回 timerHeapLock.Unlock() - return + return false } t := heap.Pop(&timerHeap).(*Timer) @@ -89,10 +92,11 @@ func tick(){ if len(t.C)>= cap(t.C) { log.Error("Timer channel full!") - return + return true } t.C <- t + return true } func Now() time.Time{