|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Amp\Parallel\Worker; |
| 4 | + |
| 5 | +use Amp\Cancellation; |
| 6 | +use Amp\DeferredFuture; |
| 7 | +use Amp\Parallel\Worker\Internal\PooledWorker; |
| 8 | + |
| 9 | +final class DelegatingWorkerPool implements WorkerPool |
| 10 | +{ |
| 11 | + /** @var array<int, Worker> */ |
| 12 | + private array $workerStorage = []; |
| 13 | + |
| 14 | + private int $pendingWorkerCount = 0; |
| 15 | + |
| 16 | + /** @var \SplQueue<DeferredFuture<Worker|null>> */ |
| 17 | + private readonly \SplQueue $waiting; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param int $limit Maximum number of workers to use from the delegate pool. |
| 21 | + */ |
| 22 | + public function __construct(private readonly WorkerPool $pool, private readonly int $limit) |
| 23 | + { |
| 24 | + $this->waiting = new \SplQueue(); |
| 25 | + } |
| 26 | + |
| 27 | + public function isRunning(): bool |
| 28 | + { |
| 29 | + return $this->pool->isRunning(); |
| 30 | + } |
| 31 | + |
| 32 | + public function isIdle(): bool |
| 33 | + { |
| 34 | + return $this->pool->isIdle(); |
| 35 | + } |
| 36 | + |
| 37 | + public function submit(Task $task, ?Cancellation $cancellation = null): Execution |
| 38 | + { |
| 39 | + $worker = $this->selectWorker(); |
| 40 | + |
| 41 | + $execution = $worker->submit($task, $cancellation); |
| 42 | + |
| 43 | + $execution->getFuture()->finally(fn () => $this->push($worker))->ignore(); |
| 44 | + |
| 45 | + return $execution; |
| 46 | + } |
| 47 | + |
| 48 | + private function selectWorker(): Worker |
| 49 | + { |
| 50 | + do { |
| 51 | + if (\count($this->workerStorage) + $this->pendingWorkerCount < $this->limit) { |
| 52 | + $this->pendingWorkerCount++; |
| 53 | + |
| 54 | + try { |
| 55 | + $worker = $this->pool->getWorker(); |
| 56 | + } finally { |
| 57 | + $this->pendingWorkerCount--; |
| 58 | + } |
| 59 | + } else { |
| 60 | + /** @var DeferredFuture<Worker|null> $waiting */ |
| 61 | + $waiting = new DeferredFuture(); |
| 62 | + $this->waiting->push($waiting); |
| 63 | + |
| 64 | + $worker = $waiting->getFuture()->await(); |
| 65 | + if (!$worker?->isRunning()) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $this->workerStorage[\spl_object_id($worker)] = $worker; |
| 71 | + |
| 72 | + return $worker; |
| 73 | + } while (true); |
| 74 | + } |
| 75 | + |
| 76 | + private function push(Worker $worker): void |
| 77 | + { |
| 78 | + unset($this->workerStorage[\spl_object_id($worker)]); |
| 79 | + |
| 80 | + if (!$this->waiting->isEmpty()) { |
| 81 | + $deferredFuture = $this->waiting->dequeue(); |
| 82 | + $deferredFuture->complete($worker->isRunning() ? $worker : null); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public function shutdown(): void |
| 87 | + { |
| 88 | + if (!$this->waiting->isEmpty()) { |
| 89 | + $exception = new WorkerException('The pool was shutdown before a worker could be obtained'); |
| 90 | + $this->clearWaiting($exception); |
| 91 | + } |
| 92 | + |
| 93 | + $this->pool->shutdown(); |
| 94 | + } |
| 95 | + |
| 96 | + public function kill(): void |
| 97 | + { |
| 98 | + if (!$this->waiting->isEmpty()) { |
| 99 | + $exception = new WorkerException('The pool was killed before a worker could be obtained'); |
| 100 | + $this->clearWaiting($exception); |
| 101 | + } |
| 102 | + |
| 103 | + $this->pool->kill(); |
| 104 | + } |
| 105 | + |
| 106 | + private function clearWaiting(\Throwable $exception): void |
| 107 | + { |
| 108 | + while (!$this->waiting->isEmpty()) { |
| 109 | + $deferredFuture = $this->waiting->dequeue(); |
| 110 | + $deferredFuture->error($exception); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public function getWorker(): Worker |
| 115 | + { |
| 116 | + return new PooledWorker($this->selectWorker(), $this->push(...)); |
| 117 | + } |
| 118 | + |
| 119 | + public function getWorkerCount(): int |
| 120 | + { |
| 121 | + return \min($this->limit, $this->pool->getWorkerCount()); |
| 122 | + } |
| 123 | + |
| 124 | + public function getIdleWorkerCount(): int |
| 125 | + { |
| 126 | + return \min($this->limit, $this->pool->getIdleWorkerCount()); |
| 127 | + } |
| 128 | +} |
0 commit comments