Skip to content

Commit

Permalink
Merge branch 'master' into bump_190
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Petr authored Apr 3, 2017
2 parents 8549381 + 5391649 commit e051503
Show file tree
Hide file tree
Showing 9 changed files with 736 additions and 128 deletions.
95 changes: 83 additions & 12 deletions lib/AlgoliaSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Client
const CAINFO = 'cainfo';
const CURLOPT = 'curloptions';
const PLACES_ENABLED = 'placesEnabled';
const FAILING_HOSTS_CACHE = 'failingHostsCache';

/**
* @var ClientContext
Expand Down Expand Up @@ -94,12 +95,18 @@ public function __construct($applicationID, $apiKey, $hostsArray = null, $option
case self::PLACES_ENABLED:
$this->placesEnabled = (bool) $value;
break;
case self::FAILING_HOSTS_CACHE:
if (! $value instanceof FailingHostsCache) {
throw new \InvalidArgumentException('failingHostsCache must be an instance of \AlgoliaSearch\FailingHostsCache.');
}
break;
default:
throw new \Exception('Unknown option: '.$option);
}
}

$this->context = new ClientContext($applicationID, $apiKey, $hostsArray, $this->placesEnabled);
$failingHostsCache = isset($options[self::FAILING_HOSTS_CACHE]) ? $options[self::FAILING_HOSTS_CACHE] : null;
$this->context = new ClientContext($applicationID, $apiKey, $hostsArray, $this->placesEnabled, $failingHostsCache);
}

/**
Expand Down Expand Up @@ -408,13 +415,13 @@ public function initIndex($indexName)
}

/**
* List all existing user keys with their associated ACLs.
* List all existing API keys with their associated ACLs.
*
* @return mixed
*
* @throws AlgoliaException
*/
public function listUserKeys()
public function listApiKeys()
{
return $this->request(
$this->context,
Expand All @@ -429,13 +436,22 @@ public function listUserKeys()
}

/**
* Get ACL of a user key.
* @return mixed
* @deprecated use listApiKeys instead
*/
public function listUserKeys()
{
return $this->listApiKeys();
}

/**
* Get ACL of a API key.
*
* @param string $key
*
* @return mixed
*/
public function getUserKeyACL($key)
public function getApiKey($key)
{
return $this->request(
$this->context,
Expand All @@ -450,13 +466,23 @@ public function getUserKeyACL($key)
}

/**
* Delete an existing user key.
* @param $key
* @return mixed
* @deprecated use getApiKey instead
*/
public function getUserKeyACL($key)
{
return $this->getApiKey($key);
}

/**
* Delete an existing API key.
*
* @param string $key
*
* @return mixed
*/
public function deleteUserKey($key)
public function deleteApiKey($key)
{
return $this->request(
$this->context,
Expand All @@ -471,7 +497,17 @@ public function deleteUserKey($key)
}

/**
* Create a new user key.
* @param $key
* @return mixed
* @deprecated use deleteApiKey instead
*/
public function deleteUserKey($key)
{
return $this->deleteApiKey($key);
}

/**
* Create a new API key.
*
* @param array $obj can be two different parameters:
* The list of parameters for this key. Defined by an array that
Expand Down Expand Up @@ -504,7 +540,7 @@ public function deleteUserKey($key)
*
* @throws AlgoliaException
*/
public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
public function addApiKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
{
if ($obj !== array_values($obj)) { // is dict of value
$params = $obj;
Expand Down Expand Up @@ -537,7 +573,21 @@ public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $ma
}

/**
* Update a user key.
* @param $obj
* @param int $validity
* @param int $maxQueriesPerIPPerHour
* @param int $maxHitsPerQuery
* @param null $indexes
* @return mixed
* @deprecated use addApiKey instead
*/
public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
{
return $this->addApiKey($obj, $validity, $maxQueriesPerIPPerHour, $maxHitsPerQuery, $indexes);
}

/**
* Update an API key.
*
* @param string $key
* @param array $obj can be two different parameters:
Expand Down Expand Up @@ -571,7 +621,7 @@ public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $ma
*
* @throws AlgoliaException
*/
public function updateUserKey(
public function updateApiKey(
$key,
$obj,
$validity = 0,
Expand Down Expand Up @@ -608,6 +658,27 @@ public function updateUserKey(
);
}

/**
* @param $key
* @param $obj
* @param int $validity
* @param int $maxQueriesPerIPPerHour
* @param int $maxHitsPerQuery
* @param null $indexes
* @return mixed
* @deprecated use updateApiKey instead
*/
public function updateUserKey(
$key,
$obj,
$validity = 0,
$maxQueriesPerIPPerHour = 0,
$maxHitsPerQuery = 0,
$indexes = null
) {
return $this->updateApiKey($key, $obj, $validity, $maxQueriesPerIPPerHour, $maxHitsPerQuery, $indexes);
}

/**
* Send a batch request targeting multiple indices.
*
Expand Down Expand Up @@ -918,7 +989,7 @@ public function doRequest(
curl_close($curlHandle);

if (intval($http_status / 100) == 4) {
throw new AlgoliaException(isset($answer['message']) ? $answer['message'] : $http_status . ' error');
throw new AlgoliaException(isset($answer['message']) ? $answer['message'] : $http_status.' error');
} elseif (intval($http_status / 100) != 2) {
throw new \Exception($http_status.': '.$response);
}
Expand Down
51 changes: 31 additions & 20 deletions lib/AlgoliaSearch/ClientContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,20 @@ class ClientContext
public $connectTimeout;

/**
* @var array
* @var FailingHostsCache
*/
private static $failingHosts = array();
private $failingHostsCache;

/**
* ClientContext constructor.
*
* @param string $applicationID
* @param string $apiKey
* @param array $hostsArray
* @param bool $placesEnabled
* @param string $applicationID
* @param string $apiKey
* @param array $hostsArray
* @param bool $placesEnabled
* @param FailingHostsCache $failingHostsCache
*
* @throws Exception
*/
public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled = false)
public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled = false, FailingHostsCache $failingHostsCache = null)
{
// connect timeout of 1s by default
$this->connectTimeout = 1;
Expand All @@ -113,8 +112,6 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
$this->writeHostsArray = $this->getDefaultWriteHosts();
}

$this->rotateHosts();

if ($this->applicationID == null || mb_strlen($this->applicationID) == 0) {
throw new Exception('AlgoliaSearch requires an applicationID.');
}
Expand All @@ -129,6 +126,14 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
$this->algoliaUserToken = null;
$this->rateLimitAPIKey = null;
$this->headers = array();

if ($failingHostsCache === null) {
$this->failingHostsCache = new InMemoryFailingHostsCache();
} else {
$this->failingHostsCache = $failingHostsCache;
}

$this->rotateHosts();
}

/**
Expand Down Expand Up @@ -182,7 +187,7 @@ private function getDefaultWriteHosts()
*/
public function __destruct()
{
if ($this->curlMHandle != null) {
if (is_resource($this->curlMHandle)) {
curl_multi_close($this->curlMHandle);
}
}
Expand All @@ -194,7 +199,7 @@ public function __destruct()
*/
public function getMHandle($curlHandle)
{
if ($this->curlMHandle == null) {
if (!is_resource($this->curlMHandle)) {
$this->curlMHandle = curl_multi_init();
}
curl_multi_add_handle($this->curlMHandle, $curlHandle);
Expand Down Expand Up @@ -258,15 +263,20 @@ public function setExtraHeader($key, $value)
}

/**
* @param $host
* @param string $host
*/
public static function addFailingHost($host)
public function addFailingHost($host)
{
if (! in_array($host, self::$failingHosts)) {
self::$failingHosts[] = $host;
}
$this->failingHostsCache->addFailingHost($host);
}

/**
* @return FailingHostsCache
*/
public function getFailingHostsCache()
{
return $this->failingHostsCache;
}
/**
* This method is called to pass on failing hosts.
* If the host is first either in the failingHosts array, we
Expand All @@ -276,14 +286,15 @@ public static function addFailingHost($host)
*/
public function rotateHosts()
{
$failingHosts = $this->failingHostsCache->getFailingHosts();
$i = 0;
while ($i <= count($this->readHostsArray) && in_array($this->readHostsArray[0], self::$failingHosts)) {
while ($i <= count($this->readHostsArray) && in_array($this->readHostsArray[0], $failingHosts)) {
$i++;
$this->readHostsArray[] = array_shift($this->readHostsArray);
}

$i = 0;
while ($i <= count($this->writeHostsArray) && in_array($this->writeHostsArray[0], self::$failingHosts)) {
while ($i <= count($this->writeHostsArray) && in_array($this->writeHostsArray[0], $failingHosts)) {
$i++;
$this->writeHostsArray[] = array_shift($this->writeHostsArray);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/AlgoliaSearch/FailingHostsCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace AlgoliaSearch;

interface FailingHostsCache
{
/**
* @param string $host
*/
public function addFailingHost($host);

/**
* Get failing hosts from cache. This method should also handle cache invalidation if required.
* The TTL of the failed hosts cache should be 5 minutes.
*
* @return array
*/
public function getFailingHosts();

/**
* Invalidates the cache.
*/
public function flushFailingHostsCache();
}
Loading

0 comments on commit e051503

Please sign in to comment.