Skip to content

Commit fcce7dc

Browse files
feat: Overfill worker pool before taking out pooled worker
This allows for a poolSize of 0, meaning there is no idling worker.
1 parent d6ed7b3 commit fcce7dc

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

Classes/Worker.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646

4747
public function prepare(): void
4848
{
49-
$this->cleanPool();
49+
$this->fillPool($this->poolSize);
5050

5151
$this->output = new ConsoleOutput();
5252
$this->output->outputLine('Watching queue <b>"%s"</b>', [$this->queue->getName()]);
@@ -120,7 +120,7 @@ private function createProcess(): Process
120120

121121
private function runFromPool(string $messageCacheIdentifier): Process
122122
{
123-
$this->cleanPool();
123+
$this->fillPool($this->poolSize + 1); // Overfill
124124
$process = array_shift($this->pool);
125125
assert($process instanceof Process);
126126

@@ -134,13 +134,14 @@ private function runFromPool(string $messageCacheIdentifier): Process
134134
return $process;
135135
}
136136

137-
private function cleanPool(): void
137+
private function fillPool(int $poolSize): void
138138
{
139+
$poolSize = max($poolSize, 0);
139140
$this->pool = array_filter(
140141
$this->pool,
141142
fn (Process $process) => $process->isRunning()
142143
);
143-
while (count($this->pool) < $this->poolSize) {
144+
while (count($this->pool) < $poolSize) {
144145
$this->pool[] = $this->createProcess();
145146
}
146147
}

0 commit comments

Comments
 (0)