From 9777db1460d1535bc2a843840684fb1205225b87 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 14 Sep 2024 14:16:14 -0500 Subject: [PATCH] Add LimitedWorkerPool (#205) --- src/Worker/ContextWorkerPool.php | 11 +++++++++-- src/Worker/DelegatingWorkerPool.php | 9 +++++++-- src/Worker/LimitedWorkerPool.php | 13 +++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/Worker/LimitedWorkerPool.php diff --git a/src/Worker/ContextWorkerPool.php b/src/Worker/ContextWorkerPool.php index edd1bac..4962d9d 100644 --- a/src/Worker/ContextWorkerPool.php +++ b/src/Worker/ContextWorkerPool.php @@ -20,7 +20,7 @@ * tasks simultaneously. The load on each worker is balanced such that tasks * are completed as soon as possible and workers are used efficiently. */ -final class ContextWorkerPool implements WorkerPool +final class ContextWorkerPool implements LimitedWorkerPool { use ForbidCloning; use ForbidSerialization; @@ -104,14 +104,21 @@ public function isIdle(): bool return $this->idleWorkers->count() > 0 || $this->workers->count() < $this->limit; } + public function getWorkerLimit(): int + { + return $this->limit; + } + /** * Gets the maximum number of workers the pool may spawn to handle concurrent tasks. * * @return int The maximum number of workers. + * + * @deprecated Use {@see getWorkerLimit()} instead. */ public function getLimit(): int { - return $this->limit; + return $this->getWorkerLimit(); } public function getWorkerCount(): int diff --git a/src/Worker/DelegatingWorkerPool.php b/src/Worker/DelegatingWorkerPool.php index 33039ca..129f7de 100644 --- a/src/Worker/DelegatingWorkerPool.php +++ b/src/Worker/DelegatingWorkerPool.php @@ -4,10 +4,15 @@ use Amp\Cancellation; use Amp\DeferredFuture; +use Amp\ForbidCloning; +use Amp\ForbidSerialization; use Amp\Parallel\Worker\Internal\PooledWorker; -final class DelegatingWorkerPool implements WorkerPool +final class DelegatingWorkerPool implements LimitedWorkerPool { + use ForbidCloning; + use ForbidSerialization; + /** @var array */ private array $workerStorage = []; @@ -116,7 +121,7 @@ public function getWorker(): Worker return new PooledWorker($this->selectWorker(), $this->push(...)); } - public function getLimit(): int + public function getWorkerLimit(): int { return $this->limit; } diff --git a/src/Worker/LimitedWorkerPool.php b/src/Worker/LimitedWorkerPool.php new file mode 100644 index 0000000..cb8ed62 --- /dev/null +++ b/src/Worker/LimitedWorkerPool.php @@ -0,0 +1,13 @@ +