Skip to content

Commit 84f5e1b

Browse files
authored
Fixes retry strategy when hosts are unreachable (#572)
1 parent f9aad3c commit 84f5e1b

File tree

4 files changed

+70
-60
lines changed

4 files changed

+70
-60
lines changed

src/Http/Guzzle6HttpClient.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Http;
44

5+
use Algolia\AlgoliaSearch\Http\Psr7\Response;
56
use GuzzleHttp\Client as GuzzleClient;
67
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
78
use GuzzleHttp\HandlerStack;
@@ -27,9 +28,15 @@ public function sendRequest(RequestInterface $request, $timeout, $connectTimeout
2728
} catch (GuzzleRequestException $e) {
2829
if ($e->hasResponse()) {
2930
return $e->getResponse();
31+
} else {
32+
return new Response(
33+
0,
34+
array(),
35+
null,
36+
'1.1',
37+
$e->getMessage()
38+
);
3039
}
31-
32-
throw $e;
3340
}
3441

3542
return $response;

src/Http/Php53HttpClient.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Algolia\AlgoliaSearch\Http;
44

5-
use Algolia\AlgoliaSearch\Exceptions\RetriableException;
65
use Algolia\AlgoliaSearch\Http\Psr7\Response;
76
use Psr\Http\Message\RequestInterface;
87

@@ -103,17 +102,11 @@ public function sendRequest(RequestInterface $request, $timeout, $connectTimeout
103102
$statusCode = (int) curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
104103
$responseBody = curl_multi_getcontent($curlHandle);
105104
$error = curl_error($curlHandle);
105+
106106
$this->releaseMHandle($curlHandle);
107107
curl_close($curlHandle);
108108

109-
if (!empty($error)) {
110-
throw new RetriableException(
111-
'An internal server error occurred on '.$request->getUri()->getHost(),
112-
$statusCode
113-
);
114-
}
115-
116-
return new Response($statusCode, array(), $responseBody);
109+
return new Response($statusCode, array(), $responseBody, '1.1', $error);
117110
}
118111

119112
private function getMHandle($curlHandle)

src/RetryStrategy/ApiWrapper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,14 @@ private function handleResponse(ResponseInterface $response, RequestInterface $r
185185
$statusCode = $response->getStatusCode();
186186

187187
if (0 === $statusCode || $statusCode >= 500) {
188+
$reason = $response->getReasonPhrase();
189+
190+
if (null === $response->getReasonPhrase() || '' === $response->getReasonPhrase()) {
191+
$reason = $statusCode >= 500 ? 'Internal Server Error' : 'Unreachable Host';
192+
}
193+
188194
throw new RetriableException(
189-
'An internal server error occurred on '.$request->getUri()->getHost(),
195+
'Retriable failure on '.$request->getUri()->getHost().': '.$reason,
190196
$statusCode
191197
);
192198
}

tests/Integration/SettingsTest.php

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,65 @@
66

77
class SettingsTest extends AlgoliaIntegrationTestCase
88
{
9-
private $settings = array(
10-
'searchableAttributes' => array('attribute1', 'attribute2', 'attribute3', 'ordered(attribute4)', 'unordered(attribute5)'),
11-
'attributesForFaceting' => array('attribute1', 'filterOnly(attribute2)', 'searchable(attribute3)'),
12-
'unretrievableAttributes' => array('attribute1', 'attribute2'),
13-
'attributesToRetrieve' => array('attribute3', 'attribute4'),
14-
'ranking' => array('asc(attribute1)', 'desc(attribute2)', 'attribute', 'custom', 'exact', 'filters', 'geo', 'proximity', 'typo', 'words'),
15-
'customRanking' => array('asc(attribute1)', 'desc(attribute1)'),
16-
'replicas' => array('main_replica1', 'main_replica2'),
17-
'maxValuesPerFacet' => 100,
18-
'sortFacetValuesBy' => 'count',
19-
'attributesToHighlight' => array('attribute1', 'attribute2'),
20-
'attributesToSnippet' => array('attribute1:10', 'attribute2:8'),
21-
'highlightPreTag' => '<strong>',
22-
'highlightPostTag' => '</strong>',
23-
'snippetEllipsisText' => ' and so on.',
24-
'restrictHighlightAndSnippetArrays' => true,
25-
'hitsPerPage' => 42,
26-
'paginationLimitedTo' => 43,
27-
'minWordSizefor1Typo' => 2,
28-
'minWordSizefor2Typos' => 6,
29-
'typoTolerance' => 'false',
30-
'allowTyposOnNumericTokens' => false,
31-
'ignorePlurals' => true,
32-
'disableTypoToleranceOnAttributes' => array('attribute1', 'attribute2'),
33-
'disableTypoToleranceOnWords' => array('word1', 'word2'),
34-
'separatorsToIndex' => '()array()(',
35-
'queryType' => 'prefixNone',
36-
'removeWordsIfNoResults' => 'allOptional',
37-
'advancedSyntax' => true,
38-
'optionalWords' => array('word1', 'word2'),
39-
'removeStopWords' => true,
40-
'disablePrefixOnAttributes' => array('attribute1', 'attribute2'),
41-
'disableExactOnAttributes' => array('attribute1', 'attribute2'),
42-
'exactOnSingleWordQuery' => 'word',
43-
'enableRules' => false,
44-
'numericAttributesForFiltering' => array('attribute1', 'attribute2'),
45-
'allowCompressionOfIntegerArray' => true,
46-
'attributeForDistinct' => 'attribute1',
47-
'distinct' => 2,
48-
'replaceSynonymsInHighlight' => false,
49-
'minProximity' => 7,
50-
'responseFields' => array('hits', 'hitsPerPage'),
51-
'maxFacetHits' => 100,
52-
'camelCaseAttributes' => array('attribute1', 'attribute2'),
53-
'decompoundedAttributes' => array('de' => array('attribute1', 'attribute2'), 'fi' => array('attribute3')),
54-
'keepDiacriticsOnCharacters' => 'øé',
55-
);
9+
private $settings = array();
5610

5711
protected function setUp()
5812
{
5913
parent::setUp();
6014

61-
if (!isset(static::$indexes['main'])) {
15+
if (! isset(static::$indexes['main'])) {
16+
static::$indexes['main_replica_1'] = self::safeName('settings-mgmt-replica-1');
17+
static::$indexes['main_replica_2'] = self::safeName('settings-mgmt-replica-2');
6218
static::$indexes['main'] = self::safeName('settings-mgmt');
6319
}
20+
21+
$this->settings = array(
22+
'searchableAttributes' => array('attribute1', 'attribute2', 'attribute3', 'ordered(attribute4)', 'unordered(attribute5)'),
23+
'attributesForFaceting' => array('attribute1', 'filterOnly(attribute2)', 'searchable(attribute3)'),
24+
'unretrievableAttributes' => array('attribute1', 'attribute2'),
25+
'attributesToRetrieve' => array('attribute3', 'attribute4'),
26+
'ranking' => array('asc(attribute1)', 'desc(attribute2)', 'attribute', 'custom', 'exact', 'filters', 'geo', 'proximity', 'typo', 'words'),
27+
'customRanking' => array('asc(attribute1)', 'desc(attribute1)'),
28+
'replicas' => array(static::$indexes['main_replica_1'], static::$indexes['main_replica_2']),
29+
'maxValuesPerFacet' => 100,
30+
'sortFacetValuesBy' => 'count',
31+
'attributesToHighlight' => array('attribute1', 'attribute2'),
32+
'attributesToSnippet' => array('attribute1:10', 'attribute2:8'),
33+
'highlightPreTag' => '<strong>',
34+
'highlightPostTag' => '</strong>',
35+
'snippetEllipsisText' => ' and so on.',
36+
'restrictHighlightAndSnippetArrays' => true,
37+
'hitsPerPage' => 42,
38+
'paginationLimitedTo' => 43,
39+
'minWordSizefor1Typo' => 2,
40+
'minWordSizefor2Typos' => 6,
41+
'typoTolerance' => 'false',
42+
'allowTyposOnNumericTokens' => false,
43+
'ignorePlurals' => true,
44+
'disableTypoToleranceOnAttributes' => array('attribute1', 'attribute2'),
45+
'disableTypoToleranceOnWords' => array('word1', 'word2'),
46+
'separatorsToIndex' => '()array()(',
47+
'queryType' => 'prefixNone',
48+
'removeWordsIfNoResults' => 'allOptional',
49+
'advancedSyntax' => true,
50+
'optionalWords' => array('word1', 'word2'),
51+
'removeStopWords' => true,
52+
'disablePrefixOnAttributes' => array('attribute1', 'attribute2'),
53+
'disableExactOnAttributes' => array('attribute1', 'attribute2'),
54+
'exactOnSingleWordQuery' => 'word',
55+
'enableRules' => false,
56+
'numericAttributesForFiltering' => array('attribute1', 'attribute2'),
57+
'allowCompressionOfIntegerArray' => true,
58+
'attributeForDistinct' => 'attribute1',
59+
'distinct' => 2,
60+
'replaceSynonymsInHighlight' => false,
61+
'minProximity' => 7,
62+
'responseFields' => array('hits', 'hitsPerPage'),
63+
'maxFacetHits' => 100,
64+
'camelCaseAttributes' => array('attribute1', 'attribute2'),
65+
'decompoundedAttributes' => array('de' => array('attribute1', 'attribute2'), 'fi' => array('attribute3')),
66+
'keepDiacriticsOnCharacters' => 'øé',
67+
);
6468
}
6569

6670
public function testSettings()

0 commit comments

Comments
 (0)