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 2a28dbd commit f8b9035
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions cache/stores/redis/addinstanceform.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,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
33 changes: 24 additions & 9 deletions cache/stores/redis/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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().
*
Expand Down Expand Up @@ -197,6 +205,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 @@ -277,8 +288,8 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$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,
Expand All @@ -287,8 +298,8 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$redis = new RedisCluster(
null,
$trimmedservers,
self::CONNECTION_TIMEOUT,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
$this->connectiontimeout,
true, $password,
!empty($opts) ? $opts : null,
);
Expand All @@ -300,18 +311,18 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$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,
);
}
Expand Down Expand Up @@ -863,6 +874,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 @@ -887,6 +899,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 f8b9035

Please sign in to comment.