Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Properly support RedisCluster
Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
  • Loading branch information
skjnldsv authored and backportbot[bot] committed Jul 22, 2021
commit 8b17f317ee03b41dbdc27a8d40af12d10a430c12
4 changes: 2 additions & 2 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@
'user' => '', // Optional, if not defined no password will be used.
'password' => '', // Optional, if not defined no password will be used.
'dbindex' => 0, // Optional, if undefined SELECT will not run and will use Redis Server's default DB Index.
// If redis is encrypted, provide certificates
// If redis in-transit encryption is enabled, provide certificates
// SSL context https://www.php.net/manual/en/context.ssl.php
'ssl_context' => [
'local_cert' => '/certs/redis.crt',
Expand Down Expand Up @@ -1277,7 +1277,7 @@
'failover_mode' => \RedisCluster::FAILOVER_ERROR,
'user' => '', // Optional, if not defined no password will be used.
'password' => '', // Optional, if not defined no password will be used.
// If redis is encrypted, provide certificates
// If redis in-transit encryption is enabled, provide certificates
// SSL context https://www.php.net/manual/en/context.ssl.php
'ssl_context' => [
'local_cert' => '/certs/redis.crt',
Expand Down
38 changes: 22 additions & 16 deletions lib/private/RedisFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,29 @@ public function __construct(SystemConfig $config) {
}

private function create() {
$isCluster = !empty($this->config->getValue('redis.cluster', []));
$config = $this->config->getValue('redis', []);
$isCluster = in_array('redis.cluster', $this->config->getKeys());
$config = $isCluster
? $this->config->getValue('redis.cluster', [])
: $this->config->getValue('redis', []);

// Init cluster config if any
if ($isCluster) {
if (!class_exists('RedisCluster')) {
throw new \Exception('Redis Cluster support is not available');
}
// Replace config with the cluster config
$config = $this->config->getValue('redis.cluster', []);
if (empty($config)) {
throw new \Exception('Redis config is empty');
}

if ($isCluster && !class_exists('RedisCluster')) {
throw new \Exception('Redis Cluster support is not available');
}

if (isset($config['timeout'])) {
$timeout = $config['timeout'];
} else {
$timeout = null;
$timeout = 0.0;
}

if (isset($config['read_timeout'])) {
$readTimeout = $config['read_timeout'];
} else {
$readTimeout = null;
$readTimeout = 0.0;
}

$auth = null;
Expand All @@ -85,10 +86,11 @@ private function create() {

// cluster config
if ($isCluster) {
// Support for older phpredis versions not supporting connectionParameters
if ($connectionParameters !== null) {
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth, $connectionParameters);
} else {
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, $auth);
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth);
}

if (isset($config['failover_mode'])) {
Expand All @@ -111,13 +113,17 @@ private function create() {
$port = null;
}

if (!empty($connectionParameters)) {
// Support for older phpredis versions not supporting connectionParameters
if ($connectionParameters !== null) {
// Non-clustered redis requires connection parameters to be wrapped inside `stream`
$connectionParameters = [
'stream' => $this->getSslContext($config)
];
$this->instance->connect($host, $port, $timeout, null, 0, $readTimeout, $connectionParameters);
} else {
$this->instance->connect($host, $port, $timeout, null, 0, $readTimeout);
}

$this->instance->connect($host, $port, $timeout, null, 0, $readTimeout, $connectionParameters);

// Auth if configured
if ($auth !== null) {
Expand All @@ -134,7 +140,7 @@ private function create() {
* Get the ssl context config
*
* @param Array $config the current config
* @return Array
* @return Array|null
* @throws \UnexpectedValueException
*/
private function getSslContext($config) {
Expand All @@ -147,7 +153,7 @@ private function getSslContext($config) {
}
return $config['ssl_context'];
}
return [];
return null;
}

public function getInstance() {
Expand Down