Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LimitedWorkerPool #205

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
kelunik marked this conversation as resolved.
Show resolved Hide resolved
{
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;
}
Loading