增大服务最大容纳的定时器数

This commit is contained in:
boyce
2020-12-15 18:38:29 +08:00
parent c0cb20a5ef
commit 1eeffb2d05
2 changed files with 11 additions and 7 deletions

View File

@@ -15,7 +15,7 @@ import (
var closeSig chan bool var closeSig chan bool
var timerDispatcherLen = 10000 var timerDispatcherLen = 100000
type IService interface { type IService interface {
Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{})

View File

@@ -65,23 +65,26 @@ func StartTimer(minTimerInterval time.Duration,maxTimerNum int){
} }
func tickRoutine(minTimerInterval time.Duration){ func tickRoutine(minTimerInterval time.Duration){
var bContinue bool
for{ for{
time.Sleep(minTimerInterval) bContinue = tick()
tick() if bContinue == false {
time.Sleep(minTimerInterval)
}
} }
} }
func tick(){ func tick() bool{
now := Now() now := Now()
timerHeapLock.Lock() timerHeapLock.Lock()
if timerHeap.Len() <= 0 { // 没有任何定时器,立刻返回 if timerHeap.Len() <= 0 { // 没有任何定时器,立刻返回
timerHeapLock.Unlock() timerHeapLock.Unlock()
return return false
} }
nextFireTime := timerHeap.timers[0].fireTime nextFireTime := timerHeap.timers[0].fireTime
if nextFireTime.After(now) { // 没有到时间的定时器,返回 if nextFireTime.After(now) { // 没有到时间的定时器,返回
timerHeapLock.Unlock() timerHeapLock.Unlock()
return return false
} }
t := heap.Pop(&timerHeap).(*Timer) t := heap.Pop(&timerHeap).(*Timer)
@@ -89,10 +92,11 @@ func tick(){
if len(t.C)>= cap(t.C) { if len(t.C)>= cap(t.C) {
log.Error("Timer channel full!") log.Error("Timer channel full!")
return return true
} }
t.C <- t t.C <- t
return true
} }
func Now() time.Time{ func Now() time.Time{