From b63bc3e0f371e4640c50bbd5d7c2243eb83f0d7d Mon Sep 17 00:00:00 2001 From: Radhi Guennichi Date: Sun, 29 Jan 2023 03:27:27 +0100 Subject: [PATCH] Support injecting additional options for php/relay sentinel --- src/Factory/PhpredisClientFactory.php | 18 +++++++++++++++++- tests/Factory/PhpredisClientFactoryTest.php | 12 ++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Factory/PhpredisClientFactory.php b/src/Factory/PhpredisClientFactory.php index a17e8f5f..308afef1 100644 --- a/src/Factory/PhpredisClientFactory.php +++ b/src/Factory/PhpredisClientFactory.php @@ -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; @@ -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; @@ -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; diff --git a/tests/Factory/PhpredisClientFactoryTest.php b/tests/Factory/PhpredisClientFactoryTest.php index 8f90e1fd..aacf377c 100644 --- a/tests/Factory/PhpredisClientFactoryTest.php +++ b/tests/Factory/PhpredisClientFactoryTest.php @@ -115,8 +115,15 @@ 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, ); @@ -124,6 +131,7 @@ public function testCreateSentinelConfig(string $sentinelClass, string $outputCl $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()); }