From 92621453b8715c05d3ecbb394dbf583a9e3df56f Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Wed, 18 Dec 2024 10:10:49 +1000 Subject: [PATCH] MDL-83753 redis: Allow for configurable max retries setting --- config-dist.php | 1 + lib/classes/session/redis.php | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/config-dist.php b/config-dist.php index dc54678a8298..130590416535 100644 --- a/config-dist.php +++ b/config-dist.php @@ -371,6 +371,7 @@ // $CFG->session_redis_lock_retry = 100; // Optional wait between lock attempts in ms, default is 100. // // After 5 seconds it will throttle down to once per second. // $CFG->session_redis_connection_timeout = 3; // Optional wait for connection or response from Redis server. +// $CFG->session_redis_maxretries = 3; // Optional number of retries when connecting to Redis server. // // Use the igbinary serializer instead of the php default one. Note that phpredis must be compiled with // igbinary support to make the setting to work. Also, if you change the serializer you have to flush the database! diff --git a/lib/classes/session/redis.php b/lib/classes/session/redis.php index 3511e2ee4568..8f366e99a034 100644 --- a/lib/classes/session/redis.php +++ b/lib/classes/session/redis.php @@ -106,7 +106,7 @@ class redis extends handler implements SessionHandlerInterface { protected bool $clustermode = false; /** @var int Maximum number of retries for cache store operations. */ - const MAX_RETRIES = 5; + protected int $maxretries = 5; /** @var int $firstaccesstimeout The initial timeout (seconds) for the first browser access without login. */ protected int $firstaccesstimeout = 180; @@ -208,6 +208,10 @@ public function __construct() { $this->connectiontimeout = (int)$CFG->session_redis_connection_timeout; } + if (isset($CFG->session_redis_max_retries)) { + $this->maxretries = (int)$CFG->session_redis_max_retries; + } + $this->clock = di::get(clock::class); } @@ -285,7 +289,7 @@ public function init(): bool { // MDL-59866: Add retries for connections (up to 5 times) to make sure it goes through. $counter = 1; $exceptionclass = $this->clustermode ? 'RedisClusterException' : 'RedisException'; - while ($counter <= self::MAX_RETRIES) { + while ($counter <= $this->maxretries) { $this->connection = null; // Make a connection to Redis server(s). try { @@ -387,7 +391,7 @@ public function init(): bool { return true; } catch (RedisException | RedisClusterException $e) { $redishost = $this->clustermode ? implode(',', $this->host) : $server . ':' . $port; - $logstring = "Failed to connect (try {$counter} out of " . self::MAX_RETRIES . ") to Redis "; + $logstring = "Failed to connect (try {$counter} out of " . $this->maxretries . ") to Redis "; $logstring .= "at ". $redishost .", the error returned was: {$e->getMessage()}"; debugging($logstring); }