Skip to content

Commit e5bd109

Browse files
rguennichiostrolucky
authored andcommitted
Support injecting additional options for php/relay sentinel
1 parent dd0a737 commit e5bd109

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"doctrine/coding-standard": "^10.0",
3636
"friendsofphp/proxy-manager-lts": "^1.0.6",
3737
"monolog/monolog": "*",
38-
"phpunit/phpunit": "^8.5 || ^9.5",
38+
"phpunit/phpunit": "^8.5.32 || ^9.5.28",
3939
"predis/predis": "^2.0",
4040
"symfony/browser-kit": "^4.4 || ^5.3 || ^6.0",
4141
"symfony/cache": "^4.4 || ^5.3 || ^6.0",

src/Factory/PhpredisClientFactory.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use ProxyManager\Proxy\AccessInterceptorInterface;
1212
use Redis;
1313
use RedisCluster;
14+
use RedisException;
1415
use RedisSentinel;
1516
use ReflectionClass;
1617
use ReflectionMethod;
18+
use Relay\Exception as RelayException;
1719
use Relay\Relay;
1820
use Relay\Sentinel;
1921
use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn;
@@ -102,19 +104,34 @@ public function create(string $class, array $dsns, array $options, string $alias
102104
}
103105

104106
/**
105-
* @param class-string $class
106-
* @param list<RedisDsn> $dsns
107-
* @param array{service: ?string} $options
107+
* @param class-string $class
108+
* @param list<RedisDsn> $dsns
109+
* @param array{service: ?string, connection_persistent: ?bool, connection_timeout: ?string, read_write_timeout: ?string} $options
108110
*
109111
* @return Redis|Relay
110112
*/
111113
private function createClientFromSentinel(string $class, array $dsns, string $alias, array $options, bool $loggingEnabled)
112114
{
113-
$isRelay = is_a($class, Sentinel::class, true);
114-
$sentinelClass = $isRelay ? Sentinel::class : RedisSentinel::class;
115+
$isRelay = is_a($class, Sentinel::class, true);
116+
$sentinelClass = $isRelay ? Sentinel::class : RedisSentinel::class;
117+
$masterName = $options['service'];
118+
$connectionTimeout = $options['connection_timeout'] ?? 0;
119+
$connectionPersistent = $options['connection_persistent'] ? $masterName : null;
120+
$readTimeout = $options['read_write_timeout'] ?? 0;
115121

116122
foreach ($dsns as $dsn) {
117-
$address = (new $sentinelClass($dsn->getHost(), (int) $dsn->getPort()))->getMasterAddrByName($options['service']);
123+
try {
124+
$address = (new $sentinelClass(
125+
$dsn->getHost(),
126+
(int) $dsn->getPort(),
127+
$connectionTimeout,
128+
$connectionPersistent,
129+
5, // retry interval
130+
$readTimeout,
131+
))->getMasterAddrByName($masterName);
132+
} catch (RedisException | RelayException $e) {
133+
continue;
134+
}
118135

119136
if (!$address) {
120137
continue;
@@ -139,7 +156,7 @@ public function __construct(string $dsn, string $host, int $port)
139156
throw new InvalidArgumentException(
140157
sprintf(
141158
'Failed to retrieve master information from sentinel %s and dsn %s.',
142-
var_export($options['service'], true),
159+
var_export($masterName, true),
143160
var_export($dsns, true),
144161
),
145162
);

tests/Factory/PhpredisClientFactoryTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,23 @@ public function testCreateSentinelConfig(string $sentinelClass, string $outputCl
115115

116116
$client = $factory->create(
117117
$sentinelClass,
118-
['redis://sncredis@localhost:26379'],
119-
['connection_timeout' => 5, 'connection_persistent' => false, 'service' => 'mymaster'],
118+
[
119+
'redis://undefined@localhost:55555', // unreachable instance
120+
'redis://sncredis@localhost:26379',
121+
],
122+
[
123+
'connection_timeout' => 5,
124+
'connection_persistent' => false,
125+
'service' => 'mymaster',
126+
],
120127
'phpredissentinel',
121128
true,
122129
);
123130

124131
$this->assertInstanceOf($outputClass, $client);
125132
$this->assertNull($client->getOption(Redis::OPT_PREFIX));
126133
$this->assertSame(0, $client->getOption(Redis::OPT_SERIALIZER));
134+
$this->assertSame(5., $client->getTimeout());
127135
$this->assertSame('sncredis', $client->getAuth());
128136
}
129137

0 commit comments

Comments
 (0)