From 1463c89a36931031a60e591ab5221e7b5dee840b Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 12 Jun 2018 07:35:58 +0200 Subject: [PATCH 1/4] timeout --- README.md | 6 ++++++ src/Sentinel.php | 5 +++-- src/SentinelPool.php | 5 +++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 44162eb..63347f2 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,9 @@ $info = $redis->info(); print_r($info); ``` +In order to prevent redis/sentinel to wait too long for connections in case of +issues with the Redis backend it's advisable to use a timeout (in seconds): + +```php +$sentinel_pool->addSentinel('127.0.0.1', 26380, 1.0); # 1 second timeout +``` \ No newline at end of file diff --git a/src/Sentinel.php b/src/Sentinel.php index d2687e6..4c45d11 100644 --- a/src/Sentinel.php +++ b/src/Sentinel.php @@ -31,11 +31,12 @@ public function __destruct() /** * @param $host * @param int $port + * @param float $timeout connect timeout in seconds * @return boolean */ - public function connect($host, $port = 26379) + public function connect($host, $port = 26379, $timeout = 0.0) { - if (!$this->redis->connect($host, $port)) { + if (!$this->redis->connect($host, $port, $timeout)) { return false; } diff --git a/src/SentinelPool.php b/src/SentinelPool.php index 4672a8d..a27bdcd 100644 --- a/src/SentinelPool.php +++ b/src/SentinelPool.php @@ -51,13 +51,14 @@ public function __construct(array $sentinels = array()) * * @param string $host sentinel server host * @param int $port sentinel server port + * @param float $timeout connect timeout in seconds * @return bool */ - public function addSentinel($host, $port) + public function addSentinel($host, $port, $timeout = 0.0) { $sentinel = new Sentinel(); // if connect to sentinel successfully, add it to sentinels array - if ($sentinel->connect($host, $port)) { + if ($sentinel->connect($host, $port, $timeout)) { $this->sentinels[] = $sentinel; return true; } From cb3c92f6c53442490e65548823b2bbf6d3002a52 Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 12 Jun 2018 07:41:20 +0200 Subject: [PATCH 2/4] same timeout for redis connect --- src/Sentinel.php | 20 +++++++++++++++++--- src/SentinelPool.php | 14 ++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Sentinel.php b/src/Sentinel.php index 4c45d11..29da468 100644 --- a/src/Sentinel.php +++ b/src/Sentinel.php @@ -15,9 +15,15 @@ class Sentinel */ protected $redis; - public function __construct() + /** + * @var float connect timeout in seconds + */ + protected $timeout; + + public function __construct($timeout = null) { $this->redis = new \Redis(); + $this->timeout = $timeout === null ? 0.0 : $timeout; } public function __destruct() @@ -34,9 +40,9 @@ public function __destruct() * @param float $timeout connect timeout in seconds * @return boolean */ - public function connect($host, $port = 26379, $timeout = 0.0) + public function connect($host, $port = 26379, $timeout = null) { - if (!$this->redis->connect($host, $port, $timeout)) { + if (!$this->redis->connect($host, $port, $timeout === null ? $this->timeout : $timeout)) { return false; } @@ -283,4 +289,12 @@ public function info() return $this->redis->info(); } + /** + * @return float + */ + public function getTimeout() + { + return $this->timeout; + } + } diff --git a/src/SentinelPool.php b/src/SentinelPool.php index a27bdcd..54d3bfb 100644 --- a/src/SentinelPool.php +++ b/src/SentinelPool.php @@ -35,6 +35,11 @@ class SentinelPool */ protected $sentinels = array(); + /** + * @var Sentinel + */ + protected $currentSentinel = null; + /** * SentinelPool constructor. * @param array $sentinels [['host'=>'host', 'port'=>'port']] @@ -51,12 +56,12 @@ public function __construct(array $sentinels = array()) * * @param string $host sentinel server host * @param int $port sentinel server port - * @param float $timeout connect timeout in seconds + * @param null|float $timeout connect timeout in seconds * @return bool */ - public function addSentinel($host, $port, $timeout = 0.0) + public function addSentinel($host, $port, $timeout = null) { - $sentinel = new Sentinel(); + $sentinel = new Sentinel($timeout); // if connect to sentinel successfully, add it to sentinels array if ($sentinel->connect($host, $port, $timeout)) { $this->sentinels[] = $sentinel; @@ -77,7 +82,7 @@ public function getRedis($master_name) { $address = $this->getMasterAddrByName($master_name); $redis = new \Redis(); - if (!$redis->connect($address['ip'], $address['port'])) { + if (!$redis->connect($address['ip'], $address['port'], $this->currentSentinel->getTimeout())) { throw new \RedisException("connect to redis failed"); } @@ -91,6 +96,7 @@ public function __call($name, $arguments) throw new \BadMethodCallException("method not exists. method: {$name}"); } try { + $this->currentSentinel = $sentinel; return call_user_func_array(array($sentinel, $name), $arguments); } catch (\Exception $e) { continue; From 928d24cce6e2ba04270f33848ead72d0c0aad89d Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 12 Jun 2018 07:48:12 +0200 Subject: [PATCH 3/4] timeout test --- src/SentinelPool.php | 8 ++++++++ tests/SentinelPoolTest.php | 4 ++-- tests/SentinelTimeoutTest.php | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/SentinelTimeoutTest.php diff --git a/src/SentinelPool.php b/src/SentinelPool.php index 54d3bfb..8cc8afc 100644 --- a/src/SentinelPool.php +++ b/src/SentinelPool.php @@ -105,4 +105,12 @@ public function __call($name, $arguments) throw new SentinelClientNotConnectException("all sentinel failed"); } + + /** + * @return Sentinel + */ + public function getCurrentSentinel() + { + return $this->currentSentinel; + } } \ No newline at end of file diff --git a/tests/SentinelPoolTest.php b/tests/SentinelPoolTest.php index 07ccb51..2aa78cf 100644 --- a/tests/SentinelPoolTest.php +++ b/tests/SentinelPoolTest.php @@ -15,8 +15,8 @@ class SentinelPoolTest extends \PHPUnit_Framework_TestCase /** * @var SentinelPool */ - private $sentinel_pool; - private $master_name = 'mymaster'; + protected $sentinel_pool; + protected $master_name = 'mymaster'; public function setUp() { diff --git a/tests/SentinelTimeoutTest.php b/tests/SentinelTimeoutTest.php new file mode 100644 index 0000000..31dffa7 --- /dev/null +++ b/tests/SentinelTimeoutTest.php @@ -0,0 +1,26 @@ +sentinel_pool = new SentinelPool(); + $this->sentinel_pool->addSentinel('127.0.0.1', 26379, 1.1); + $this->sentinel_pool->addSentinel('127.0.0.1', 26380, 1.2); + $this->sentinel_pool->addSentinel('127.0.0.1', 26381, 1.3); + } + + public function testTimeout() + { + // run command to populate current sentinel + $this->assertEquals('+PONG', $this->sentinel_pool->ping()); + + $this->assertNotNull($this->sentinel_pool->getCurrentSentinel()); + + $this->assertEquals(1.1, $this->sentinel_pool->getCurrentSentinel()->getTimeout()); + } +} \ No newline at end of file From 86971fe560165e915ebf913d72e7666563edc37b Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 12 Jun 2018 07:50:29 +0200 Subject: [PATCH 4/4] php 5.3 not supported anymore by travis in current setup --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 93c9642..e2e248e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.3 - 5.4 - 5.5 - 5.6