2015-12-05 06:50:32 +01:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Icicle\Awaitable;
|
2015-12-16 18:16:21 +01:00
|
|
|
use Icicle\Exception\InvalidArgumentError;
|
2015-12-05 06:50:32 +01:00
|
|
|
use Icicle\Concurrent\Exception\StatusError;
|
|
|
|
use Icicle\Coroutine\Coroutine;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a pool of workers that can be used to execute multiple tasks asynchronously.
|
|
|
|
*
|
|
|
|
* A worker pool is a collection of worker threads that can perform multiple
|
|
|
|
* tasks simultaneously. The load on each worker is balanced such that tasks
|
|
|
|
* are completed as soon as possible and workers are used efficiently.
|
|
|
|
*/
|
|
|
|
class DefaultPool implements Pool
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var bool Indicates if the pool is currently running.
|
|
|
|
*/
|
|
|
|
private $running = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int The minimum number of workers the pool should spawn.
|
|
|
|
*/
|
|
|
|
private $minSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int The maximum number of workers the pool should spawn.
|
|
|
|
*/
|
|
|
|
private $maxSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var WorkerFactory A worker factory to be used to create new workers.
|
|
|
|
*/
|
|
|
|
private $factory;
|
|
|
|
|
|
|
|
/**
|
2015-12-12 05:47:46 +01:00
|
|
|
* @var \SplObjectStorage A collection of all workers in the pool.
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
2015-12-12 05:47:46 +01:00
|
|
|
private $workers;
|
2015-12-05 06:50:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \SplQueue A collection of idle workers.
|
|
|
|
*/
|
|
|
|
private $idleWorkers;
|
|
|
|
|
|
|
|
/**
|
2016-01-15 05:13:38 +01:00
|
|
|
* @var \SplQueue A queue of workers that have been assigned to tasks.
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
|
|
|
private $busyQueue;
|
|
|
|
|
2016-01-15 01:54:53 +01:00
|
|
|
/**
|
|
|
|
* @var \Closure
|
|
|
|
*/
|
|
|
|
private $push;
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
/**
|
|
|
|
* Creates a new worker pool.
|
|
|
|
*
|
2015-12-16 23:39:25 +01:00
|
|
|
* @param int|null $minSize The minimum number of workers the pool should spawn.
|
|
|
|
* Defaults to `Pool::DEFAULT_MIN_SIZE`.
|
|
|
|
* @param int|null $maxSize The maximum number of workers the pool should spawn.
|
|
|
|
* Defaults to `Pool::DEFAULT_MAX_SIZE`.
|
2015-12-05 06:50:32 +01:00
|
|
|
* @param \Icicle\Concurrent\Worker\WorkerFactory|null $factory A worker factory to be used to create
|
|
|
|
* new workers.
|
|
|
|
*
|
2015-12-16 18:16:21 +01:00
|
|
|
* @throws \Icicle\Exception\InvalidArgumentError
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function __construct(int $minSize = null, int $maxSize = null, WorkerFactory $factory = null)
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
$minSize = $minSize ?: self::DEFAULT_MIN_SIZE;
|
|
|
|
$maxSize = $maxSize ?: self::DEFAULT_MAX_SIZE;
|
|
|
|
|
|
|
|
if (!is_int($minSize) || $minSize < 0) {
|
|
|
|
throw new InvalidArgumentError('Minimum size must be a non-negative integer.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_int($maxSize) || $maxSize < 0 || $maxSize < $minSize) {
|
|
|
|
throw new InvalidArgumentError('Maximum size must be a non-negative integer at least '.$minSize.'.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->maxSize = $maxSize;
|
|
|
|
$this->minSize = $minSize;
|
|
|
|
|
2016-01-11 16:32:06 +01:00
|
|
|
// Use the global factory if none is given.
|
|
|
|
$this->factory = $factory ?: factory();
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2015-12-12 05:47:46 +01:00
|
|
|
$this->workers = new \SplObjectStorage();
|
2015-12-05 06:50:32 +01:00
|
|
|
$this->idleWorkers = new \SplQueue();
|
|
|
|
$this->busyQueue = new \SplQueue();
|
2016-01-15 01:54:53 +01:00
|
|
|
|
|
|
|
$this->push = function (Worker $worker) {
|
|
|
|
$this->push($worker);
|
|
|
|
};
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the pool is running.
|
|
|
|
*
|
|
|
|
* @return bool True if the pool is running, otherwise false.
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function isRunning(): bool
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
return $this->running;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the pool has any idle workers.
|
|
|
|
*
|
|
|
|
* @return bool True if the pool has at least one idle worker, otherwise false.
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function isIdle(): bool
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
return $this->idleWorkers->count() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-16 22:53:53 +01:00
|
|
|
* {@inheritdoc}
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function getMinSize(): int
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
return $this->minSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-16 22:53:53 +01:00
|
|
|
* {@inheritdoc}
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function getMaxSize(): int
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
return $this->maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function getWorkerCount(): int
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
2015-12-16 22:53:53 +01:00
|
|
|
return $this->workers->count();
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function getIdleWorkerCount(): int
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
return $this->idleWorkers->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the worker pool execution.
|
|
|
|
*
|
|
|
|
* When the worker pool starts up, the minimum number of workers will be created. This adds some overhead to
|
|
|
|
* starting the pool, but allows for greater performance during runtime.
|
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
if ($this->isRunning()) {
|
|
|
|
throw new StatusError('The worker pool has already been started.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start up the pool with the minimum number of workers.
|
|
|
|
$count = $this->minSize;
|
|
|
|
while (--$count >= 0) {
|
|
|
|
$worker = $this->createWorker();
|
2015-12-12 05:47:46 +01:00
|
|
|
$this->idleWorkers->enqueue($worker);
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->running = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues a task to be executed by the worker pool.
|
|
|
|
*
|
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* @param Task $task The task to enqueue.
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @resolve mixed The return value of the task.
|
|
|
|
*
|
|
|
|
* @throws \Icicle\Concurrent\Exception\StatusError If the pool has not been started.
|
|
|
|
* @throws \Icicle\Concurrent\Exception\TaskException If the task throws an exception.
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function enqueue(Task $task): \Generator
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
2016-01-15 01:54:53 +01:00
|
|
|
$worker = $this->get();
|
2016-01-23 07:00:56 +01:00
|
|
|
return yield from $worker->enqueue($task);
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuts down the pool and all workers in it.
|
|
|
|
*
|
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @throws \Icicle\Concurrent\Exception\StatusError If the pool has not been started.
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function shutdown(): \Generator
|
2015-12-05 06:50:32 +01:00
|
|
|
{
|
|
|
|
if (!$this->isRunning()) {
|
|
|
|
throw new StatusError('The pool is not running.');
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:13:38 +01:00
|
|
|
$this->running = false;
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2015-12-12 05:47:46 +01:00
|
|
|
$shutdowns = [];
|
|
|
|
|
|
|
|
foreach ($this->workers as $worker) {
|
2015-12-16 22:53:53 +01:00
|
|
|
if ($worker->isRunning()) {
|
|
|
|
$shutdowns[] = new Coroutine($worker->shutdown());
|
|
|
|
}
|
2015-12-12 05:47:46 +01:00
|
|
|
}
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
return yield Awaitable\reduce($shutdowns, function ($carry, $value) {
|
2015-12-05 06:50:32 +01:00
|
|
|
return $carry ?: $value;
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kills all workers in the pool and halts the worker pool.
|
|
|
|
*/
|
|
|
|
public function kill()
|
|
|
|
{
|
2016-01-15 05:13:38 +01:00
|
|
|
$this->running = false;
|
2015-12-05 06:50:32 +01:00
|
|
|
|
|
|
|
foreach ($this->workers as $worker) {
|
|
|
|
$worker->kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a worker and adds them to the pool.
|
|
|
|
*
|
|
|
|
* @return Worker The worker created.
|
|
|
|
*/
|
|
|
|
private function createWorker()
|
|
|
|
{
|
|
|
|
$worker = $this->factory->create();
|
|
|
|
$worker->start();
|
|
|
|
|
2016-01-15 01:54:53 +01:00
|
|
|
$this->workers->attach($worker, 0);
|
2015-12-05 06:50:32 +01:00
|
|
|
return $worker;
|
|
|
|
}
|
2016-01-15 01:54:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function get(): Worker
|
2016-01-15 01:54:53 +01:00
|
|
|
{
|
|
|
|
if (!$this->isRunning()) {
|
|
|
|
throw new StatusError('The queue is not running.');
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if ($this->idleWorkers->isEmpty()) {
|
|
|
|
if ($this->getWorkerCount() >= $this->maxSize) {
|
|
|
|
// All possible workers busy, so shift from head (will be pushed back onto tail below).
|
|
|
|
$worker = $this->busyQueue->shift();
|
|
|
|
} else {
|
|
|
|
// Max worker count has not been reached, so create another worker.
|
|
|
|
$worker = $this->createWorker();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Shift a worker off the idle queue.
|
|
|
|
$worker = $this->idleWorkers->shift();
|
|
|
|
}
|
2016-01-15 06:37:02 +01:00
|
|
|
|
|
|
|
if ($worker->isRunning()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->workers->detach($worker);
|
|
|
|
} while (true);
|
2016-01-15 01:54:53 +01:00
|
|
|
|
|
|
|
$this->busyQueue->push($worker);
|
|
|
|
$this->workers[$worker] += 1;
|
|
|
|
|
|
|
|
return new Internal\PooledWorker($worker, $this->push);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pushes the worker back into the queue.
|
|
|
|
*
|
|
|
|
* @param \Icicle\Concurrent\Worker\Worker $worker
|
|
|
|
*
|
|
|
|
* @throws \Icicle\Exception\InvalidArgumentError If the worker was not part of this queue.
|
|
|
|
*/
|
|
|
|
private function push(Worker $worker)
|
|
|
|
{
|
|
|
|
if (!$this->workers->contains($worker)) {
|
|
|
|
throw new InvalidArgumentError(
|
|
|
|
'The provided worker was not part of this queue.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 === ($this->workers[$worker] -= 1)) {
|
|
|
|
// Worker is completely idle, remove from busy queue and add to idle queue.
|
|
|
|
foreach ($this->busyQueue as $key => $busy) {
|
|
|
|
if ($busy === $worker) {
|
|
|
|
unset($this->busyQueue[$key]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->idleWorkers->push($worker);
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|