Skip to content

Commit 1fbf79e

Browse files
authored
Merge pull request #97 from tharropoulos/dont-throw-on-node-retry
fix(api-call): retry on 500s / 408s
2 parents 3154bcf + d2c1f45 commit 1fbf79e

File tree

2 files changed

+492
-11
lines changed

2 files changed

+492
-11
lines changed

src/ApiCall.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,40 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
266266
->getContents(), true, 512, JSON_THROW_ON_ERROR) : $response->getBody()
267267
->getContents();
268268
} catch (HttpException $exception) {
269-
if (
270-
$exception->getResponse()
271-
->getStatusCode() === 408
272-
) {
269+
$statusCode = $exception->getResponse()->getStatusCode();
270+
271+
// Skip 408 timeouts and continue to next iteration
272+
if ($statusCode === 408) {
273273
continue;
274274
}
275+
276+
// For 4xx errors, don't retry - throw immediately
277+
if (400 <= $statusCode && $statusCode < 500) {
278+
$this->setNodeHealthCheck($node, false);
279+
throw $this->getException($statusCode)
280+
->setMessage($exception->getMessage());
281+
}
282+
283+
// For 5xx errors, set exception and continue to retry logic
275284
$this->setNodeHealthCheck($node, false);
276-
throw $this->getException($exception->getResponse()
277-
->getStatusCode())
285+
$lastException = $this->getException($statusCode)
278286
->setMessage($exception->getMessage());
279-
} catch (TypesenseClientError | HttpClientException $exception) {
287+
} catch (HttpClientException $exception) {
288+
// For network errors, set exception and continue to retry logic
280289
$this->setNodeHealthCheck($node, false);
281-
throw $exception;
290+
$lastException = $exception;
282291
} catch (Exception $exception) {
292+
if ($exception instanceof TypesenseClientError) {
293+
throw $exception;
294+
}
295+
283296
$this->setNodeHealthCheck($node, false);
284297
$lastException = $exception;
285-
if ($numRetries < $this->config->getNumRetries() + 1) {
286-
sleep($this->config->getRetryIntervalSeconds());
287-
}
288298
}
299+
300+
if ($numRetries < $this->config->getNumRetries() + 1) {
301+
usleep((int) ($this->config->getRetryIntervalSeconds() * 10**6));
302+
}
289303
}
290304

291305
if ($lastException) {

0 commit comments

Comments
 (0)