1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ReactParallel\Pool\Limited; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise\Promise; |
8
|
|
|
use React\Promise\PromiseInterface; |
9
|
|
|
use ReactParallel\Contracts\ClosedException; |
10
|
|
|
use ReactParallel\Contracts\GroupInterface; |
11
|
|
|
use ReactParallel\Contracts\LowLevelPoolInterface; |
12
|
|
|
use ReactParallel\Contracts\PoolInterface; |
13
|
|
|
use ReactParallel\Pool\Infinite\Infinite; |
14
|
|
|
use SplQueue; |
15
|
|
|
use WyriHaximus\PoolInfo\Info; |
16
|
|
|
use function count; |
17
|
|
|
use function React\Promise\reject; |
18
|
|
|
|
19
|
|
|
final class Limited implements PoolInterface |
20
|
|
|
{ |
21
|
|
|
private PoolInterface $pool; |
|
|
|
|
22
|
|
|
|
23
|
|
|
private int $threadCount; |
24
|
|
|
|
25
|
|
|
private int $idleRuntimes; |
26
|
|
|
|
27
|
|
|
private SplQueue $queue; |
28
|
|
|
|
29
|
|
|
private ?GroupInterface $group = null; |
30
|
|
|
|
31
|
|
|
private bool $closed = false; |
32
|
|
|
|
33
|
|
|
public static function create(LoopInterface $loop, int $threadCount): self |
34
|
|
|
{ |
35
|
|
|
return new self(new Infinite($loop, 1), $threadCount); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function createWithPool(PoolInterface $pool, int $threadCount): self |
39
|
|
|
{ |
40
|
|
|
return new self($pool, $threadCount); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function __construct(PoolInterface $pool, int $threadCount) |
44
|
|
|
{ |
45
|
|
|
$this->pool = $pool; |
46
|
|
|
$this->threadCount = $threadCount; |
47
|
|
|
$this->idleRuntimes = $threadCount; |
48
|
|
|
$this->queue = new SplQueue(); |
49
|
|
|
|
50
|
|
|
if (! ($this->pool instanceof LowLevelPoolInterface)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->group = $this->pool->acquireGroup(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed[] $args |
59
|
|
|
*/ |
60
|
|
|
public function run(Closure $callable, array $args = []): PromiseInterface |
61
|
|
|
{ |
62
|
|
|
if ($this->closed === true) { |
63
|
|
|
return reject(ClosedException::create()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return (new Promise(function (callable $resolve): void { |
67
|
|
|
if ($this->idleRuntimes === 0) { |
68
|
|
|
$this->queue->enqueue($resolve); |
69
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$resolve(); |
74
|
|
|
}))->then(function () use ($callable, $args): PromiseInterface { |
75
|
|
|
$this->idleRuntimes--; |
76
|
|
|
|
77
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */ |
78
|
|
|
return $this->pool->run($callable, $args)->always(function (): void { |
79
|
|
|
$this->idleRuntimes++; |
80
|
|
|
$this->progressQueue(); |
81
|
|
|
}); |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function close(): bool |
86
|
|
|
{ |
87
|
|
|
$this->closed = true; |
88
|
|
|
|
89
|
|
|
if ($this->pool instanceof LowLevelPoolInterface && $this->group instanceof GroupInterface) { |
90
|
|
|
$this->pool->releaseGroup($this->group); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->pool->close(); |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function kill(): bool |
99
|
|
|
{ |
100
|
|
|
$this->closed = true; |
101
|
|
|
|
102
|
|
|
if ($this->pool instanceof LowLevelPoolInterface && $this->group instanceof GroupInterface) { |
103
|
|
|
$this->pool->releaseGroup($this->group); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->pool->kill(); |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return iterable<string, int> |
113
|
|
|
*/ |
114
|
|
|
public function info(): iterable |
115
|
|
|
{ |
116
|
|
|
yield Info::TOTAL => $this->threadCount; |
117
|
|
|
yield Info::BUSY => $this->threadCount - $this->idleRuntimes; |
118
|
|
|
yield Info::CALLS => $this->queue->count(); |
119
|
|
|
yield Info::IDLE => $this->idleRuntimes; |
120
|
|
|
yield Info::SIZE => $this->threadCount; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function progressQueue(): void |
124
|
|
|
{ |
125
|
|
|
if (count($this->queue) === 0) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
($this->queue->dequeue())(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|