Skip to content

Commit

Permalink
Add LimitedWorkerPool (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski authored Sep 14, 2024
1 parent feed27e commit 9777db1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/Worker/ContextWorkerPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Worker/DelegatingWorkerPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Worker> */
private array $workerStorage = [];

Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Worker/LimitedWorkerPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Amp\Parallel\Worker;

interface LimitedWorkerPool extends WorkerPool
{
/**
* Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
*
* @return int The maximum number of workers.
*/
public function getWorkerLimit(): int;
}

0 comments on commit 9777db1

Please sign in to comment.