From 81ac90b560b226f9c811423b6475e5b2858ace7a Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Tue, 17 Dec 2024 13:46:38 +1000 Subject: [PATCH] MDL-83753 redis: Set connection and read timeouts to configurable value --- config-dist.php | 1 + lib/classes/session/redis.php | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/config-dist.php b/config-dist.php index bdbda3dd47332..dc54678a82985 100644 --- a/config-dist.php +++ b/config-dist.php @@ -370,6 +370,7 @@ // $CFG->session_redis_lock_expire = 7200; // Optional, defaults to session timeout. // $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. // // 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 8479859e23277..1eee8a2d53cc2 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; + const MAX_RETRIES = 3; /** @var int $firstaccesstimeout The initial timeout (seconds) for the first browser access without login. */ protected int $firstaccesstimeout = 180; @@ -114,8 +114,8 @@ class redis extends handler implements SessionHandlerInterface { /** @var clock A clock instance */ protected clock $clock; - /** @var int The number of seconds to wait for a connection or response from the Redis server. */ - const CONNECTION_TIMEOUT = 10; + /** @var int $connectiontimeout The number of seconds to wait for a connection or response from the Redis server. */ + protected int $connectiontimeout = 3; /** * Create new instance of handler. @@ -204,6 +204,10 @@ public function __construct() { $this->compressor = $CFG->session_redis_compressor; } + if (isset($CFG->session_redis_connection_timeout)) { + $this->connectiontimeout = (int)$CFG->session_redis_connection_timeout; + } + $this->clock = di::get(clock::class); } @@ -293,8 +297,8 @@ public function init(): bool { $this->connection = 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: $this->auth, context: !empty($opts) ? $opts : null, @@ -303,8 +307,8 @@ public function init(): bool { $this->connection = new \RedisCluster( null, $trimmedservers, - self::CONNECTION_TIMEOUT, - self::CONNECTION_TIMEOUT, + $this->connectiontimeout, + $this->connectiontimeout, true, $this->auth, !empty($opts) ? $opts : null @@ -318,19 +322,19 @@ public function init(): bool { $this->connection->connect( host: $server, port: $port, - timeout: self::CONNECTION_TIMEOUT, // Timeout. + timeout: $this->connectiontimeout, // Timeout. retry_interval: $delay, - read_timeout: self::CONNECTION_TIMEOUT, // Read timeout. + read_timeout: $this->connectiontimeout, // Read timeout. context: $opts, ); } else { $this->connection->connect( $server, $port, - self::CONNECTION_TIMEOUT, + $this->connectiontimeout, null, $delay, - self::CONNECTION_TIMEOUT, + $this->connectiontimeout, $opts ); }