From 209072abd9877da57a7df6056b7f66b8e5533ccf Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Tue, 17 Dec 2024 14:09:02 +1000 Subject: [PATCH] MDL-83753 cachestore_redis: Allow for configurable connection timeout setting --- cache/stores/redis/lib.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/cache/stores/redis/lib.php b/cache/stores/redis/lib.php index 4d3d1a622f9a7..e1cdf7a57b713 100644 --- a/cache/stores/redis/lib.php +++ b/cache/stores/redis/lib.php @@ -65,9 +65,6 @@ 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; - /** * Name of this store. * @@ -133,6 +130,13 @@ class cachestore_redis extends store implements /** @var ?array Array of current locks, or null if we haven't registered shutdown function */ protected $currentlocks = null; + /** + * The number of seconds to wait for a connection or response from the Redis server. + * + * @var int + */ + protected $connectiontimeout = 3; + /** * Determines if the requirements for this type of store are met. * @@ -203,6 +207,9 @@ public function __construct( if (array_key_exists('locktimeout', $configuration)) { $this->locktimeout = (int)$configuration['locktimeout']; } + if ($connectiontimeout = get_config('cachestore_redis', 'connectiontimeout')) { + $this->connectiontimeout = $connectiontimeout; + } $this->redis = $this->new_redis($configuration); } @@ -277,8 +284,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 +294,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 +307,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, ); }