Skip to content

Commit

Permalink
Merge pull request #1 from RobinUS2/master
Browse files Browse the repository at this point in the history
Timeout
  • Loading branch information
white-poto authored Oct 27, 2020
2 parents e49fc30 + 86971fe commit 2818ca8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
21 changes: 18 additions & 3 deletions src/Sentinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -31,11 +37,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 = null)
{
if (!$this->redis->connect($host, $port)) {
if (!$this->redis->connect($host, $port, $timeout === null ? $this->timeout : $timeout)) {
return false;
}

Expand Down Expand Up @@ -282,4 +289,12 @@ public function info()
return $this->redis->info();
}

/**
* @return float
*/
public function getTimeout()
{
return $this->timeout;
}

}
23 changes: 19 additions & 4 deletions src/SentinelPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class SentinelPool
*/
protected $sentinels = array();

/**
* @var Sentinel
*/
protected $currentSentinel = null;

/**
* SentinelPool constructor.
* @param array $sentinels [['host'=>'host', 'port'=>'port']]
Expand All @@ -51,13 +56,14 @@ public function __construct(array $sentinels = array())
*
* @param string $host sentinel server host
* @param int $port sentinel server port
* @param null|float $timeout connect timeout in seconds
* @return bool
*/
public function addSentinel($host, $port)
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)) {
if ($sentinel->connect($host, $port, $timeout)) {
$this->sentinels[] = $sentinel;
return true;
}
Expand All @@ -76,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");
}

Expand All @@ -90,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;
Expand All @@ -98,4 +105,12 @@ public function __call($name, $arguments)

throw new SentinelClientNotConnectException("all sentinel failed");
}

/**
* @return Sentinel
*/
public function getCurrentSentinel()
{
return $this->currentSentinel;
}
}
4 changes: 2 additions & 2 deletions tests/SentinelPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
26 changes: 26 additions & 0 deletions tests/SentinelTimeoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Jenner\RedisSentinel\Test;

use Jenner\RedisSentinel\SentinelPool;

class SentinelTimeoutTest extends SentinelPoolTest
{
public function setUp()
{
parent::setUp();
$this->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());
}
}

0 comments on commit 2818ca8

Please sign in to comment.