From 2eb9c55e9149ab320e7fd47fd1eb2435554f8bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C6=B0=C6=A1ng=20Gia=20B=E1=BA=A3o?= Date: Fri, 24 May 2024 21:26:21 +0700 Subject: [PATCH] Prevent blocking stuff (#195) --- composer.json | 2 +- .../Reverb/Console/Commands/StartServer.php | 18 ++++++++++-------- src/Servers/Reverb/Http/Server.php | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index dfa86d36..3770fe2c 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "laravel/prompts": "^0.1.15", "pusher/pusher-php-server": "^7.2", "ratchet/rfc6455": "^0.3.1", + "react/async": "^4.2", "react/promise-timer": "^1.10", "react/socket": "^1.14", "symfony/console": "^6.0|^7.0", @@ -39,7 +40,6 @@ "pestphp/pest": "^2.0", "phpstan/phpstan": "^1.10", "ratchet/pawl": "^0.4.1", - "react/async": "^4.0", "react/http": "^1.9" }, "autoload": { diff --git a/src/Servers/Reverb/Console/Commands/StartServer.php b/src/Servers/Reverb/Console/Commands/StartServer.php index d4209444..5da26d1e 100644 --- a/src/Servers/Reverb/Console/Commands/StartServer.php +++ b/src/Servers/Reverb/Console/Commands/StartServer.php @@ -20,6 +20,8 @@ use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\SignalableCommandInterface; +use function React\Async\async; + #[AsCommand(name: 'reverb:start')] class StartServer extends Command implements SignalableCommandInterface { @@ -90,10 +92,10 @@ protected function ensureHorizontalScalability(LoopInterface $loop): void */ protected function ensureStaleConnectionsAreCleaned(LoopInterface $loop): void { - $loop->addPeriodicTimer(60, function () { + $loop->addPeriodicTimer(60, async(function () { PruneStaleConnections::dispatch(); PingInactiveConnections::dispatch(); - }); + })); } /** @@ -103,7 +105,7 @@ protected function ensureRestartCommandIsRespected(Server $server, LoopInterface { $lastRestart = Cache::get('laravel:reverb:restart'); - $loop->addPeriodicTimer(5, function () use ($server, $host, $port, $lastRestart) { + $loop->addPeriodicTimer(5, async(function () use ($server, $host, $port, $lastRestart) { if ($lastRestart === Cache::get('laravel:reverb:restart')) { return; } @@ -113,7 +115,7 @@ protected function ensureRestartCommandIsRespected(Server $server, LoopInterface $server->stop(); $this->components->info("Stopping server on {$host}:{$port}"); - }); + })); } /** @@ -141,9 +143,9 @@ protected function ensurePulseEventsAreCollected(LoopInterface $loop, int $inter return; } - $loop->addPeriodicTimer($interval, function () { + $loop->addPeriodicTimer($interval, async(function () { $this->laravel->make(\Laravel\Pulse\Pulse::class)->ingest(); - }); + })); } /** @@ -155,9 +157,9 @@ protected function ensureTelescopeEntriesAreCollected(LoopInterface $loop, int $ return; } - $loop->addPeriodicTimer($interval, function () { + $loop->addPeriodicTimer($interval, async(function () { \Laravel\Telescope\Telescope::store($this->laravel->make(\Laravel\Telescope\Contracts\EntriesRepository::class)); - }); + })); } /** diff --git a/src/Servers/Reverb/Http/Server.php b/src/Servers/Reverb/Http/Server.php index 182a849f..bdb44d8a 100644 --- a/src/Servers/Reverb/Http/Server.php +++ b/src/Servers/Reverb/Http/Server.php @@ -27,7 +27,7 @@ public function __construct(protected ServerInterface $socket, protected Router $this->loop = $loop ?: Loop::get(); - $this->loop->addPeriodicTimer(30, fn () => gc_collect_cycles()); + $this->loop->addPeriodicTimer(30, \React\Async\async(fn () => gc_collect_cycles())); // Register __invoke handler for this class to receive new connections... $socket->on('connection', $this);