Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support injecting additional options for php/relay sentinel #695

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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