Merge branch '3.5'

This commit is contained in:
Dylan K. Taylor 2019-01-03 15:44:48 +00:00
commit 19fa6f5e8f

View File

@ -46,6 +46,8 @@ class AsyncPool{
/** @var AsyncWorker[] */ /** @var AsyncWorker[] */
private $workers = []; private $workers = [];
/** @var int[] */
private $workerLastUsed = [];
/** @var \Closure[] */ /** @var \Closure[] */
private $workerStartHooks = []; private $workerStartHooks = [];
@ -155,6 +157,7 @@ class AsyncPool{
$this->getWorker($worker)->stack($task); $this->getWorker($worker)->stack($task);
$this->taskQueues[$worker]->enqueue($task); $this->taskQueues[$worker]->enqueue($task);
$this->workerLastUsed[$worker] = time();
} }
/** /**
@ -253,10 +256,11 @@ class AsyncPool{
public function shutdownUnusedWorkers() : int{ public function shutdownUnusedWorkers() : int{
$ret = 0; $ret = 0;
$time = time();
foreach($this->taskQueues as $i => $queue){ foreach($this->taskQueues as $i => $queue){
if($queue->isEmpty()){ if((!isset($this->workerLastUsed[$i]) or $this->workerLastUsed[$i] + 300 < $time) and $queue->isEmpty()){
$this->workers[$i]->quit(); $this->workers[$i]->quit();
unset($this->workers[$i], $this->taskQueues[$i]); unset($this->workers[$i], $this->taskQueues[$i], $this->workerLastUsed[$i]);
$ret++; $ret++;
} }
} }
@ -289,5 +293,6 @@ class AsyncPool{
} }
$this->workers = []; $this->workers = [];
$this->taskQueues = []; $this->taskQueues = [];
$this->workerLastUsed = [];
} }
} }