diff --git a/config-dist.php b/config-dist.php index 7f4c86ecc5f..06040c0d423 100644 --- a/config-dist.php +++ b/config-dist.php @@ -363,6 +363,7 @@ $CFG->admin = 'admin'; // $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, default is 3. +// $CFG->session_redis_maxretries = 3; // Optional, default is 3. // // 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 0d9fbaf214e..d168d4f9fac 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 = 3; /** @var int $connectiontimeout The number of seconds to wait for a connection or response from the Redis server. */ protected int $connectiontimeout = 3; @@ -207,6 +207,10 @@ class redis extends handler implements SessionHandlerInterface { if (isset($CFG->session_redis_connection_timeout)) { $this->connectiontimeout = (int)$CFG->session_redis_connection_timeout; } + + if (isset($CFG->session_redis_max_retries)) { + $this->maxretries = (int)$CFG->session_redis_max_retries; + } } /** @@ -286,10 +290,10 @@ class redis extends handler implements SessionHandlerInterface { } } - // MDL-59866: Add retries for connections (up to 5 times) to make sure it goes through. + // Add retries for connections 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 { @@ -351,7 +355,7 @@ class redis extends handler implements SessionHandlerInterface { 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); } diff --git a/lib/tests/session_redis_test.php b/lib/tests/session_redis_test.php index 917b23ee733..ad1d3bbb148 100644 --- a/lib/tests/session_redis_test.php +++ b/lib/tests/session_redis_test.php @@ -349,8 +349,8 @@ final class session_redis_test extends \advanced_testcase { // Therefore, to get the host, we need to explode it. list($host, ) = explode(':', TEST_SESSION_REDIS_HOST); - $expected = "Failed to connect (try 5 out of 5) to Redis at $host:111111"; - $this->assertDebuggingCalledCount(5); + $expected = "Failed to connect (try 3 out of 3) to Redis at $host:111111"; + $this->assertDebuggingCalledCount(3); $this->assertStringContainsString($expected, $actual); }