From 53b8dc8fb0215b8d9c33e4788e8bc05de01bdfb9 Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Wed, 18 Dec 2024 13:12:06 +1000 Subject: [PATCH] MDL-83753 cachestore_redis: Allow for configurable connection timeout --- cache/stores/redis/addinstanceform.php | 5 ++++ .../stores/redis/lang/en/cachestore_redis.php | 2 ++ cache/stores/redis/lib.php | 25 +++++++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cache/stores/redis/addinstanceform.php b/cache/stores/redis/addinstanceform.php index 770100c37658e..c9eaf368cbec4 100644 --- a/cache/stores/redis/addinstanceform.php +++ b/cache/stores/redis/addinstanceform.php @@ -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); } } diff --git a/cache/stores/redis/lang/en/cachestore_redis.php b/cache/stores/redis/lang/en/cachestore_redis.php index f7674800d7490..c3332562c8f91 100644 --- a/cache/stores/redis/lang/en/cachestore_redis.php +++ b/cache/stores/redis/lang/en/cachestore_redis.php @@ -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'; diff --git a/cache/stores/redis/lib.php b/cache/stores/redis/lib.php index b5b15c217a333..2ca865468b8d0 100644 --- a/cache/stores/redis/lib.php +++ b/cache/stores/redis/lib.php @@ -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 @@ 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']; } @@ -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, @@ -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)) { @@ -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, @@ -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']; }