Skip to content

Commit

Permalink
Add interval between starting each worker
Browse files Browse the repository at this point in the history
Experiment for #112.
  • Loading branch information
trowski committed Apr 20, 2020
1 parent ebd15bf commit 5345eb5
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/Worker/TaskWorker.php
Expand Up @@ -2,6 +2,7 @@

namespace Amp\Parallel\Worker;

use Amp\Delayed;
use Amp\Failure;
use Amp\Parallel\Context\Context;
use Amp\Parallel\Context\StatusError;
Expand All @@ -19,6 +20,9 @@ abstract class TaskWorker implements Worker
const SHUTDOWN_TIMEOUT = 1000;
const ERROR_TIMEOUT = 250;

/** @var Delayed */
private static $throttle;

/** @var Context */
private $context;

Expand All @@ -36,6 +40,10 @@ abstract class TaskWorker implements Worker
*/
public function __construct(Context $context)
{
if (self::$throttle === null) {
self::$throttle = new Delayed(0);
}

if ($context->isRunning()) {
throw new \Error("The context was already running");
}
Expand Down Expand Up @@ -110,6 +118,10 @@ public function enqueue(Task $task): Promise
throw new WorkerException("The worker crashed");
}

$delayed = self::$throttle;
self::$throttle = new Delayed(100);
yield $delayed;

$this->started = true;
yield $this->context->start();
}
Expand Down

0 comments on commit 5345eb5

Please sign in to comment.