Skip to content

Commit 19fcd8a

Browse files
committed
handle 503 responses also in single-server case
1 parent 7867540 commit 19fcd8a

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/ArangoDBClient/Connection.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,25 @@ public function delete($url, array $customHeaders = [], $data = '')
327327
*/
328328
private function handleFailover($cb)
329329
{
330+
$start = microtime(true);
330331
if (!$this->_options->haveMultipleEndpoints()) {
331-
// the simple case: no failover
332-
return $cb();
332+
// the simple case: just one server
333+
while (true) {
334+
try {
335+
return $cb();
336+
} catch (FailoverException $e) {
337+
if (microtime(true) - $start >= $this->_options[ConnectionOptions::OPTION_FAILOVER_TIMEOUT]) {
338+
// timeout reached, we will abort now
339+
$this->notify('servers not reachable after timeout');
340+
throw $e;
341+
}
342+
// continue because we have not yet reached the timeout
343+
usleep(20 * 1000);
344+
}
345+
}
333346
}
334347

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

644656
if ($httpCode < 200 || $httpCode >= 400) {
645657
// failure on server
646-
647658
$body = $response->getBody();
648659
if ($body !== '') {
649660
// check if we can find details in the response body
@@ -687,6 +698,13 @@ public function parseResponse(HttpResponse $response)
687698
throw $exception;
688699
}
689700
}
701+
702+
// check if server has responded with any other 503 response not handled above
703+
if ($httpCode === 503) {
704+
// generic service unavailable response
705+
$exception = new FailoverException('service unavailable', 503);
706+
throw $exception;
707+
}
690708

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

0 commit comments

Comments
 (0)