Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,11 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
} catch (HttpException $exception) {
$statusCode = $exception->getResponse()->getStatusCode();

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

Expand Down
63 changes: 63 additions & 0 deletions tests/Feature/ApiCallRetryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,67 @@ public function testDrainedResponseBodyStillDecodesWithoutRetrying(): void
$this->assertSame(['ok' => true], $result);
$this->assertSame(1, $callCount);
}

public function testEvery408AttemptStillThrowsInsteadOfReturningNothing(): void
{
$callCount = 0;

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('sendRequest')
->willReturnCallback(function() use (&$callCount) {
$callCount++;

$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(408);
throw new HttpException('Request timeout', $this->createMock(RequestInterface::class), $response);
});

$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,
'client' => $httpClient
]);

$apiCall = new ApiCall($config);

try {
$result = $apiCall->get('/test', []);
$this->fail('Expected a TypesenseClientError, got ' . var_export($result, true));
} catch (TypesenseClientError $exception) {
$this->assertSame(408, $exception->getCode());
$this->assertSame('Request timeout', $exception->getMessage());
}

$this->assertSame(3, $callCount);
}

public function testSearchSurfacesATimeoutWhenEveryAttemptReturns408(): void
{
$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('sendRequest')
->willReturnCallback(function() {
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(408);
throw new HttpException('Request timeout', $this->createMock(RequestInterface::class), $response);
});

$client = new \Typesense\Client([
'api_key' => 'test-key',
'nodes' => [
['host' => 'node1', 'port' => 8108, 'protocol' => 'http']
],
'num_retries' => 1,
'retry_interval_seconds' => 0,
'client' => $httpClient
]);

$this->expectException(TypesenseClientError::class);

$client->collections['books']->documents->search(['q' => '*', 'query_by' => 'title']);
}
}
Loading