1
0
Fork 0
mirror of synced 2024-06-29 19:50:26 +12: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;
$start = time();
while ($attempts < 3) {
while ($attempts < 300) {
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->connect('redis', 6379);
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
if($attempts > 0) {
Console::error('Connection lost (lasted '.(time() - $start).' seconds). Attempting restart (attempt #'.$attempts.')');
}
if($redis->ping('')) {
if($redis->ping(true)) {
$attempts = 0;
Console::success('Connection established');
}
@ -52,8 +53,6 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) {
Console::error('Connection failed');
}
sleep(1); // 1 sec delay between connection attempts
$redis->subscribe(['realtime'], function($redis, $channel, $message) use ($server, $workerId, &$connections) {
$message = 'Message from worker #'.$workerId.'; '.$message;
@ -74,12 +73,13 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) {
}
});
$attempts++;
} catch (\Throwable $th) {
Console::error('Connection error: '.$th->getMessage());
$attempts++;
continue;
}
$attempts++;
}
Console::error('Failed to restart connection...');