Skip to content

Commit

Permalink
MDL-83753 redis: Set connection and read timeouts to configurable value
Browse files Browse the repository at this point in the history
  • Loading branch information
djarran committed Dec 17, 2024
1 parent a97ddeb commit 81ac90b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
26 changes: 15 additions & 11 deletions lib/classes/session/redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ 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;

/** @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.
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
);
}
Expand Down

0 comments on commit 81ac90b

Please sign in to comment.