Skip to content

Commit 15cdc86

Browse files
committed
refactor(worker): parallelize pop for filling the pool
1 parent 4c91774 commit 15cdc86

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/worker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,16 @@ export class Worker {
139139

140140
async *#fillPool(queues: string[]): AsyncGenerator<WorkerCycle, void, unknown> {
141141
const concurrency = this.#config.worker?.concurrency || 1
142+
const slotsAvailable = concurrency - this.#pool!.size
142143

143-
while (this.#pool!.hasCapacity(concurrency)) {
144-
const result = await this.#acquireNextJob(queues)
145-
if (!result) break
144+
if (slotsAvailable <= 0) return
145+
146+
const popPromises = Array.from({ length: slotsAvailable }, () => this.#acquireNextJob(queues))
147+
148+
const results = await Promise.all(popPromises)
149+
150+
for (const result of results) {
151+
if (!result) continue
146152

147153
const { job, queue } = result
148154
const promise = this.#execute(job, queue)

0 commit comments

Comments
 (0)