Skip to content

Commit

Permalink
MDL-83753 cachestore_redis: Allow for configurable connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
djarran committed Dec 19, 2024
1 parent e05f74f commit 53b8dc8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cache/stores/redis/addinstanceform.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,10 @@ protected function configuration_definition() {
$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 changes: 2 additions & 0 deletions cache/stores/redis/lang/en/cachestore_redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
$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';
Expand Down
25 changes: 20 additions & 5 deletions cache/stores/redis/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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().
*
Expand Down Expand Up @@ -195,6 +203,9 @@ public function __construct(
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'];
}
Expand Down Expand Up @@ -272,8 +283,8 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$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,
Expand All @@ -283,10 +294,10 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$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)) {
Expand Down Expand Up @@ -834,6 +845,7 @@ public static function config_get_configuration_array($data) {
'password' => $data->password,
'serializer' => $data->serializer,
'compressor' => $data->compressor,
'connectiontimeout' => $data->connectiontimeout,
'encryption' => $data->encryption,
'cafile' => $data->cafile,
'clustermode' => $data->clustermode,
Expand All @@ -858,6 +870,9 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
if (!empty($config['compressor'])) {
$data['compressor'] = $config['compressor'];
}
if (!empty($config['connectiontimeout'])) {
$data['connectiontimeout'] = $config['connectiontimeout'];
}
if (!empty($config['encryption'])) {
$data['encryption'] = $config['encryption'];
}
Expand Down

0 comments on commit 53b8dc8

Please sign in to comment.