MDL-81987 redis: Set connection and read timeouts to 10 seconds

This allows the site to continue operating in case of degraded
performance, rather than throwing exceptions.
This commit is contained in:
Mark Johnson 2024-08-07 14:16:51 +01:00
parent 1a33da6637
commit ff31b37c78
No known key found for this signature in database
GPG Key ID: EB30E1468CFAE242
2 changed files with 42 additions and 5 deletions

View File

@ -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);
}

View File

@ -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.');
}