Merge branch 'MDL-83753_404_STABLE' of https://github.com/djarran/moodle into MOODLE_404_STABLE
This commit is contained in:
+5
@@ -77,5 +77,10 @@ class cachestore_redis_addinstance_form extends cachestore_addinstance_form {
|
||||
$form->addHelpButton('compressor', 'usecompressor', 'cachestore_redis');
|
||||
$form->setDefault('compressor', cachestore_redis::COMPRESSOR_NONE);
|
||||
$form->setType('compressor', PARAM_INT);
|
||||
|
||||
$form->addElement('text', 'connectiontimeout', get_string('connectiontimeout', 'cachestore_redis'));
|
||||
$form->addHelpButton('connectiontimeout', 'connectiontimeout', 'cachestore_redis');
|
||||
$form->setDefault('connectiontimeout', cachestore_redis::CONNECTION_TIMEOUT);
|
||||
$form->setType('connectiontimeout', PARAM_INT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ $string['clustermodeunavailable'] = 'Redis Cluster is currently unavailable. Ple
|
||||
$string['compressor_none'] = 'No compression.';
|
||||
$string['compressor_php_gzip'] = 'Use gzip compression.';
|
||||
$string['compressor_php_zstd'] = 'Use Zstandard compression.';
|
||||
$string['connectiontimeout'] = 'Connection timeout';
|
||||
$string['connectiontimeout_help'] = 'This sets the timeout when attempting to connect to the Redis server.';
|
||||
$string['encrypt_connection'] = 'Use TLS encryption.';
|
||||
$string['encrypt_connection_help'] = 'Use TLS to connect to Redis. Do not use \'tls://\' in the hostname for Redis, use this option instead.';
|
||||
$string['password'] = 'Password';
|
||||
|
||||
Vendored
+20
-5
@@ -64,7 +64,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
const TTL_EXPIRE_BATCH = 10000;
|
||||
|
||||
/** @var int The number of seconds to wait for a connection or response from the Redis server. */
|
||||
const CONNECTION_TIMEOUT = 10;
|
||||
const CONNECTION_TIMEOUT = 3;
|
||||
|
||||
/**
|
||||
* Name of this store.
|
||||
@@ -115,6 +115,14 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
*/
|
||||
protected $compressor = self::COMPRESSOR_NONE;
|
||||
|
||||
|
||||
/**
|
||||
* The number of seconds to wait for a connection or response from the Redis server.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $connectiontimeout = self::CONNECTION_TIMEOUT;
|
||||
|
||||
/**
|
||||
* Bytes read or written by last call to set()/get() or set_many()/get_many().
|
||||
*
|
||||
@@ -195,6 +203,9 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
if (array_key_exists('compressor', $configuration)) {
|
||||
$this->compressor = (int)$configuration['compressor'];
|
||||
}
|
||||
if (array_key_exists('connectiontimeout', $configuration)) {
|
||||
$this->connectiontimeout = (int)$configuration['connectiontimeout'];
|
||||
}
|
||||
if (array_key_exists('lockwait', $configuration)) {
|
||||
$this->lockwait = (int)$configuration['lockwait'];
|
||||
}
|
||||
@@ -272,8 +283,8 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
$redis = new RedisCluster(
|
||||
null,
|
||||
$trimmedservers,
|
||||
self::CONNECTION_TIMEOUT, // Timeout.
|
||||
self::CONNECTION_TIMEOUT, // Read timeout.
|
||||
$this->connectiontimeout, // Timeout.
|
||||
$this->connectiontimeout, // Read timeout.
|
||||
true,
|
||||
$password,
|
||||
!empty($opts) ? $opts : null,
|
||||
@@ -283,10 +294,10 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
$redis->connect(
|
||||
$server,
|
||||
$port,
|
||||
self::CONNECTION_TIMEOUT, // Timeout.
|
||||
$this->connectiontimeout, // Timeout.
|
||||
null,
|
||||
100, // Retry interval.
|
||||
self::CONNECTION_TIMEOUT, // Read timeout.
|
||||
$this->connectiontimeout, // Read timeout.
|
||||
$opts,
|
||||
);
|
||||
if (!empty($password)) {
|
||||
@@ -834,6 +845,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
'password' => $data->password,
|
||||
'serializer' => $data->serializer,
|
||||
'compressor' => $data->compressor,
|
||||
'connectiontimeout' => $data->connectiontimeout,
|
||||
'encryption' => $data->encryption,
|
||||
'cafile' => $data->cafile,
|
||||
'clustermode' => $data->clustermode,
|
||||
@@ -858,6 +870,9 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
|
||||
if (!empty($config['compressor'])) {
|
||||
$data['compressor'] = $config['compressor'];
|
||||
}
|
||||
if (!empty($config['connectiontimeout'])) {
|
||||
$data['connectiontimeout'] = $config['connectiontimeout'];
|
||||
}
|
||||
if (!empty($config['encryption'])) {
|
||||
$data['encryption'] = $config['encryption'];
|
||||
}
|
||||
|
||||
@@ -362,6 +362,8 @@ $CFG->admin = 'admin';
|
||||
// $CFG->session_redis_lock_expire = 7200; // Optional, defaults to session timeout.
|
||||
// $CFG->session_redis_lock_retry = 100; // Optional wait between lock attempts in ms, default is 100.
|
||||
// // After 5 seconds it will throttle down to once per second.
|
||||
// $CFG->session_redis_connection_timeout = 3; // Optional, default is 3.
|
||||
// $CFG->session_redis_maxretries = 3; // Optional, default is 3.
|
||||
//
|
||||
// Use the igbinary serializer instead of the php default one. Note that phpredis must be compiled with
|
||||
// igbinary support to make the setting to work. Also, if you change the serializer you have to flush the database!
|
||||
|
||||
@@ -106,10 +106,10 @@ class redis extends handler implements SessionHandlerInterface {
|
||||
protected bool $clustermode = false;
|
||||
|
||||
/** @var int Maximum number of retries for cache store operations. */
|
||||
const MAX_RETRIES = 5;
|
||||
protected int $maxretries = 3;
|
||||
|
||||
/** @var int The number of seconds to wait for a connection or response from the Redis server. */
|
||||
const CONNECTION_TIMEOUT = 10;
|
||||
/** @var int $connectiontimeout The number of seconds to wait for a connection or response from the Redis server. */
|
||||
protected int $connectiontimeout = 3;
|
||||
|
||||
/**
|
||||
* Create new instance of handler.
|
||||
@@ -203,6 +203,14 @@ class redis extends handler implements SessionHandlerInterface {
|
||||
if (isset($CFG->session_redis_compressor)) {
|
||||
$this->compressor = $CFG->session_redis_compressor;
|
||||
}
|
||||
|
||||
if (isset($CFG->session_redis_connection_timeout)) {
|
||||
$this->connectiontimeout = (int)$CFG->session_redis_connection_timeout;
|
||||
}
|
||||
|
||||
if (isset($CFG->session_redis_max_retries)) {
|
||||
$this->maxretries = (int)$CFG->session_redis_max_retries;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,10 +290,10 @@ class redis extends handler implements SessionHandlerInterface {
|
||||
}
|
||||
}
|
||||
|
||||
// MDL-59866: Add retries for connections (up to 5 times) to make sure it goes through.
|
||||
// Add retries for connections to make sure it goes through.
|
||||
$counter = 1;
|
||||
$exceptionclass = $this->clustermode ? 'RedisClusterException' : 'RedisException';
|
||||
while ($counter <= self::MAX_RETRIES) {
|
||||
while ($counter <= $this->maxretries) {
|
||||
$this->connection = null;
|
||||
// Make a connection to Redis server(s).
|
||||
try {
|
||||
@@ -294,8 +302,8 @@ class redis extends handler implements SessionHandlerInterface {
|
||||
$this->connection = new \RedisCluster(
|
||||
null,
|
||||
$trimmedservers,
|
||||
self::CONNECTION_TIMEOUT, // Timeout.
|
||||
self::CONNECTION_TIMEOUT, // Read timeout.
|
||||
$this->connectiontimeout, // Timeout.
|
||||
$this->connectiontimeout, // Read timeout.
|
||||
true,
|
||||
$this->auth,
|
||||
!empty($opts) ? $opts : null,
|
||||
@@ -306,10 +314,10 @@ class redis extends handler implements SessionHandlerInterface {
|
||||
$this->connection->connect(
|
||||
$server,
|
||||
$port,
|
||||
self::CONNECTION_TIMEOUT, // Timeout.
|
||||
$this->connectiontimeout, // Timeout.
|
||||
null,
|
||||
$delay, // Retry interval.
|
||||
self::CONNECTION_TIMEOUT, // Read timeout.
|
||||
$this->connectiontimeout, // Read timeout.
|
||||
$opts,
|
||||
);
|
||||
if ($this->auth !== '' && !$this->connection->auth($this->auth)) {
|
||||
@@ -347,7 +355,7 @@ class redis extends handler implements SessionHandlerInterface {
|
||||
return true;
|
||||
} catch (RedisException | RedisClusterException $e) {
|
||||
$redishost = $this->clustermode ? implode(',', $this->host) : $server. ':'. $port;
|
||||
$logstring = "Failed to connect (try {$counter} out of " . self::MAX_RETRIES . ") to Redis ";
|
||||
$logstring = "Failed to connect (try {$counter} out of " . $this->maxretries . ") to Redis ";
|
||||
$logstring .= "at ". $redishost .", the error returned was: {$e->getMessage()}";
|
||||
debugging($logstring);
|
||||
}
|
||||
|
||||
@@ -349,8 +349,8 @@ final class session_redis_test extends \advanced_testcase {
|
||||
// Therefore, to get the host, we need to explode it.
|
||||
list($host, ) = explode(':', TEST_SESSION_REDIS_HOST);
|
||||
|
||||
$expected = "Failed to connect (try 5 out of 5) to Redis at $host:111111";
|
||||
$this->assertDebuggingCalledCount(5);
|
||||
$expected = "Failed to connect (try 3 out of 3) to Redis at $host:111111";
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
$this->assertStringContainsString($expected, $actual);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user