Skip to content

Commit

Permalink
MDL-83753 cachestore_redis: Allow for configurable connection timeout…
Browse files Browse the repository at this point in the history
… setting
  • Loading branch information
djarran committed Dec 18, 2024
1 parent c6bf610 commit 209072a
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions cache/stores/redis/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ class cachestore_redis extends store implements
*/
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.
*
Expand Down Expand Up @@ -133,6 +130,13 @@ class cachestore_redis extends store implements
/** @var ?array Array of current locks, or null if we haven't registered shutdown function */
protected $currentlocks = null;

/**
* The number of seconds to wait for a connection or response from the Redis server.
*
* @var int
*/
protected $connectiontimeout = 3;

/**
* Determines if the requirements for this type of store are met.
*
Expand Down Expand Up @@ -203,6 +207,9 @@ public function __construct(
if (array_key_exists('locktimeout', $configuration)) {
$this->locktimeout = (int)$configuration['locktimeout'];
}
if ($connectiontimeout = get_config('cachestore_redis', 'connectiontimeout')) {
$this->connectiontimeout = $connectiontimeout;
}
$this->redis = $this->new_redis($configuration);
}

Expand Down Expand Up @@ -277,8 +284,8 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$redis = 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: $password,
context: !empty($opts) ? $opts : null,
Expand All @@ -287,8 +294,8 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$redis = new RedisCluster(
null,
$trimmedservers,
self::CONNECTION_TIMEOUT,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
$this->connectiontimeout,
true, $password,
!empty($opts) ? $opts : null,
);
Expand All @@ -300,18 +307,18 @@ protected function new_redis(array $configuration): Redis|RedisCluster|null {
$redis->connect(
host: $server,
port: $port,
timeout: self::CONNECTION_TIMEOUT, // Timeout.
timeout: $this->connectiontimeout, // Timeout.
retry_interval: 100, // Retry interval.
read_timeout: self::CONNECTION_TIMEOUT, // Read timeout.
read_timeout: $this->connectiontimeout, // Read timeout.
context: $opts,
);
} else {
$redis->connect(
$server, $port,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
null,
100,
self::CONNECTION_TIMEOUT,
$this->connectiontimeout,
$opts,
);
}
Expand Down

0 comments on commit 209072a

Please sign in to comment.