Skip to content

Commit

Permalink
Prioritize context.Done statement in worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Corentin Clabaut committed Jun 10, 2024
1 parent f865e23 commit f59ff90
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ func worker(context context.Context, waitGroup *sync.WaitGroup, firstTask func()
}
return
case task, ok := <-tasks:
if task == nil || !ok {
// We have received a signal to exit
return
}
// Prioritize context.Done statement (https://stackoverflow.com/questions/46200343/force-priority-of-go-select-statement)
select {
case <-context.Done():
if task != nil && ok {
// We have received a task, ignore it
taskWaitGroup.Done()
}
default:
if task == nil || !ok {
// We have received a signal to exit
return
}

// We have received a task, execute it
taskExecutor(task, false)
// We have received a task, execute it
taskExecutor(task, false)
}
}
}
}

0 comments on commit f59ff90

Please sign in to comment.