From ed41d0f61df6f19a61fdf1b9f97e7aef21b81319 Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Mon, 23 Nov 2026 17:16:46 +0200 Subject: [PATCH] fix(api-call): retry 5xx responses from the default http client --- src/ApiCall.php | 11 +++- tests/Feature/ApiCallRetryTest.php | 93 +++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src/ApiCall.php b/src/ApiCall.php index 0a528318..1bf4baae 100644 --- a/src/ApiCall.php +++ b/src/ApiCall.php @@ -309,11 +309,16 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr // For network errors, set exception and continue to retry logic $this->setNodeHealthCheck($node, false); $lastException = $exception; - } catch (Exception $exception) { - if ($exception instanceof TypesenseClientError) { + } catch (TypesenseClientError $exception) { + // For 4xx errors, and errors without an HTTP status, don't retry - throw immediately + if ($exception->getCode() < 500) { throw $exception; } - + + // For 5xx errors, set exception and continue to retry logic + $this->setNodeHealthCheck($node, false); + $lastException = $exception; + } catch (Exception $exception) { $this->setNodeHealthCheck($node, false); $lastException = $exception; } diff --git a/tests/Feature/ApiCallRetryTest.php b/tests/Feature/ApiCallRetryTest.php index 4fc400f1..4d52d176 100644 --- a/tests/Feature/ApiCallRetryTest.php +++ b/tests/Feature/ApiCallRetryTest.php @@ -8,6 +8,7 @@ use Typesense\Exceptions\ServerError; use Typesense\Exceptions\RequestMalformed; use Typesense\Exceptions\ObjectNotFound; +use Typesense\Exceptions\ServiceUnavailable; use Http\Client\Exception\HttpException; use Http\Client\Exception\TransferException; use JsonException; @@ -54,7 +55,8 @@ public function testExceptionsCarryTheHttpStatusCode(): void public function testThrownExceptionsCarryTheStatusCodeAndTheServerMessage(): void { $httpClient = $this->createMock(ClientInterface::class); - $httpClient->method('sendRequest') + $httpClient->expects($this->once()) + ->method('sendRequest') ->willReturn($this->createJsonResponseMock('{"message": "Not found."}', 404)); $config = new Configuration([ @@ -632,4 +634,93 @@ public function testSearchSurfacesATimeoutWhenEveryAttemptReturns408(): void $client->collections['books']->documents->search(['q' => '*', 'query_by' => 'title']); } + + public function testThrowsTheLastErrorWhenEveryAttemptReturns5xx(): void + { + $expectedExceptions = [ + 500 => ServerError::class, + 502 => TypesenseClientError::class, + 503 => ServiceUnavailable::class, + ]; + + foreach ($expectedExceptions as $statusCode => $expectedException) { + $callCount = 0; + + $httpClient = $this->createMock(ClientInterface::class); + $httpClient->method('sendRequest') + ->willReturnCallback(function () use (&$callCount, $statusCode) { + $callCount++; + + return $this->createJsonResponseMock( + '{"message": "Attempt ' . $callCount . ' failed."}', + $statusCode + ); + }); + + $config = new Configuration([ + 'api_key' => 'test-key', + 'nodes' => [ + ['host' => 'node1', 'port' => 8108, 'protocol' => 'http'], + ['host' => 'node2', 'port' => 8108, 'protocol' => 'http'] + ], + 'num_retries' => 2, + 'retry_interval_seconds' => 0.001, + 'client' => $httpClient + ]); + + $apiCall = new ApiCall($config); + + try { + $apiCall->get('/test', []); + $this->fail("Expected a {$expectedException} for HTTP {$statusCode}."); + } catch (TypesenseClientError $exception) { + $this->assertSame($expectedException, get_class($exception)); + $this->assertSame($statusCode, $exception->getCode()); + $this->assertSame('Attempt 3 failed.', $exception->getMessage()); + } + + $this->assertSame(3, $callCount); + } + } + + public function test5xxResponseMarksTheNodeUnhealthy(): void + { + $requestedHosts = []; + + $httpClient = $this->createMock(ClientInterface::class); + $httpClient->method('sendRequest') + ->willReturnCallback(function (RequestInterface $request) use (&$requestedHosts) { + $requestedHosts[] = $request->getUri()->getHost(); + + if ($request->getUri()->getHost() === 'node2') { + return $this->createJsonResponseMock('{"message": "Not Ready or Lagging"}', 503); + } + + return $this->createJsonResponseMock('{"success": true}'); + }); + + $config = new Configuration([ + 'api_key' => 'test-key', + 'nodes' => [ + ['host' => 'node1', 'port' => 8108, 'protocol' => 'http'], + ['host' => 'node2', 'port' => 8108, 'protocol' => 'http'] + ], + 'num_retries' => 2, + 'retry_interval_seconds' => 0.001, + 'randomize_nodes' => false, + 'client' => $httpClient + ]); + + $apiCall = new ApiCall($config); + + // Nodes are picked round-robin, so the first request starts on node2 + $this->assertEquals(['success' => true], $apiCall->get('/test', [])); + $this->assertTrue($config->getNodes()[0]->isHealthy()); + $this->assertFalse($config->getNodes()[1]->isHealthy()); + + // node2 is next in line, but is skipped until its health check is due + $this->assertEquals(['success' => true], $apiCall->get('/test', [])); + + $this->assertSame(['node2', 'node1', 'node1'], $requestedHosts); + } }