private function switchNodeIfNeeded()
{
$sortedNodes = $this->priorityStrategy->sort($this->nodes, $this->nodeId);
// Try to connect by priority, continue on error, return on success
foreach ($sortedNodes as $nodeCandidate) {
// If the first recommended node is our current node and it has
// a working connection, return early.
// If its connection is not working, let's try and reconnect further
// below, or find the first best connected node.
if ($nodeCandidate->getId() === $this->nodeId && $nodeCandidate->isConnected()) {
return;
}
try {
if ($nodeCandidate->isConnected()) {
// Say a new HELLO to the node, the cluster might have changed
$nodeCandidate->sayHello();
} else {
$nodeCandidate->connect();
}
} catch (ConnectionException $e) {
continue;
}
$this->switchToNode($nodeCandidate);
return;
}
throw new ConnectionException('Could not switch to any node');
}