mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化定时器
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duanhf2012/origin/log"
|
"github.com/duanhf2012/origin/log"
|
||||||
"github.com/duanhf2012/origin/network"
|
"github.com/duanhf2012/origin/network"
|
||||||
"github.com/duanhf2012/origin/util/timewheel"
|
"github.com/duanhf2012/origin/util/timer"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -62,18 +62,17 @@ func (client *Client) Connect(id int,addr string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) startCheckRpcCallTimer(){
|
func (client *Client) startCheckRpcCallTimer(){
|
||||||
timer:=timewheel.NewTimer(3*time.Second)
|
t:=timer.NewTimer(3*time.Second)
|
||||||
for{
|
for{
|
||||||
select {
|
select {
|
||||||
case <- timer.C:
|
case timer:=<- t.C:
|
||||||
timewheel.ReleaseTimer(timer)
|
timer.SetupTimer(time.Now())
|
||||||
timer=timewheel.NewTimer(3*time.Second)
|
|
||||||
client.checkRpcCallTimeout()
|
client.checkRpcCallTimeout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
timer.Close()
|
t.Cancel()
|
||||||
timewheel.ReleaseTimer(timer)
|
timer.ReleaseTimer(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) makeCallFail(call *Call){
|
func (client *Client) makeCallFail(call *Call){
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ func SetupTimer(timer *Timer) *Timer{
|
|||||||
return timer
|
return timer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewTimer(d time.Duration) *Timer{
|
||||||
|
c := make(chan *Timer,1)
|
||||||
|
timer := newTimer(d,c,nil,"",nil)
|
||||||
|
SetupTimer(timer)
|
||||||
|
return timer
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReleaseTimer(timer *Timer) {
|
||||||
|
releaseTimer(timer)
|
||||||
|
}
|
||||||
|
|
||||||
type _TimerHeap struct {
|
type _TimerHeap struct {
|
||||||
timers []*Timer
|
timers []*Timer
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,6 +89,11 @@ func (t *Timer) Do(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Timer) SetupTimer(now time.Time){
|
||||||
|
t.fireTime = now.Add(t.interval)
|
||||||
|
SetupTimer(t)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Timer) GetInterval() time.Duration{
|
func (t *Timer) GetInterval() time.Duration{
|
||||||
return t.interval
|
return t.interval
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user