1
0
Fork 0
mirror of synced 2024-09-30 17:26:48 +13:00

Fixed connection retry

This commit is contained in:
Eldad Fux 2020-10-19 23:38:49 +03:00
parent a6cb86639b
commit a52de551a1

View file

@ -34,17 +34,18 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) {
$attempts = 0; $attempts = 0;
$start = time(); $start = time();
while ($attempts < 3) { while ($attempts < 300) {
try { try {
if($attempts > 0) {
Console::error('Connection lost (lasted '.(time() - $start).' seconds). Attempting restart in 5 seconds (attempt #'.$attempts.')');
sleep(5); // 1 sec delay between connection attempts
}
$redis = new Redis(); $redis = new Redis();
$redis->connect('redis', 6379); $redis->connect('redis', 6379);
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1); $redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
if($attempts > 0) { if($redis->ping(true)) {
Console::error('Connection lost (lasted '.(time() - $start).' seconds). Attempting restart (attempt #'.$attempts.')');
}
if($redis->ping('')) {
$attempts = 0; $attempts = 0;
Console::success('Connection established'); Console::success('Connection established');
} }
@ -52,8 +53,6 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) {
Console::error('Connection failed'); Console::error('Connection failed');
} }
sleep(1); // 1 sec delay between connection attempts
$redis->subscribe(['realtime'], function($redis, $channel, $message) use ($server, $workerId, &$connections) { $redis->subscribe(['realtime'], function($redis, $channel, $message) use ($server, $workerId, &$connections) {
$message = 'Message from worker #'.$workerId.'; '.$message; $message = 'Message from worker #'.$workerId.'; '.$message;
@ -74,12 +73,13 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) {
} }
}); });
$attempts++;
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::error('Connection error: '.$th->getMessage());
$attempts++; $attempts++;
continue; continue;
} }
$attempts++;
} }
Console::error('Failed to restart connection...'); Console::error('Failed to restart connection...');