mirror of
https://github.com/predis/predis.git
synced 2026-08-28 03:02:04 +00:00
Specify a timeout for the connect() operation to sentinel servers.
This value should be reasonably low so that the client can fallback to the next sentinel if the connect() operation is taking too much and slowing things down. When the connection parameters of sentinels contain a "timeout" parameter, its value takes the precedence over the default sentinels timeout.
This commit is contained in:
@@ -16,6 +16,7 @@ use Predis\Connection\ConnectionException;
|
||||
use Predis\Connection\Factory as ConnectionFactory;
|
||||
use Predis\Connection\FactoryInterface as ConnectionFactoryInterface;
|
||||
use Predis\Connection\NodeConnectionInterface;
|
||||
use Predis\Connection\Parameters;
|
||||
use Predis\Replication\ReplicationStrategy;
|
||||
use Predis\Response\ErrorInterface as ErrorResponseInterface;
|
||||
use Predis\Response\ServerException;
|
||||
@@ -48,6 +49,11 @@ class SentinelReplication extends MasterSlaveReplication
|
||||
*/
|
||||
protected $sentinelConnection;
|
||||
|
||||
/**
|
||||
* Timeout for the connection to a sentinel.
|
||||
*/
|
||||
protected $sentinelTimeout = 0.100;
|
||||
|
||||
/**
|
||||
* @param array $sentinels Sentinel servers connection parameters.
|
||||
* @param string $service Name of the service for autodiscovery.
|
||||
@@ -67,6 +73,19 @@ class SentinelReplication extends MasterSlaveReplication
|
||||
parent::__construct($strategy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a default timeout for connections to sentinels.
|
||||
*
|
||||
* When "timeout" is present in the connection parameters of sentinels, its
|
||||
* value overrides the default sentinel timeout.
|
||||
*
|
||||
* @param float $timeout Timeout value.
|
||||
*/
|
||||
public function setDefaultSentinelTimeout($timeout)
|
||||
{
|
||||
$this->sentinelTimeout = (float) $timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -88,6 +107,32 @@ class SentinelReplication extends MasterSlaveReplication
|
||||
$this->slaves = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the connection to a sentinel server.
|
||||
*
|
||||
* @return NodeConnectionInterface
|
||||
*/
|
||||
protected function createSentinelConnection($parameters)
|
||||
{
|
||||
if ($parameters instanceof NodeConnectionInterface) {
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
if (is_string($parameters)) {
|
||||
$parameters = Parameters::parse($parameters);
|
||||
}
|
||||
|
||||
if (is_array($parameters)) {
|
||||
$parameters += array(
|
||||
'timeout' => $this->sentinelTimeout,
|
||||
);
|
||||
}
|
||||
|
||||
$connection = $this->connectionFactory->create($parameters);
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current sentinel connection.
|
||||
*
|
||||
@@ -103,7 +148,7 @@ class SentinelReplication extends MasterSlaveReplication
|
||||
}
|
||||
|
||||
$sentinel = array_shift($this->sentinels);
|
||||
$this->sentinelConnection = $this->connectionFactory->create($sentinel);
|
||||
$this->sentinelConnection = $this->createSentinelConnection($sentinel);
|
||||
}
|
||||
|
||||
return $this->sentinelConnection;
|
||||
|
||||
Reference in New Issue
Block a user