|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the reliforp/reli-prof package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Reli\Inspector\Daemon\Dispatcher; |
|
15
|
|
|
|
|
16
|
|
|
use Reli\Inspector\Daemon\Reader\Controller\PhpReaderControllerInterface; |
|
17
|
|
|
|
|
18
|
|
|
use function is_null; |
|
19
|
|
|
|
|
20
|
|
|
final class DispatchTable |
|
21
|
|
|
{ |
|
22
|
|
|
private TargetProcessListInterface $assigned; |
|
23
|
|
|
/** @var array<int, PhpReaderControllerInterface> */ |
|
24
|
|
|
private array $dispatch_table = []; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
public WorkerPoolInterface $worker_pool, |
|
28
|
|
|
) { |
|
29
|
|
|
$this->assigned = new TargetProcessList(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function updateTargets(TargetProcessListInterface $update): \Generator |
|
33
|
|
|
{ |
|
34
|
|
|
$diff = $this->assigned->getDiff($update); |
|
35
|
|
|
$this->release($diff); |
|
36
|
|
|
$unassigned_new = $update->getDiff($this->assigned); |
|
37
|
|
|
for ($worker = $this->worker_pool->getFreeWorker(); $worker; $worker = $this->worker_pool->getFreeWorker()) { |
|
38
|
|
|
$picked = $unassigned_new->pickOne(); |
|
39
|
|
|
if (is_null($picked)) { |
|
40
|
|
|
$this->worker_pool->returnWorkerToPool($worker); |
|
41
|
|
|
break; |
|
42
|
|
|
} |
|
43
|
|
|
$this->assigned->putOne($picked); |
|
44
|
|
|
$this->dispatch_table[$picked->pid] = $worker; |
|
45
|
|
|
yield $worker->sendAttach($picked); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function release(TargetProcessListInterface $targets): void |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($targets->getArray() as $pid) { |
|
52
|
|
|
$this->releaseOne($pid->pid); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function releaseOne(int $pid): void |
|
57
|
|
|
{ |
|
58
|
|
|
if (isset($this->dispatch_table[$pid])) { |
|
59
|
|
|
$worker = $this->dispatch_table[$pid]; |
|
60
|
|
|
$this->worker_pool->returnWorkerToPool($worker); |
|
61
|
|
|
unset($this->dispatch_table[$pid]); |
|
62
|
|
|
} |
|
63
|
|
|
$this->assigned->removeByPid($pid); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|