set guarded to false only on quitting background flush (#342)

* set guarded to false only on quitting background flush

* set guarded to false only on quitting background flush, cont.
master
Kevin Wan 4 years ago committed by GitHub
parent 510e966982
commit 5bc01e4bfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -111,6 +111,9 @@ func (pe *PeriodicalExecutor) addAndCheck(task interface{}) (interface{}, bool)
func (pe *PeriodicalExecutor) backgroundFlush() { func (pe *PeriodicalExecutor) backgroundFlush() {
threading.GoSafe(func() { threading.GoSafe(func() {
// flush before quit goroutine to avoid missing tasks
defer pe.Flush()
ticker := pe.newTicker(pe.interval) ticker := pe.newTicker(pe.interval)
defer ticker.Stop() defer ticker.Stop()
@ -130,30 +133,17 @@ func (pe *PeriodicalExecutor) backgroundFlush() {
commanded = false commanded = false
} else if pe.Flush() { } else if pe.Flush() {
last = timex.Now() last = timex.Now()
} else if timex.Since(last) > pe.interval*idleRound { } else if pe.shallQuit(last) {
if pe.cleanup() { pe.lock.Lock()
pe.guarded = false
pe.lock.Unlock()
return return
} }
} }
} }
}
}) })
} }
func (pe *PeriodicalExecutor) cleanup() (stop bool) {
pe.lock.Lock()
pe.guarded = false
if atomic.LoadInt32(&pe.inflight) == 0 {
stop = true
// defer to unlock quickly
// flush again to avoid missing tasks
defer pe.Flush()
}
pe.lock.Unlock()
return
}
func (pe *PeriodicalExecutor) doneExecution() { func (pe *PeriodicalExecutor) doneExecution() {
pe.waitGroup.Done() pe.waitGroup.Done()
} }
@ -189,3 +179,9 @@ func (pe *PeriodicalExecutor) hasTasks(tasks interface{}) bool {
return true return true
} }
} }
func (pe *PeriodicalExecutor) shallQuit(last time.Duration) bool {
idleEnough := timex.Since(last) > pe.interval*idleRound
noPending := atomic.LoadInt32(&pe.inflight) == 0
return idleEnough && noPending
}

Loading…
Cancel
Save