From 587fbbc44656178a29c84c99ebb1102d583a2398 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sun, 16 Aug 2015 15:24:53 +0200 Subject: [PATCH] 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. --- .../Aggregate/SentinelReplication.php | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Connection/Aggregate/SentinelReplication.php b/src/Connection/Aggregate/SentinelReplication.php index d951a81a..a4fa2c32 100644 --- a/src/Connection/Aggregate/SentinelReplication.php +++ b/src/Connection/Aggregate/SentinelReplication.php @@ -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;