Skip to content

Commit

Permalink
fix(workerpool.go): add nil check in AddTask method to prevent adding…
Browse files Browse the repository at this point in the history
… nil task to reduce the worker number.
  • Loading branch information
luogz17 committed Dec 18, 2023
1 parent 99454e5 commit 0dbc007
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions common/utils/workerpool/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ func (vwp *WorkerPool) Run() {
for i := 0; i < vwp.maxWorker; i++ {
go func() {
for task := range vwp.taskQueueChan {
if task != nil {
task()
vwp.wg.Done()
} else {
return
}
task()
vwp.wg.Done()
}
}()
}
Expand All @@ -46,6 +42,9 @@ func (vwp *WorkerPool) Stop() {

// AddTask adds a task to WorkerPool
func (vwp *WorkerPool) AddTask(task func()) {
if task == nil {
return
}
vwp.wg.Add(1)
vwp.taskQueueChan <- task
}

0 comments on commit 0dbc007

Please sign in to comment.