diff --git a/cache/stores/redis/addinstanceform.php b/cache/stores/redis/addinstanceform.php index 6fa64c68a5f56..e775bbcd0db10 100644 --- a/cache/stores/redis/addinstanceform.php +++ b/cache/stores/redis/addinstanceform.php @@ -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); } } 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 4d3d1a622f9a7..170f04fc6b9b5 100644 --- a/cache/stores/redis/lib.php +++ b/cache/stores/redis/lib.php @@ -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 @@ 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']; } @@ -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, @@ -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, ); @@ -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, ); } @@ -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, @@ -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']; }