MDL-83753 cachestore_redis: Allow for configurable connection timeout

This commit is contained in:
djarrancotleanu
2024-12-19 14:21:59 +10:00
parent 2a28dbdf78
commit f8b903596a
3 changed files with 31 additions and 9 deletions
+5
View File
@@ -65,5 +65,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);
}
}
+2
View File
@@ -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';
+24 -9
View File
@@ -66,7 +66,7 @@ class cachestore_redis extends store implements
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.
@@ -117,6 +117,14 @@ class cachestore_redis extends store implements
*/
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().
*
@@ -197,6 +205,9 @@ class cachestore_redis extends store implements
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'];
}
@@ -277,8 +288,8 @@ class cachestore_redis extends store implements
$redis = new RedisCluster(
name: null,
seeds: $trimmedservers,
timeout: self::CONNECTION_TIMEOUT, // Timeout.
read_timeout: self::CONNECTION_TIMEOUT, // Read timeout.
timeout: $this->connectiontimeout, // Timeout.
read_timeout: $this->connectiontimeout, // Read timeout.
persistent: true,
auth: $password,
context: !empty($opts) ? $opts : null,
@@ -287,8 +298,8 @@ class cachestore_redis extends store implements
$redis = new RedisCluster(
null,
$trimmedservers,
self::CONNECTION_TIMEOUT,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
$this->connectiontimeout,
true, $password,
!empty($opts) ? $opts : null,
);
@@ -300,18 +311,18 @@ class cachestore_redis extends store implements
$redis->connect(
host: $server,
port: $port,
timeout: self::CONNECTION_TIMEOUT, // Timeout.
timeout: $this->connectiontimeout, // Timeout.
retry_interval: 100, // Retry interval.
read_timeout: self::CONNECTION_TIMEOUT, // Read timeout.
read_timeout: $this->connectiontimeout, // Read timeout.
context: $opts,
);
} else {
$redis->connect(
$server, $port,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
null,
100,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
$opts,
);
}
@@ -863,6 +874,7 @@ class cachestore_redis extends store implements
'password' => $data->password,
'serializer' => $data->serializer,
'compressor' => $data->compressor,
'connectiontimeout' => $data->connectiontimeout,
'encryption' => $data->encryption,
'cafile' => $data->cafile,
'clustermode' => $data->clustermode,
@@ -887,6 +899,9 @@ class cachestore_redis extends store implements
if (!empty($config['compressor'])) {
$data['compressor'] = $config['compressor'];
}
if (!empty($config['connectiontimeout'])) {
$data['connectiontimeout'] = $config['connectiontimeout'];
}
if (!empty($config['encryption'])) {
$data['encryption'] = $config['encryption'];
}