Skip to content

Commit

Permalink
handle 503 responses also in single-server case
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteemann committed Feb 18, 2018
1 parent 2795e25 commit 1582959
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/ArangoDBClient/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,25 @@ public function delete($url, array $customHeaders = [], $data = '')
*/
private function handleFailover($cb)
{
$start = microtime(true);
if (!$this->_options->haveMultipleEndpoints()) {
// the simple case: no failover
return $cb();
// the simple case: just one server
while (true) {
try {
return $cb();
} catch (FailoverException $e) {
if (microtime(true) - $start >= $this->_options[ConnectionOptions::OPTION_FAILOVER_TIMEOUT]) {
// timeout reached, we will abort now
$this->notify('servers not reachable after timeout');
throw $e;
}
// continue because we have not yet reached the timeout
usleep(20 * 1000);
}
}
}

// here we need to try it with failover
$start = microtime(true);
$tried = [];
$notReachable = [];
while (true) {
Expand Down Expand Up @@ -643,7 +655,6 @@ public function parseResponse(HttpResponse $response)

if ($httpCode < 200 || $httpCode >= 400) {
// failure on server

$body = $response->getBody();
if ($body !== '') {
// check if we can find details in the response body
Expand Down Expand Up @@ -687,6 +698,13 @@ public function parseResponse(HttpResponse $response)
throw $exception;
}
}

// check if server has responded with any other 503 response not handled above
if ($httpCode === 503) {
// generic service unavailable response
$exception = new FailoverException('service unavailable', 503);
throw $exception;
}

// no details found, throw normal exception
throw new ServerException($response->getResult(), $httpCode);
Expand Down

0 comments on commit 1582959

Please # to comment.