Skip to content

Commit c38099e

Browse files
authored
perf: unify startWorker and worker to spawn less goroutines (#59)
* perf: unify startWorker and worker to spawn less goroutines * refactor: simplify worker logic
1 parent 85cc841 commit c38099e

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

workerpool.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Loop:
194194
// Create a new worker, if not at max.
195195
if workerCount < p.maxWorkers {
196196
wg.Add(1)
197-
go startWorker(task, p.workerQueue, &wg)
197+
go worker(task, p.workerQueue, &wg)
198198
workerCount++
199199
} else {
200200
// Enqueue task to be executed by next available worker.
@@ -231,21 +231,13 @@ Loop:
231231
timeout.Stop()
232232
}
233233

234-
// startWorker runs initial task, then starts a worker waiting for more.
235-
func startWorker(task func(), workerQueue chan func(), wg *sync.WaitGroup) {
236-
task()
237-
go worker(workerQueue, wg)
238-
}
239-
240234
// worker executes tasks and stops when it receives a nil task.
241-
func worker(workerQueue chan func(), wg *sync.WaitGroup) {
242-
for task := range workerQueue {
243-
if task == nil {
244-
wg.Done()
245-
return
246-
}
235+
func worker(task func(), workerQueue chan func(), wg *sync.WaitGroup) {
236+
for task != nil {
247237
task()
238+
task = <-workerQueue
248239
}
240+
wg.Done()
249241
}
250242

251243
// stop tells the dispatcher to exit, and whether or not to complete queued

0 commit comments

Comments
 (0)