mirror of
https://github.com/xtaci/kcp-go.git
synced 2024-04-21 12:32:15 +00:00
properly drain timer.C
This commit is contained in:
+9
-1
@@ -62,6 +62,7 @@ func NewTimedSched(parallel int) *TimedSched {
|
||||
func (ts *TimedSched) sched() {
|
||||
var tasks timedFuncHeap
|
||||
timer := time.NewTimer(0)
|
||||
drained := false
|
||||
for {
|
||||
select {
|
||||
case task := <-ts.chTask:
|
||||
@@ -71,15 +72,22 @@ func (ts *TimedSched) sched() {
|
||||
task.execute()
|
||||
} else {
|
||||
heap.Push(&tasks, task)
|
||||
// reset timer to trigger based on the top element
|
||||
// properly reset timer to trigger based on the top element
|
||||
stopped := timer.Stop()
|
||||
if !stopped && !drained {
|
||||
<-timer.C
|
||||
}
|
||||
timer.Reset(tasks[0].ts.Sub(now))
|
||||
drained = false
|
||||
}
|
||||
case now := <-timer.C:
|
||||
drained = true
|
||||
for tasks.Len() > 0 {
|
||||
if now.After(tasks[0].ts) {
|
||||
heap.Pop(&tasks).(timedFunc).execute()
|
||||
} else {
|
||||
timer.Reset(tasks[0].ts.Sub(now))
|
||||
drained = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user