MDL-83753 redis: Set connection and read timeouts to configurable value

This commit is contained in:
djarrancotleanu 2024-12-17 13:46:38 +10:00
parent a97ddeb2a2
commit 50100d3106
2 changed files with 15 additions and 10 deletions

View file

@ -370,6 +370,7 @@ $CFG->admin = 'admin';
// $CFG->session_redis_lock_expire = 7200; // Optional, defaults to session timeout. // $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. // $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. // // After 5 seconds it will throttle down to once per second.
// $CFG->session_redis_connection_timeout = 3; // Optional, default is 3.
// //
// Use the igbinary serializer instead of the php default one. Note that phpredis must be compiled with // 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! // igbinary support to make the setting to work. Also, if you change the serializer you have to flush the database!

View file

@ -114,8 +114,8 @@ class redis extends handler implements SessionHandlerInterface {
/** @var clock A clock instance */ /** @var clock A clock instance */
protected clock $clock; protected clock $clock;
/** @var int The number of seconds to wait for a connection or response from the Redis server. */ /** @var int $connectiontimeout The number of seconds to wait for a connection or response from the Redis server. */
const CONNECTION_TIMEOUT = 10; protected int $connectiontimeout = 3;
/** /**
* Create new instance of handler. * Create new instance of handler.
@ -204,6 +204,10 @@ class redis extends handler implements SessionHandlerInterface {
$this->compressor = $CFG->session_redis_compressor; $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); $this->clock = di::get(clock::class);
} }
@ -293,8 +297,8 @@ class redis extends handler implements SessionHandlerInterface {
$this->connection = new \RedisCluster( $this->connection = new \RedisCluster(
name: null, name: null,
seeds: $trimmedservers, seeds: $trimmedservers,
timeout: self::CONNECTION_TIMEOUT, // Timeout. timeout: $this->connectiontimeout, // Timeout.
read_timeout: self::CONNECTION_TIMEOUT, // Read timeout. read_timeout: $this->connectiontimeout, // Read timeout.
persistent: true, persistent: true,
auth: $this->auth, auth: $this->auth,
context: !empty($opts) ? $opts : null, context: !empty($opts) ? $opts : null,
@ -303,8 +307,8 @@ class redis extends handler implements SessionHandlerInterface {
$this->connection = new \RedisCluster( $this->connection = new \RedisCluster(
null, null,
$trimmedservers, $trimmedservers,
self::CONNECTION_TIMEOUT, $this->connectiontimeout,
self::CONNECTION_TIMEOUT, $this->connectiontimeout,
true, true,
$this->auth, $this->auth,
!empty($opts) ? $opts : null !empty($opts) ? $opts : null
@ -318,19 +322,19 @@ class redis extends handler implements SessionHandlerInterface {
$this->connection->connect( $this->connection->connect(
host: $server, host: $server,
port: $port, port: $port,
timeout: self::CONNECTION_TIMEOUT, // Timeout. timeout: $this->connectiontimeout, // Timeout.
retry_interval: $delay, retry_interval: $delay,
read_timeout: self::CONNECTION_TIMEOUT, // Read timeout. read_timeout: $this->connectiontimeout, // Read timeout.
context: $opts, context: $opts,
); );
} else { } else {
$this->connection->connect( $this->connection->connect(
$server, $server,
$port, $port,
self::CONNECTION_TIMEOUT, $this->connectiontimeout,
null, null,
$delay, $delay,
self::CONNECTION_TIMEOUT, $this->connectiontimeout,
$opts $opts
); );
} }