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 authored and ostrolucky committed Jan 29, 2023
1 parent dd0a737 commit e5bd109
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -35,7 +35,7 @@
"doctrine/coding-standard": "^10.0",
"friendsofphp/proxy-manager-lts": "^1.0.6",
"monolog/monolog": "*",
"phpunit/phpunit": "^8.5 || ^9.5",
"phpunit/phpunit": "^8.5.32 || ^9.5.28",
"predis/predis": "^2.0",
"symfony/browser-kit": "^4.4 || ^5.3 || ^6.0",
"symfony/cache": "^4.4 || ^5.3 || ^6.0",
Expand Down
31 changes: 24 additions & 7 deletions 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 Down Expand Up @@ -102,19 +104,34 @@ public function create(string $class, array $dsns, array $options, string $alias
}

/**
* @param class-string $class
* @param list<RedisDsn> $dsns
* @param array{service: ?string} $options
* @param class-string $class
* @param list<RedisDsn> $dsns
* @param array{service: ?string, connection_persistent: ?bool, connection_timeout: ?string, read_write_timeout: ?string} $options
*
* @return Redis|Relay
*/
private function createClientFromSentinel(string $class, array $dsns, string $alias, array $options, bool $loggingEnabled)
{
$isRelay = is_a($class, Sentinel::class, true);
$sentinelClass = $isRelay ? Sentinel::class : RedisSentinel::class;
$isRelay = is_a($class, Sentinel::class, true);
$sentinelClass = $isRelay ? Sentinel::class : RedisSentinel::class;
$masterName = $options['service'];
$connectionTimeout = $options['connection_timeout'] ?? 0;
$connectionPersistent = $options['connection_persistent'] ? $masterName : null;
$readTimeout = $options['read_write_timeout'] ?? 0;

foreach ($dsns as $dsn) {
$address = (new $sentinelClass($dsn->getHost(), (int) $dsn->getPort()))->getMasterAddrByName($options['service']);
try {
$address = (new $sentinelClass(
$dsn->getHost(),
(int) $dsn->getPort(),
$connectionTimeout,
$connectionPersistent,
5, // retry interval
$readTimeout,
))->getMasterAddrByName($masterName);
} catch (RedisException | RelayException $e) {
continue;
}

if (!$address) {
continue;
Expand All @@ -139,7 +156,7 @@ public function __construct(string $dsn, string $host, int $port)
throw new InvalidArgumentException(
sprintf(
'Failed to retrieve master information from sentinel %s and dsn %s.',
var_export($options['service'], true),
var_export($masterName, true),
var_export($dsns, true),
),
);
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 e5bd109

Please sign in to comment.