diff --git a/cache/stores/redis/lib.php b/cache/stores/redis/lib.php index 7e3145324da..b5b15c217a3 100644 --- a/cache/stores/redis/lib.php +++ b/cache/stores/redis/lib.php @@ -63,6 +63,9 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_ */ 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. * @@ -266,10 +269,26 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_ try { // Create a $redis object of a RedisCluster or Redis class. if ($clustermode) { - $redis = new RedisCluster(null, $trimmedservers, 1, 1, true, $password, !empty($opts) ? $opts : null); + $redis = new RedisCluster( + null, + $trimmedservers, + self::CONNECTION_TIMEOUT, // Timeout. + self::CONNECTION_TIMEOUT, // Read timeout. + true, + $password, + !empty($opts) ? $opts : null, + ); } else { $redis = new Redis(); - $redis->connect($server, $port, 1, null, 100, 1, $opts); + $redis->connect( + $server, + $port, + self::CONNECTION_TIMEOUT, // Timeout. + null, + 100, // Retry interval. + self::CONNECTION_TIMEOUT, // Read timeout. + $opts, + ); if (!empty($password)) { $redis->auth($password); } diff --git a/lib/classes/session/redis.php b/lib/classes/session/redis.php index 2d67ea7909e..a2fe0593a61 100644 --- a/lib/classes/session/redis.php +++ b/lib/classes/session/redis.php @@ -99,6 +99,9 @@ class redis extends handler implements SessionHandlerInterface { /** @var int Maximum number of retries for cache store operations. */ const MAX_RETRIES = 5; + /** @var int The number of seconds to wait for a connection or response from the Redis server. */ + const CONNECTION_TIMEOUT = 10; + /** * Create new instance of handler. */ @@ -265,12 +268,27 @@ class redis extends handler implements SessionHandlerInterface { try { // Create a $redis object of a RedisCluster or Redis class. if ($this->clustermode) { - $this->connection = new \RedisCluster(null, $trimmedservers, 1, 1, true, - $this->auth, !empty($opts) ? $opts : null); + $this->connection = new \RedisCluster( + null, + $trimmedservers, + self::CONNECTION_TIMEOUT, // Timeout. + self::CONNECTION_TIMEOUT, // Read timeout. + true, + $this->auth, + !empty($opts) ? $opts : null, + ); } else { $delay = rand(100, 500); $this->connection = new \Redis(); - $this->connection->connect($server, $port, 1, null, $delay, 1, $opts); + $this->connection->connect( + $server, + $port, + self::CONNECTION_TIMEOUT, // Timeout. + null, + $delay, // Retry interval. + self::CONNECTION_TIMEOUT, // Read timeout. + $opts, + ); if ($this->auth !== '' && !$this->connection->auth($this->auth)) { throw new $exceptionclass('Unable to authenticate.'); }