mirror of
https://github.com/moodle/moodle.git
synced 2025-03-22 16:40:07 +01:00
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:
parent
1a33da6637
commit
ff31b37c78
23
cache/stores/redis/lib.php
vendored
23
cache/stores/redis/lib.php
vendored
@ -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);
|
||||
}
|
||||
|
@ -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.');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user