Skip to content

Commit bcdb36c

Browse files
authored
Merge pull request #127 from arpitjain099/fix/408-retry-exhaustion
fix(api-call): report the timeout when every retry ends in a 408
2 parents 9ee0d8e + 5493840 commit bcdb36c

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎src/ApiCall.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,11 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
286286
} catch (HttpException $exception) {
287287
$statusCode = $exception->getResponse()->getStatusCode();
288288

289-
// Skip 408 timeouts and continue to next iteration
289+
// Skip 408 timeouts and continue to next iteration, but keep the
290+
// exception so that exhausting the retries still reports the timeout
290291
if ($statusCode === 408) {
292+
$lastException = $this->getException($statusCode)
293+
->setMessage($exception->getMessage());
291294
continue;
292295
}
293296

‎tests/Feature/ApiCallRetryTest.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,67 @@ public function testDrainedResponseBodyStillDecodesWithoutRetrying(): void
569569
$this->assertSame(['ok' => true], $result);
570570
$this->assertSame(1, $callCount);
571571
}
572+
573+
public function testEvery408AttemptStillThrowsInsteadOfReturningNothing(): void
574+
{
575+
$callCount = 0;
576+
577+
$httpClient = $this->createMock(ClientInterface::class);
578+
$httpClient->method('sendRequest')
579+
->willReturnCallback(function() use (&$callCount) {
580+
$callCount++;
581+
582+
$response = $this->createMock(ResponseInterface::class);
583+
$response->method('getStatusCode')->willReturn(408);
584+
throw new HttpException('Request timeout', $this->createMock(RequestInterface::class), $response);
585+
});
586+
587+
$config = new Configuration([
588+
'api_key' => 'test-key',
589+
'nodes' => [
590+
['host' => 'node1', 'port' => 8108, 'protocol' => 'http'],
591+
['host' => 'node2', 'port' => 8108, 'protocol' => 'http']
592+
],
593+
'num_retries' => 2,
594+
'retry_interval_seconds' => 0,
595+
'client' => $httpClient
596+
]);
597+
598+
$apiCall = new ApiCall($config);
599+
600+
try {
601+
$result = $apiCall->get('/test', []);
602+
$this->fail('Expected a TypesenseClientError, got ' . var_export($result, true));
603+
} catch (TypesenseClientError $exception) {
604+
$this->assertSame(408, $exception->getCode());
605+
$this->assertSame('Request timeout', $exception->getMessage());
606+
}
607+
608+
$this->assertSame(3, $callCount);
609+
}
610+
611+
public function testSearchSurfacesATimeoutWhenEveryAttemptReturns408(): void
612+
{
613+
$httpClient = $this->createMock(ClientInterface::class);
614+
$httpClient->method('sendRequest')
615+
->willReturnCallback(function() {
616+
$response = $this->createMock(ResponseInterface::class);
617+
$response->method('getStatusCode')->willReturn(408);
618+
throw new HttpException('Request timeout', $this->createMock(RequestInterface::class), $response);
619+
});
620+
621+
$client = new \Typesense\Client([
622+
'api_key' => 'test-key',
623+
'nodes' => [
624+
['host' => 'node1', 'port' => 8108, 'protocol' => 'http']
625+
],
626+
'num_retries' => 1,
627+
'retry_interval_seconds' => 0,
628+
'client' => $httpClient
629+
]);
630+
631+
$this->expectException(TypesenseClientError::class);
632+
633+
$client->collections['books']->documents->search(['q' => '*', 'query_by' => 'title']);
634+
}
572635
}

0 commit comments

Comments
 (0)