Skip to content

Commit

Permalink
Merge pull request #77 from algolia/feature/fallback-strategy-compliance
Browse files Browse the repository at this point in the history
Randomize the fallback hosts on a per client instance basis.
  • Loading branch information
rayrutjes committed May 10, 2016
2 parents 8976175 + da9edbe commit 3effa6e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
5 changes: 3 additions & 2 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ return Symfony\CS\Config\Config::create()
->fixers([
'align_double_arrow',
'short_array_syntax',
'-multiline_array_trailing_comma'
'-multiline_array_trailing_comma',
'-pre_increment',
])
->finder($finder)
;
;
2 changes: 1 addition & 1 deletion src/AlgoliaSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ public function doRequest(
//curl_setopt($curlHandle, CURLOPT_VERBOSE, true);
$curlHeaders = [];
foreach ($context->headers as $key => $value) {
$curlHeaders[] = $key . ": " . $value;
$curlHeaders[] = $key.': '.$value;
}
if ($context->adminAPIKey == null) {
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([
Expand Down
21 changes: 15 additions & 6 deletions src/AlgoliaSearch/ClientContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,33 +131,42 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
private function getDefaultReadHosts($placesEnabled)
{
if ($placesEnabled) {
return [
'places-dsn.algolia.net',
$hosts = [
'places-1.algolianet.com',
'places-2.algolianet.com',
'places-3.algolianet.com',
];
shuffle($hosts);
array_unshift($hosts, 'places-dsn.algolia.net');

return $hosts;
}

return [
$this->applicationID.'-dsn.algolia.net',
$hosts = [
$this->applicationID.'-1.algolianet.com',
$this->applicationID.'-2.algolianet.com',
$this->applicationID.'-3.algolianet.com',
];
shuffle($hosts);
array_unshift($hosts, $this->applicationID.'-dsn.algolia.net');

return $hosts;
}

/**
* @return array
*/
private function getDefaultWriteHosts()
{
return [
$this->applicationID.'.algolia.net',
$hosts = [
$this->applicationID.'-1.algolianet.com',
$this->applicationID.'-2.algolianet.com',
$this->applicationID.'-3.algolianet.com',
];
shuffle($hosts);
array_unshift($hosts, $this->applicationID.'.algolia.net');

return $hosts;
}

/**
Expand Down
101 changes: 101 additions & 0 deletions tests/AlgoliaSearch/Tests/ClientContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace AlgoliaSearch\Tests;

use AlgoliaSearch\ClientContext;

class ClientContextTest extends \PHPUnit_Framework_TestCase
{
public function testRandomReadFallbackHosts()
{
$context = new ClientContext('whatever', 'whatever', null);
$hosts = $context->readHostsArray;

// Here we check that different calls results in the hosts being in and different order.
$isRandom = false;
for ($i = 0; $i < 100; $i++) {
$otherContext = new ClientContext('whatever', 'whatever', null);
if ($hosts !== $otherContext->readHostsArray) {
$isRandom = true;
break;
}
}

$this->assertTrue($isRandom);

// Check that the first entry is the correct API endpoint.
$this->assertEquals('whatever-dsn.algolia.net', $hosts[0]);

// As hosts are in a random order, we sort everything to be sure the correct hosts are present.
sort($hosts, SORT_STRING);
$expectedHosts = [
'whatever-1.algolianet.com',
'whatever-2.algolianet.com',
'whatever-3.algolianet.com',
'whatever-dsn.algolia.net',
];
$this->assertEquals($expectedHosts, $hosts);
}

public function testRandomReadPlacesFallbackHosts()
{
$context = new ClientContext('whatever', 'whatever', null, true);
$hosts = $context->readHostsArray;

// Here we check that different calls results in the hosts being in and different order.
$isRandom = false;
for ($i = 0; $i < 100; $i++) {
$otherContext = new ClientContext('whatever', 'whatever', null);
if ($hosts !== $otherContext->readHostsArray) {
$isRandom = true;
break;
}
}

$this->assertTrue($isRandom);

// Check that the first entry is the correct places API endpoint.
$this->assertEquals('places-dsn.algolia.net', $hosts[0]);

// As hosts are in a random order, we sort everything to be sure the correct hosts are present.
sort($hosts, SORT_STRING);
$expectedHosts = [
'places-1.algolianet.com',
'places-2.algolianet.com',
'places-3.algolianet.com',
'places-dsn.algolia.net',
];
$this->assertEquals($expectedHosts, $hosts);
}

public function testRandomWriteFallbackHosts()
{
$context = new ClientContext('whatever', 'whatever', null);
$hosts = $context->writeHostsArray;

// Here we check that different calls results in the hosts being in and different order.
$isRandom = false;
for ($i = 0; $i < 100; $i++) {
$otherContext = new ClientContext('whatever', 'whatever', null);
if ($hosts !== $otherContext->writeHostsArray) {
$isRandom = true;
break;
}
}

$this->assertTrue($isRandom);

// Check that the first entry is the correct API endpoint.
$this->assertEquals('whatever.algolia.net', $hosts[0]);

// As hosts are in a random order, we sort everything to be sure the correct hosts are present.
sort($hosts, SORT_STRING);
$expectedHosts = [
'whatever-1.algolianet.com',
'whatever-2.algolianet.com',
'whatever-3.algolianet.com',
'whatever.algolia.net',
];
$this->assertEquals($expectedHosts, $hosts);
}
}
2 changes: 1 addition & 1 deletion tests/AlgoliaSearch/Tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ public function microtime_float()
{
list($usec, $sec) = explode(' ', microtime());

return ((float) $usec + (float) $sec);
return (float) $usec + (float) $sec;
}
}
8 changes: 1 addition & 7 deletions tests/AlgoliaSearch/Tests/InitPlacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ class InitPlacesTest extends AlgoliaSearchTestCase
public function testInitPlaces()
{
$placesIndex = Client::initPlaces(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_API_KEY'));

$this->assertEquals([
'places-dsn.algolia.net',
'places-1.algolianet.com',
'places-2.algolianet.com',
'places-3.algolianet.com'
], $placesIndex->getContext()->readHostsArray);
$this->assertInstanceOf('\AlgoliaSearch\PlacesIndex', $placesIndex);
}

public function testExtraHeader()
Expand Down

0 comments on commit 3effa6e

Please sign in to comment.