Skip to content

Commit

Permalink
Support injecting additional options for php/relay sentinel
Browse files Browse the repository at this point in the history
  • Loading branch information
rguennichi committed Jan 29, 2023
1 parent dd0a737 commit b63bc3e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/Factory/PhpredisClientFactory.php
Expand Up @@ -11,9 +11,11 @@
use ProxyManager\Proxy\AccessInterceptorInterface;
use Redis;
use RedisCluster;
use RedisException;
use RedisSentinel;
use ReflectionClass;
use ReflectionMethod;
use Relay\Exception as RelayException;
use Relay\Relay;
use Relay\Sentinel;
use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn;
Expand All @@ -28,6 +30,7 @@
use function in_array;
use function is_a;
use function is_array;
use function shuffle;
use function spl_autoload_register;
use function sprintf;
use function var_export;
Expand Down Expand Up @@ -113,8 +116,21 @@ private function createClientFromSentinel(string $class, array $dsns, string $al
$isRelay = is_a($class, Sentinel::class, true);
$sentinelClass = $isRelay ? Sentinel::class : RedisSentinel::class;

shuffle($dsns);

foreach ($dsns as $dsn) {
$address = (new $sentinelClass($dsn->getHost(), (int) $dsn->getPort()))->getMasterAddrByName($options['service']);
try {
$address = (new $sentinelClass(
$dsn->getHost(),
(int) $dsn->getPort(),
$options['connection_timeout'] ?? 0,
$options['connection_persistent'] ? $options['service'] : null,
5, // retry interval
$options['read_write_timeout'] ?? 0,
))->getMasterAddrByName($options['service']);
} catch (RedisException | RelayException $e) {
continue;
}

if (!$address) {
continue;
Expand Down
12 changes: 10 additions & 2 deletions tests/Factory/PhpredisClientFactoryTest.php
Expand Up @@ -115,15 +115,23 @@ public function testCreateSentinelConfig(string $sentinelClass, string $outputCl

$client = $factory->create(
$sentinelClass,
['redis://sncredis@localhost:26379'],
['connection_timeout' => 5, 'connection_persistent' => false, 'service' => 'mymaster'],
[
'redis://undefined@localhost:55555', // unreachable instance
'redis://sncredis@localhost:26379',
],
[
'connection_timeout' => 5,
'connection_persistent' => false,
'service' => 'mymaster',
],
'phpredissentinel',
true,
);

$this->assertInstanceOf($outputClass, $client);
$this->assertNull($client->getOption(Redis::OPT_PREFIX));
$this->assertSame(0, $client->getOption(Redis::OPT_SERIALIZER));
$this->assertSame(5., $client->getTimeout());
$this->assertSame('sncredis', $client->getAuth());
}

Expand Down

0 comments on commit b63bc3e

Please sign in to comment.