From 70df7ca64d3ac4b70f144ac043158fd05fa847b6 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sun, 8 Dec 2013 11:01:36 +0100 Subject: [PATCH] Apply common parameters to connections created on the fly in cluster. These parameters are applied only to connections being created on the fly when not part of the current pool. This condition usually happens upon -MOVE or -ASK responses returned by Redis to redirect client to different nodes. --- lib/Predis/Connection/RedisCluster.php | 27 +++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/Predis/Connection/RedisCluster.php b/lib/Predis/Connection/RedisCluster.php index a301b93b..d0b8aef1 100644 --- a/lib/Predis/Connection/RedisCluster.php +++ b/lib/Predis/Connection/RedisCluster.php @@ -47,6 +47,7 @@ use Predis\Response; class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Countable { private $askClusterNodes = false; + private $defaultParameters = array(); private $pool = array(); private $slots = array(); private $slotsMap; @@ -298,11 +299,12 @@ class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Cou if (!$connection = $this->getConnectionById($connectionID)) { $host = explode(':', $connectionID, 2); - $connection = $this->connections->create(array( + $parameters = array_merge($this->defaultParameters, array( 'host' => $host[0], 'port' => $host[1], )); + $connection = $this->connections->create($parameters); $this->pool[$connectionID] = $connection; } @@ -381,11 +383,12 @@ class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Cou if (!$connection) { $host = explode(':', $host, 2); - - $connection = $this->connections->create(array( + $parameters = array_merge($this->defaultParameters, array( 'host' => $host[0], 'port' => $host[1], )); + + $connection = $this->connections->create($parameters); } switch ($request) { @@ -484,4 +487,22 @@ class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Cou { $this->askClusterNodes = (bool) $value; } + + /** + * Sets a default array of connection parameters to be applied when creating + * new connection instances on the fly when they are not part of the initial + * pool supplied upon cluster initialization. + * + * These parameters are not applied to connections added to the pool using + * the add() method. + * + * @param array $parameters Array of connection parameters. + */ + public function setDefaultParameters(array $parameters) + { + $this->defaultParameters = array_merge( + $this->defaultParameters, + $parameters ?: array() + ); + } }