Skip to content

Commit

Permalink
simplify periodical executor background routine (zeromicro#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jan 3, 2021
1 parent 10e3b8a commit 510e966
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions core/executors/periodicalexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,12 @@ func (pe *PeriodicalExecutor) Wait() {
func (pe *PeriodicalExecutor) addAndCheck(task interface{}) (interface{}, bool) {
pe.lock.Lock()
defer func() {
var start bool
if !pe.guarded {
pe.guarded = true
start = true
// defer to unlock quickly
defer pe.backgroundFlush()
}
pe.lock.Unlock()
if start {
pe.backgroundFlush()
}
}()

if pe.container.AddTask(task) {
Expand Down Expand Up @@ -148,13 +145,11 @@ func (pe *PeriodicalExecutor) cleanup() (stop bool) {
pe.guarded = false
if atomic.LoadInt32(&pe.inflight) == 0 {
stop = true
}
pe.lock.Unlock()

if stop {
// defer to unlock quickly
// flush again to avoid missing tasks
pe.Flush()
defer pe.Flush()
}
pe.lock.Unlock()

return
}
Expand Down

0 comments on commit 510e966

Please sign in to comment.