forked from ruflin/Elastica
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ruflin#661 from chabior/master
Connection pool & round robin strategy
- Loading branch information
Showing
14 changed files
with
789 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Elastica\Connection; | ||
|
||
use Elastica\Client; | ||
use Elastica\Connection; | ||
use Elastica\Connection\Strategy\StrategyInterface; | ||
use Exception; | ||
|
||
/** | ||
* Description of ConnectionPool | ||
* | ||
* @author chabior | ||
*/ | ||
class ConnectionPool | ||
{ | ||
/** | ||
* Connections array | ||
* | ||
* @var array|\Elastica\Connection[] | ||
*/ | ||
protected $_connections; | ||
|
||
/** | ||
* Strategy for connection | ||
* | ||
* @var \Elastica\Connection\Strategy\StrategyInterface | ||
*/ | ||
protected $_strategy; | ||
|
||
/** | ||
* Callback function called on connection fail | ||
* | ||
* @var callback | ||
*/ | ||
protected $_callback; | ||
|
||
/** | ||
* @param array $connections | ||
* @param \Elastica\Connection\Strategy\StrategyInterface $strategy | ||
* @param callback $callback | ||
*/ | ||
public function __construct(array $connections, StrategyInterface $strategy, $callback = null) | ||
{ | ||
$this->_connections = $connections; | ||
|
||
$this->_strategy = $strategy; | ||
|
||
$this->_callback = $callback; | ||
} | ||
|
||
/** | ||
* @param \Elastica\Connection $connection | ||
*/ | ||
public function addConnection(Connection $connection) | ||
{ | ||
$this->_connections[] = $connection; | ||
} | ||
|
||
/** | ||
* @param array|\Elastica\Connection[] $connections | ||
*/ | ||
public function setConnections(array $connections) | ||
{ | ||
$this->_connections = $connections; | ||
} | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
public function hasConnection() | ||
{ | ||
foreach ($this->_connections as $connection) { | ||
if ($connection->isEnabled()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getConnections() | ||
{ | ||
return $this->_connections; | ||
} | ||
|
||
/** | ||
* @return \Elastica\Connection | ||
* @throws \Elastica\Exception\ClientException | ||
*/ | ||
public function getConnection() | ||
{ | ||
return $this->_strategy->getConnection($this->getConnections()); | ||
} | ||
|
||
/** | ||
* @param \Elastica\Connection $connection | ||
* @param \Exception $e | ||
* @param Client $client | ||
*/ | ||
public function onFail(Connection $connection, Exception $e, Client $client) | ||
{ | ||
$connection->setEnabled(false); | ||
|
||
if ($this->_callback) { | ||
call_user_func($this->_callback, $connection, $e, $client); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @return \Elastica\Connection\Strategy\StrategyInterface | ||
*/ | ||
public function getStrategy() | ||
{ | ||
return $this->_strategy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Elastica\Connection\Strategy; | ||
|
||
use Elastica\Exception\InvalidException; | ||
|
||
/** | ||
* Description of CallbackStrategy | ||
* | ||
* @author chabior | ||
*/ | ||
class CallbackStrategy implements StrategyInterface | ||
{ | ||
|
||
/** | ||
* @var Closure | ||
*/ | ||
protected $_callback; | ||
|
||
/** | ||
* @param Closure $callback | ||
* @throws \Elastica\Exception\InvalidException | ||
*/ | ||
public function __construct($callback) | ||
{ | ||
if (!self::isValid($callback)) { | ||
throw new InvalidException(sprintf('Callback should be a Closure, %s given!', gettype($callback))); | ||
} | ||
|
||
$this->_callback = $callback; | ||
} | ||
|
||
/** | ||
* @param array|\Elastica\Connection[] $connections | ||
* @return \Elastica\Connection | ||
*/ | ||
public function getConnection($connections) | ||
{ | ||
return $this->_callback->__invoke($connections); | ||
} | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
public static function isValid($callback) | ||
{ | ||
return is_object($callback) && ($callback instanceof \Closure); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Elastica\Connection\Strategy; | ||
|
||
/** | ||
* Description of RoundRobin | ||
* | ||
* @author chabior | ||
*/ | ||
class RoundRobin extends Simple | ||
{ | ||
|
||
/** | ||
* @param array|\Elastica\Connection[] $connections | ||
* @return \Elastica\Connection | ||
* @throws \Elastica\Exception\ClientException | ||
*/ | ||
public function getConnection($connections) | ||
{ | ||
shuffle($connections); | ||
|
||
return parent::getConnection($connections); | ||
} | ||
} |
Oops, something went wrong.