reactphp-parallel /
infinite-pool
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ReactParallel\Pool\Infinite; |
||
| 6 | |||
| 7 | use Closure; |
||
| 8 | use React\EventLoop\LoopInterface; |
||
| 9 | use React\EventLoop\TimerInterface; |
||
| 10 | use React\Promise\Promise; |
||
| 11 | use React\Promise\PromiseInterface; |
||
| 12 | use ReactParallel\Contracts\ClosedException; |
||
| 13 | use ReactParallel\Contracts\GroupInterface; |
||
| 14 | use ReactParallel\Contracts\LowLevelPoolInterface; |
||
| 15 | use ReactParallel\EventLoop\EventLoopBridge; |
||
| 16 | use ReactParallel\Runtime\Runtime; |
||
| 17 | use WyriHaximus\PoolInfo\Info; |
||
| 18 | |||
| 19 | use function array_key_exists; |
||
| 20 | use function array_pop; |
||
| 21 | use function assert; |
||
| 22 | use function count; |
||
| 23 | use function dirname; |
||
| 24 | use function file_exists; |
||
| 25 | use function is_string; |
||
| 26 | use function React\Promise\reject; |
||
| 27 | use function spl_object_hash; |
||
| 28 | |||
| 29 | use const DIRECTORY_SEPARATOR; |
||
| 30 | |||
| 31 | final class Infinite implements LowLevelPoolInterface |
||
| 32 | { |
||
| 33 | private LoopInterface $loop; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 34 | |||
| 35 | /** @var Runtime[] */ |
||
| 36 | private array $runtimes = []; |
||
| 37 | |||
| 38 | /** @var string[] */ |
||
| 39 | private array $idleRuntimes = []; |
||
| 40 | |||
| 41 | /** @var TimerInterface[] */ |
||
| 42 | private array $ttlTimers = []; |
||
| 43 | |||
| 44 | private EventLoopBridge $eventLoopBridge; |
||
| 45 | |||
| 46 | private string $autoload; |
||
| 47 | |||
| 48 | private float $ttl; |
||
| 49 | |||
| 50 | /** @var GroupInterface[] */ |
||
| 51 | private array $groups = []; |
||
| 52 | |||
| 53 | private bool $closed = false; |
||
| 54 | |||
| 55 | public function __construct(LoopInterface $loop, EventLoopBridge $eventLoopBridge, float $ttl) |
||
| 56 | { |
||
| 57 | $this->loop = $loop; |
||
| 58 | $this->ttl = $ttl; |
||
| 59 | $this->autoload = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; |
||
| 60 | foreach ([2, 5] as $level) { |
||
| 61 | $this->autoload = dirname(__FILE__, $level) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; |
||
| 62 | if (file_exists($this->autoload)) { |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->eventLoopBridge = $eventLoopBridge; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param mixed[] $args |
||
| 72 | */ |
||
| 73 | public function run(Closure $callable, array $args = []): PromiseInterface |
||
| 74 | { |
||
| 75 | if ($this->closed === true) { |
||
| 76 | return reject(ClosedException::create()); |
||
| 77 | } |
||
| 78 | |||
| 79 | return (new Promise(function (callable $resolve, callable $reject): void { |
||
| 80 | if (count($this->idleRuntimes) === 0) { |
||
| 81 | $resolve($this->spawnRuntime()); |
||
| 82 | |||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | $resolve($this->getIdleRuntime()); |
||
| 87 | }))->then(function (Runtime $runtime) use ($callable, $args): PromiseInterface { |
||
| 88 | /** @psalm-suppress UndefinedInterfaceMethod */ |
||
| 89 | return $runtime->run($callable, $args)->always(function () use ($runtime): void { |
||
| 90 | if ($this->ttl >= 0.1) { |
||
| 91 | $this->addRuntimeToIdleList($runtime); |
||
| 92 | $this->startTtlTimer($runtime); |
||
| 93 | |||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->closeRuntime(spl_object_hash($runtime)); |
||
| 98 | }); |
||
| 99 | }); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function close(): bool |
||
| 103 | { |
||
| 104 | if (count($this->groups) > 0) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->closed = true; |
||
| 109 | |||
| 110 | foreach ($this->runtimes as $hash => $runtime) { |
||
| 111 | $this->closeRuntime($hash); |
||
| 112 | } |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function kill(): bool |
||
| 118 | { |
||
| 119 | if (count($this->groups) > 0) { |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | $this->closed = true; |
||
| 124 | |||
| 125 | foreach ($this->runtimes as $runtime) { |
||
| 126 | $runtime->kill(); |
||
| 127 | } |
||
| 128 | |||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return iterable<string, int> |
||
| 134 | */ |
||
| 135 | public function info(): iterable |
||
| 136 | { |
||
| 137 | yield Info::TOTAL => count($this->runtimes); |
||
| 138 | yield Info::BUSY => count($this->runtimes) - count($this->idleRuntimes); |
||
| 139 | yield Info::CALLS => 0; |
||
| 140 | yield Info::IDLE => count($this->idleRuntimes); |
||
| 141 | yield Info::SIZE => count($this->runtimes); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function acquireGroup(): GroupInterface |
||
| 145 | { |
||
| 146 | $group = Group::create(); |
||
| 147 | $this->groups[(string) $group] = $group; |
||
| 148 | |||
| 149 | return $group; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function releaseGroup(GroupInterface $group): void |
||
| 153 | { |
||
| 154 | unset($this->groups[(string) $group]); |
||
| 155 | } |
||
| 156 | |||
| 157 | private function getIdleRuntime(): Runtime |
||
| 158 | { |
||
| 159 | $hash = array_pop($this->idleRuntimes); |
||
| 160 | assert(is_string($hash)); |
||
| 161 | |||
| 162 | if (array_key_exists($hash, $this->ttlTimers)) { |
||
| 163 | $this->loop->cancelTimer($this->ttlTimers[$hash]); |
||
| 164 | unset($this->ttlTimers[$hash]); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $this->runtimes[$hash]; |
||
| 168 | } |
||
| 169 | |||
| 170 | private function addRuntimeToIdleList(Runtime $runtime): void |
||
| 171 | { |
||
| 172 | $hash = spl_object_hash($runtime); |
||
| 173 | $this->idleRuntimes[$hash] = $hash; |
||
| 174 | } |
||
| 175 | |||
| 176 | private function spawnRuntime(): Runtime |
||
| 177 | { |
||
| 178 | $runtime = new Runtime($this->eventLoopBridge, $this->autoload); |
||
| 179 | $this->runtimes[spl_object_hash($runtime)] = $runtime; |
||
| 180 | |||
| 181 | return $runtime; |
||
| 182 | } |
||
| 183 | |||
| 184 | private function startTtlTimer(Runtime $runtime): void |
||
| 185 | { |
||
| 186 | $hash = spl_object_hash($runtime); |
||
| 187 | |||
| 188 | $this->ttlTimers[$hash] = $this->loop->addTimer($this->ttl, function () use ($hash): void { |
||
| 189 | $this->closeRuntime($hash); |
||
| 190 | }); |
||
| 191 | } |
||
| 192 | |||
| 193 | private function closeRuntime(string $hash): void |
||
| 194 | { |
||
| 195 | $runtime = $this->runtimes[$hash]; |
||
| 196 | $runtime->close(); |
||
| 197 | |||
| 198 | unset($this->runtimes[$hash]); |
||
| 199 | |||
| 200 | if (array_key_exists($hash, $this->idleRuntimes)) { |
||
| 201 | unset($this->idleRuntimes[$hash]); |
||
| 202 | } |
||
| 203 | |||
| 204 | if (! array_key_exists($hash, $this->ttlTimers)) { |
||
| 205 | return; |
||
| 206 | } |
||
| 207 | |||
| 208 | $this->loop->cancelTimer($this->ttlTimers[$hash]); |
||
| 209 | |||
| 210 | unset($this->ttlTimers[$hash]); |
||
| 211 | } |
||
| 212 | } |
||
| 213 |