|
7 | 7 | use Amp\File\Internal; |
8 | 8 | use Amp\Future; |
9 | 9 | use Amp\Parallel\Worker\ContextWorkerPool; |
10 | | -use Amp\Parallel\Worker\DelegatingWorkerPool; |
11 | 10 | use Amp\Parallel\Worker\LimitedWorkerPool; |
12 | 11 | use Amp\Parallel\Worker\TaskFailureThrowable; |
13 | 12 | use Amp\Parallel\Worker\Worker; |
14 | 13 | use Amp\Parallel\Worker\WorkerException; |
15 | | -use Amp\Parallel\Worker\WorkerPool; |
16 | 14 | use function Amp\async; |
17 | 15 |
|
18 | 16 | final class ParallelFilesystemDriver implements FilesystemDriver |
19 | 17 | { |
20 | 18 | public const DEFAULT_WORKER_LIMIT = 8; |
21 | 19 |
|
22 | | - private readonly WorkerPool $pool; |
23 | | - |
24 | | - /** @var positive-int Maximum number of workers to use for open files. */ |
25 | | - private readonly int $workerLimit; |
26 | | - |
27 | 20 | /** @var \WeakMap<Worker, int> */ |
28 | 21 | private \WeakMap $workerStorage; |
29 | 22 |
|
30 | 23 | /** @var Future<Worker>|null Pending worker request */ |
31 | 24 | private ?Future $pendingWorker = null; |
32 | 25 |
|
33 | 26 | /** |
34 | | - * @param WorkerPool|null $pool Custom worker pool to use for file workers. If null, a new pool is created. |
35 | | - * @param int|null $workerLimit [Deprecated] Maximum number of workers to use from the pool for open files. Instead |
36 | | - * of using this parameter, provide a pool with a limited number using an instance of {@see LimitedWorkerPool} |
37 | | - * such as {@see ContextWorkerPool}. |
| 27 | + * @param LimitedWorkerPool $pool Custom worker pool to use for file workers. If one is not provided, a new |
| 28 | + * pool is created. |
38 | 29 | */ |
39 | | - public function __construct(?WorkerPool $pool = null, ?int $workerLimit = null) |
40 | | - { |
| 30 | + public function __construct( |
| 31 | + private readonly LimitedWorkerPool $pool = new ContextWorkerPool(self::DEFAULT_WORKER_LIMIT), |
| 32 | + ) { |
41 | 33 | /** @var \WeakMap<Worker, int> For Psalm. */ |
42 | 34 | $this->workerStorage = new \WeakMap(); |
43 | | - |
44 | | - if ($workerLimit !== null) { |
45 | | - \trigger_error( |
46 | | - 'The $workerLimit parameter is deprecated and will be removed in the next major version.' . |
47 | | - ' To limit the number of workers used from the given pool, use an instance of ' . |
48 | | - LimitedWorkerPool::class . ' instead, such as ' . ContextWorkerPool::class . ' or ' . |
49 | | - DelegatingWorkerPool::class, |
50 | | - \E_USER_DEPRECATED, |
51 | | - ); |
52 | | - } |
53 | | - |
54 | | - $workerLimit ??= $pool instanceof LimitedWorkerPool ? $pool->getWorkerLimit() : self::DEFAULT_WORKER_LIMIT; |
55 | | - if ($workerLimit <= 0) { |
56 | | - throw new \ValueError("Worker limit must be a positive integer"); |
57 | | - } |
58 | | - |
59 | | - $this->pool = $pool ?? new ContextWorkerPool($workerLimit); |
60 | | - $this->workerLimit = $workerLimit; |
61 | 35 | } |
62 | 36 |
|
63 | 37 | public function openFile(string $path, string $mode): ParallelFile |
@@ -90,7 +64,7 @@ private function selectWorker(): Worker |
90 | 64 | { |
91 | 65 | $this->pendingWorker?->await(); // Wait for any currently pending request for a worker. |
92 | 66 |
|
93 | | - if ($this->workerStorage->count() < $this->workerLimit) { |
| 67 | + if ($this->workerStorage->count() < $this->pool->getWorkerLimit()) { |
94 | 68 | $this->pendingWorker = async($this->pool->getWorker(...)); |
95 | 69 | $worker = $this->pendingWorker->await(); |
96 | 70 |
|
|
0 commit comments