Skip to content

Commit 05ece13

Browse files
committed
Add LimitedWorkerPool
1 parent feed27e commit 05ece13

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Worker/ContextWorkerPool.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* tasks simultaneously. The load on each worker is balanced such that tasks
2121
* are completed as soon as possible and workers are used efficiently.
2222
*/
23-
final class ContextWorkerPool implements WorkerPool
23+
final class ContextWorkerPool implements LimitedWorkerPool
2424
{
2525
use ForbidCloning;
2626
use ForbidSerialization;
@@ -104,14 +104,21 @@ public function isIdle(): bool
104104
return $this->idleWorkers->count() > 0 || $this->workers->count() < $this->limit;
105105
}
106106

107+
public function getWorkerLimit(): int
108+
{
109+
return $this->limit;
110+
}
111+
107112
/**
108113
* Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
109114
*
110115
* @return int The maximum number of workers.
116+
*
117+
* @deprecated Use {@see getWorkerLimit()} instead.
111118
*/
112119
public function getLimit(): int
113120
{
114-
return $this->limit;
121+
return $this->getWorkerLimit();
115122
}
116123

117124
public function getWorkerCount(): int

src/Worker/DelegatingWorkerPool.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
use Amp\Cancellation;
66
use Amp\DeferredFuture;
7+
use Amp\ForbidCloning;
8+
use Amp\ForbidSerialization;
79
use Amp\Parallel\Worker\Internal\PooledWorker;
810

9-
final class DelegatingWorkerPool implements WorkerPool
11+
final class DelegatingWorkerPool implements LimitedWorkerPool
1012
{
13+
use ForbidCloning;
14+
use ForbidSerialization;
15+
1116
/** @var array<int, Worker> */
1217
private array $workerStorage = [];
1318

@@ -116,7 +121,7 @@ public function getWorker(): Worker
116121
return new PooledWorker($this->selectWorker(), $this->push(...));
117122
}
118123

119-
public function getLimit(): int
124+
public function getWorkerLimit(): int
120125
{
121126
return $this->limit;
122127
}

src/Worker/LimitedWorkerPool.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Parallel\Worker;
4+
5+
interface LimitedWorkerPool extends WorkerPool
6+
{
7+
/**
8+
* Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
9+
*
10+
* @return int The maximum number of workers.
11+
*/
12+
public function getWorkerLimit(): int;
13+
}

0 commit comments

Comments
 (0)