private function connectToFirstAvailableHost()
{
if (count($this->protocols) > 0) {
return reset($this->protocols);
}
$latest_error = null;
foreach ($this->hosts as $host_key => &$host_info) {
try {
$protocol = $this->connectToHost($host_info['host'], $host_info['port']);
// We were able to connect, so update host status
$host_info['health'] = static::HEALTHY;
// assume healthy since we connected
$host_info['state'] = static::STATE_STARTUP;
// Default to unknown
return $protocol;
} catch (MongoConnectionException $e) {
$latest_error = $e;
continue;
}
}
$msg = "Could not connect to any of " . count($this->hosts) . " hosts. Latest error: " . ($latest_error ? $latest_error->getMessage() : '');
throw new MongoConnectionException($msg);
}