2012-12-12 00:04:03 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2013-09-19 17:11:38 +00:00
|
|
|
* Redis client connection pooling manager.
|
2012-12-12 00:04:03 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @defgroup Redis Redis
|
|
|
|
|
*/
|
|
|
|
|
|
2015-07-29 20:32:42 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-06-28 16:45:15 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2015-07-29 20:32:42 +00:00
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
/**
|
2013-09-19 17:11:38 +00:00
|
|
|
* Helper class to manage Redis connections.
|
2012-12-12 00:04:03 +00:00
|
|
|
*
|
|
|
|
|
* This can be used to get handle wrappers that free the handle when the wrapper
|
|
|
|
|
* leaves scope. The maximum number of free handles (connections) is configurable.
|
|
|
|
|
* This provides an easy way to cache connection handles that may also have state,
|
2013-02-08 18:09:41 +00:00
|
|
|
* such as a handle does between multi() and exec(), and without hoarding connections.
|
2012-12-12 00:04:03 +00:00
|
|
|
* The wrappers use PHP magic methods so that calling functions on them calls the
|
|
|
|
|
* function of the actual Redis object handle.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Redis
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
2015-07-29 22:38:40 +00:00
|
|
|
class RedisConnectionPool implements LoggerAwareInterface {
|
2022-03-08 19:01:54 +00:00
|
|
|
/** @var int Connection timeout in seconds */
|
2013-05-20 09:16:27 +00:00
|
|
|
protected $connectTimeout;
|
2014-05-05 18:43:21 +00:00
|
|
|
/** @var string Read timeout in seconds */
|
|
|
|
|
protected $readTimeout;
|
2013-05-20 09:16:27 +00:00
|
|
|
/** @var string Plaintext auth password */
|
|
|
|
|
protected $password;
|
|
|
|
|
/** @var bool Whether connections persist */
|
|
|
|
|
protected $persistent;
|
2014-04-10 18:50:10 +00:00
|
|
|
/** @var int Serializer to use (Redis::SERIALIZER_*) */
|
2013-05-20 09:16:27 +00:00
|
|
|
protected $serializer;
|
2017-04-22 00:17:07 +00:00
|
|
|
/** @var string ID for persistent connections */
|
|
|
|
|
protected $id;
|
2012-12-12 00:04:03 +00:00
|
|
|
|
2013-11-19 20:39:05 +00:00
|
|
|
/** @var int Current idle pool size */
|
2013-05-20 09:16:27 +00:00
|
|
|
protected $idlePoolSize = 0;
|
2012-12-12 00:04:03 +00:00
|
|
|
|
2020-01-24 20:14:50 +00:00
|
|
|
/**
|
|
|
|
|
* @var array (server name => ((connection info array),...)
|
|
|
|
|
* @phan-var array<string,array{conn:Redis,free:bool}[]>
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $connections = [];
|
2013-11-19 20:39:05 +00:00
|
|
|
/** @var array (server name => UNIX timestamp) */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $downServers = [];
|
2012-12-12 00:04:03 +00:00
|
|
|
|
2013-11-19 20:39:05 +00:00
|
|
|
/** @var array (pool ID => RedisConnectionPool) */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected static $instances = [];
|
2012-12-12 00:04:03 +00:00
|
|
|
|
2013-05-20 09:16:27 +00:00
|
|
|
/** integer; seconds to cache servers as "down". */
|
2020-05-16 01:32:53 +00:00
|
|
|
private const SERVER_DOWN_TTL = 30;
|
2012-12-12 00:04:03 +00:00
|
|
|
|
2015-07-29 20:32:42 +00:00
|
|
|
/**
|
|
|
|
|
* @var LoggerInterface
|
|
|
|
|
*/
|
|
|
|
|
protected $logger;
|
|
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $options
|
2017-04-22 00:17:07 +00:00
|
|
|
* @param string $id
|
2015-10-24 22:06:24 +00:00
|
|
|
* @throws Exception
|
2012-12-12 00:04:03 +00:00
|
|
|
*/
|
2017-04-22 00:17:07 +00:00
|
|
|
protected function __construct( array $options, $id ) {
|
2021-04-08 19:17:42 +00:00
|
|
|
if ( !class_exists( Redis::class ) ) {
|
2016-09-23 07:20:19 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
|
__CLASS__ . ' requires a Redis client library. ' .
|
2013-09-19 17:11:38 +00:00
|
|
|
'See https://www.mediawiki.org/wiki/Redis#Setup' );
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
2019-06-28 16:45:15 +00:00
|
|
|
$this->logger = $options['logger'] ?? new NullLogger();
|
2013-03-01 22:53:48 +00:00
|
|
|
$this->connectTimeout = $options['connectTimeout'];
|
2014-05-05 18:43:21 +00:00
|
|
|
$this->readTimeout = $options['readTimeout'];
|
2013-03-01 22:53:48 +00:00
|
|
|
$this->persistent = $options['persistent'];
|
|
|
|
|
$this->password = $options['password'];
|
2012-12-12 00:04:03 +00:00
|
|
|
if ( !isset( $options['serializer'] ) || $options['serializer'] === 'php' ) {
|
|
|
|
|
$this->serializer = Redis::SERIALIZER_PHP;
|
|
|
|
|
} elseif ( $options['serializer'] === 'igbinary' ) {
|
2019-09-05 19:45:15 +00:00
|
|
|
if ( !defined( 'Redis::SERIALIZER_IGBINARY' ) ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
__CLASS__ . ': configured serializer "igbinary" not available' );
|
|
|
|
|
}
|
2012-12-12 00:04:03 +00:00
|
|
|
$this->serializer = Redis::SERIALIZER_IGBINARY;
|
2013-03-13 21:28:06 +00:00
|
|
|
} elseif ( $options['serializer'] === 'none' ) {
|
|
|
|
|
$this->serializer = Redis::SERIALIZER_NONE;
|
2012-12-12 00:04:03 +00:00
|
|
|
} else {
|
2015-10-24 22:06:24 +00:00
|
|
|
throw new InvalidArgumentException( "Invalid serializer specified." );
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
2017-04-22 00:17:07 +00:00
|
|
|
$this->id = $id;
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 20:32:42 +00:00
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 22:53:48 +00:00
|
|
|
/**
|
2013-11-19 20:39:05 +00:00
|
|
|
* @param array $options
|
|
|
|
|
* @return array
|
2013-03-01 22:53:48 +00:00
|
|
|
*/
|
|
|
|
|
protected static function applyDefaultConfig( array $options ) {
|
|
|
|
|
if ( !isset( $options['connectTimeout'] ) ) {
|
|
|
|
|
$options['connectTimeout'] = 1;
|
|
|
|
|
}
|
2014-05-05 18:43:21 +00:00
|
|
|
if ( !isset( $options['readTimeout'] ) ) {
|
2014-07-07 03:36:24 +00:00
|
|
|
$options['readTimeout'] = 1;
|
2014-05-05 18:43:21 +00:00
|
|
|
}
|
2013-03-01 22:53:48 +00:00
|
|
|
if ( !isset( $options['persistent'] ) ) {
|
|
|
|
|
$options['persistent'] = false;
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $options['password'] ) ) {
|
2013-03-11 23:18:25 +00:00
|
|
|
$options['password'] = null;
|
2013-03-01 22:53:48 +00:00
|
|
|
}
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2013-03-01 22:53:48 +00:00
|
|
|
return $options;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
/**
|
2013-11-19 20:39:05 +00:00
|
|
|
* @param array $options
|
2013-03-18 16:57:23 +00:00
|
|
|
* $options include:
|
|
|
|
|
* - connectTimeout : The timeout for new connections, in seconds.
|
|
|
|
|
* Optional, default is 1 second.
|
2014-05-05 18:43:21 +00:00
|
|
|
* - readTimeout : The timeout for operation reads, in seconds.
|
|
|
|
|
* Commands like BLPOP can fail if told to wait longer than this.
|
2014-07-07 03:36:24 +00:00
|
|
|
* Optional, default is 1 second.
|
2013-03-18 16:57:23 +00:00
|
|
|
* - persistent : Set this to true to allow connections to persist across
|
|
|
|
|
* multiple web requests. False by default.
|
|
|
|
|
* - password : The authentication password, will be sent to Redis in clear text.
|
|
|
|
|
* Optional, if it is unspecified, no AUTH command will be sent.
|
|
|
|
|
* - serializer : Set to "php", "igbinary", or "none". Default is "php".
|
2012-12-12 00:04:03 +00:00
|
|
|
* @return RedisConnectionPool
|
|
|
|
|
*/
|
|
|
|
|
public static function singleton( array $options ) {
|
2013-03-01 22:53:48 +00:00
|
|
|
$options = self::applyDefaultConfig( $options );
|
2013-02-21 22:03:26 +00:00
|
|
|
// Map the options to a unique hash...
|
2013-03-07 06:45:59 +00:00
|
|
|
ksort( $options ); // normalize to avoid pool fragmentation
|
|
|
|
|
$id = sha1( serialize( $options ) );
|
2013-02-21 22:03:26 +00:00
|
|
|
// Initialize the object at the hash as needed...
|
2012-12-12 00:04:03 +00:00
|
|
|
if ( !isset( self::$instances[$id] ) ) {
|
2017-04-22 00:17:07 +00:00
|
|
|
self::$instances[$id] = new self( $options, $id );
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
return self::$instances[$id];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-05 07:15:36 +00:00
|
|
|
/**
|
|
|
|
|
* Destroy all singleton() instances
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public static function destroySingletons() {
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$instances = [];
|
2015-12-05 07:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
/**
|
|
|
|
|
* Get a connection to a redis server. Based on code in RedisBagOStuff.php.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $server A hostname/port combination or the absolute path of a UNIX socket.
|
2012-12-12 00:04:03 +00:00
|
|
|
* If a hostname is specified but no port, port 6379 will be used.
|
2022-01-09 17:44:44 +00:00
|
|
|
* @param LoggerInterface|null $logger PSR-3 logger instance. [optional]
|
2019-07-02 00:11:06 +00:00
|
|
|
* @return RedisConnRef|Redis|bool Returns false on failure
|
2020-01-13 21:11:40 +00:00
|
|
|
* @throws InvalidArgumentException
|
2012-12-12 00:04:03 +00:00
|
|
|
*/
|
2016-09-23 07:20:19 +00:00
|
|
|
public function getConnection( $server, LoggerInterface $logger = null ) {
|
2019-07-02 00:11:06 +00:00
|
|
|
// The above @return also documents 'Redis' for convenience with IDEs.
|
|
|
|
|
// RedisConnRef uses PHP magic methods, which wouldn't be recognised.
|
|
|
|
|
|
2016-09-23 07:20:19 +00:00
|
|
|
$logger = $logger ?: $this->logger;
|
2012-12-12 00:04:03 +00:00
|
|
|
// Check the listing "dead" servers which have had a connection errors.
|
|
|
|
|
// Servers are marked dead for a limited period of time, to
|
|
|
|
|
// avoid excessive overhead from repeated connection timeouts.
|
|
|
|
|
if ( isset( $this->downServers[$server] ) ) {
|
|
|
|
|
$now = time();
|
|
|
|
|
if ( $now > $this->downServers[$server] ) {
|
|
|
|
|
// Dead time expired
|
|
|
|
|
unset( $this->downServers[$server] );
|
|
|
|
|
} else {
|
|
|
|
|
// Server is dead
|
2016-09-23 07:20:19 +00:00
|
|
|
$logger->debug(
|
2015-07-29 20:32:42 +00:00
|
|
|
'Server "{redis_server}" is marked down for another ' .
|
|
|
|
|
( $this->downServers[$server] - $now ) . 'seconds',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'redis_server' => $server ]
|
2015-07-29 20:32:42 +00:00
|
|
|
);
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if a connection is already free for use
|
|
|
|
|
if ( isset( $this->connections[$server] ) ) {
|
|
|
|
|
foreach ( $this->connections[$server] as &$connection ) {
|
|
|
|
|
if ( $connection['free'] ) {
|
|
|
|
|
$connection['free'] = false;
|
|
|
|
|
--$this->idlePoolSize;
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2015-07-29 20:32:42 +00:00
|
|
|
return new RedisConnRef(
|
2016-09-23 07:20:19 +00:00
|
|
|
$this, $server, $connection['conn'], $logger
|
2015-07-29 20:32:42 +00:00
|
|
|
);
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 07:20:19 +00:00
|
|
|
if ( !$server ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
__CLASS__ . ": invalid configured server \"$server\"" );
|
|
|
|
|
} elseif ( substr( $server, 0, 1 ) === '/' ) {
|
2012-12-12 00:04:03 +00:00
|
|
|
// UNIX domain socket
|
|
|
|
|
// These are required by the redis extension to start with a slash, but
|
|
|
|
|
// we still need to set the port to a special value to make it work.
|
|
|
|
|
$host = $server;
|
|
|
|
|
$port = 0;
|
|
|
|
|
} else {
|
|
|
|
|
// TCP connection
|
2016-09-23 07:20:19 +00:00
|
|
|
if ( preg_match( '/^\[(.+)\]:(\d+)$/', $server, $m ) ) {
|
|
|
|
|
list( $host, $port ) = [ $m[1], (int)$m[2] ]; // (ip, port)
|
|
|
|
|
} elseif ( preg_match( '/^([^:]+):(\d+)$/', $server, $m ) ) {
|
|
|
|
|
list( $host, $port ) = [ $m[1], (int)$m[2] ]; // (ip or path, port)
|
|
|
|
|
} else {
|
|
|
|
|
list( $host, $port ) = [ $server, 6379 ]; // (ip or path, port)
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$conn = new Redis();
|
|
|
|
|
try {
|
|
|
|
|
if ( $this->persistent ) {
|
2017-04-22 00:17:07 +00:00
|
|
|
$result = $conn->pconnect( $host, $port, $this->connectTimeout, $this->id );
|
2012-12-12 00:04:03 +00:00
|
|
|
} else {
|
|
|
|
|
$result = $conn->connect( $host, $port, $this->connectTimeout );
|
|
|
|
|
}
|
|
|
|
|
if ( !$result ) {
|
2016-09-23 07:20:19 +00:00
|
|
|
$logger->error(
|
2015-07-29 20:32:42 +00:00
|
|
|
'Could not connect to server "{redis_server}"',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'redis_server' => $server ]
|
2015-07-29 20:32:42 +00:00
|
|
|
);
|
2012-12-12 00:04:03 +00:00
|
|
|
// Mark server down for some time to avoid further timeouts
|
|
|
|
|
$this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-03-29 20:12:24 +00:00
|
|
|
if ( ( $this->password !== null ) && !$conn->auth( $this->password ) ) {
|
|
|
|
|
$logger->error(
|
|
|
|
|
'Authentication error connecting to "{redis_server}"',
|
|
|
|
|
[ 'redis_server' => $server ]
|
|
|
|
|
);
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
} catch ( RedisException $e ) {
|
|
|
|
|
$this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
|
2016-09-23 07:20:19 +00:00
|
|
|
$logger->error(
|
2015-07-29 20:32:42 +00:00
|
|
|
'Redis exception connecting to "{redis_server}"',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-07-29 20:32:42 +00:00
|
|
|
'redis_server' => $server,
|
|
|
|
|
'exception' => $e,
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2015-07-29 20:32:42 +00:00
|
|
|
);
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2012-12-12 00:04:03 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 17:41:01 +00:00
|
|
|
$conn->setOption( Redis::OPT_READ_TIMEOUT, $this->readTimeout );
|
|
|
|
|
$conn->setOption( Redis::OPT_SERIALIZER, $this->serializer );
|
|
|
|
|
$this->connections[$server][] = [ 'conn' => $conn, 'free' => false ];
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2020-06-18 17:41:01 +00:00
|
|
|
return new RedisConnRef( $this, $server, $conn, $logger );
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mark a connection to a server as free to return to the pool
|
|
|
|
|
*
|
2013-11-19 20:39:05 +00:00
|
|
|
* @param string $server
|
|
|
|
|
* @param Redis $conn
|
|
|
|
|
* @return bool
|
2012-12-12 00:04:03 +00:00
|
|
|
*/
|
|
|
|
|
public function freeConnection( $server, Redis $conn ) {
|
|
|
|
|
$found = false;
|
|
|
|
|
|
|
|
|
|
foreach ( $this->connections[$server] as &$connection ) {
|
|
|
|
|
if ( $connection['conn'] === $conn && !$connection['free'] ) {
|
|
|
|
|
$connection['free'] = true;
|
|
|
|
|
++$this->idlePoolSize;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->closeExcessIdleConections();
|
|
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close any extra idle connections if there are more than the limit
|
|
|
|
|
*/
|
|
|
|
|
protected function closeExcessIdleConections() {
|
2013-03-07 06:45:59 +00:00
|
|
|
if ( $this->idlePoolSize <= count( $this->connections ) ) {
|
|
|
|
|
return; // nothing to do (no more connections than servers)
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 20:39:05 +00:00
|
|
|
foreach ( $this->connections as &$serverConnections ) {
|
2012-12-12 00:04:03 +00:00
|
|
|
foreach ( $serverConnections as $key => &$connection ) {
|
|
|
|
|
if ( $connection['free'] ) {
|
|
|
|
|
unset( $serverConnections[$key] );
|
2013-03-07 06:45:59 +00:00
|
|
|
if ( --$this->idlePoolSize <= count( $this->connections ) ) {
|
|
|
|
|
return; // done (no more connections than servers)
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 00:55:09 +00:00
|
|
|
/**
|
|
|
|
|
* The redis extension throws an exception in response to various read, write
|
|
|
|
|
* and protocol errors. Sometimes it also closes the connection, sometimes
|
|
|
|
|
* not. The safest response for us is to explicitly destroy the connection
|
|
|
|
|
* object and let it be reopened during the next request.
|
|
|
|
|
*
|
|
|
|
|
* @param RedisConnRef $cref
|
|
|
|
|
* @param RedisException $e
|
|
|
|
|
*/
|
|
|
|
|
public function handleError( RedisConnRef $cref, RedisException $e ) {
|
|
|
|
|
$server = $cref->getServer();
|
2015-07-29 20:32:42 +00:00
|
|
|
$this->logger->error(
|
|
|
|
|
'Redis exception on server "{redis_server}"',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-07-29 20:32:42 +00:00
|
|
|
'redis_server' => $server,
|
|
|
|
|
'exception' => $e,
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2015-07-29 20:32:42 +00:00
|
|
|
);
|
2012-12-12 00:04:03 +00:00
|
|
|
foreach ( $this->connections[$server] as $key => $connection ) {
|
2013-03-09 21:34:22 +00:00
|
|
|
if ( $cref->isConnIdentical( $connection['conn'] ) ) {
|
2012-12-12 00:04:03 +00:00
|
|
|
$this->idlePoolSize -= $connection['free'] ? 1 : 0;
|
|
|
|
|
unset( $this->connections[$server][$key] );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-05 22:43:49 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-11-12 17:39:47 +00:00
|
|
|
* Re-send an AUTH request to the redis server (useful after disconnects).
|
2013-11-05 22:43:49 +00:00
|
|
|
*
|
2013-11-12 17:39:47 +00:00
|
|
|
* This works around an upstream bug in phpredis. phpredis hides disconnects by transparently
|
|
|
|
|
* reconnecting, but it neglects to re-authenticate the new connection. To the user of the
|
|
|
|
|
* phpredis client API this manifests as a seemingly random tendency of connections to lose
|
|
|
|
|
* their authentication status.
|
|
|
|
|
*
|
|
|
|
|
* This method is for internal use only.
|
|
|
|
|
*
|
|
|
|
|
* @see https://github.com/nicolasff/phpredis/issues/403
|
2013-11-05 22:43:49 +00:00
|
|
|
*
|
|
|
|
|
* @param string $server
|
|
|
|
|
* @param Redis $conn
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
public function reauthenticateConnection( $server, Redis $conn ) {
|
2019-03-29 20:12:24 +00:00
|
|
|
if ( $this->password !== null && !$conn->auth( $this->password ) ) {
|
|
|
|
|
$this->logger->error(
|
|
|
|
|
'Authentication error connecting to "{redis_server}"',
|
|
|
|
|
[ 'redis_server' => $server ]
|
|
|
|
|
);
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2019-03-29 20:12:24 +00:00
|
|
|
return false;
|
2013-11-05 22:43:49 +00:00
|
|
|
}
|
2013-11-19 19:40:50 +00:00
|
|
|
|
2013-11-05 22:43:49 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-05 22:18:07 +00:00
|
|
|
|
2014-07-07 03:36:24 +00:00
|
|
|
/**
|
|
|
|
|
* Adjust or reset the connection handle read timeout value
|
|
|
|
|
*
|
|
|
|
|
* @param Redis $conn
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param int|null $timeout Optional
|
2014-07-07 03:36:24 +00:00
|
|
|
*/
|
|
|
|
|
public function resetTimeout( Redis $conn, $timeout = null ) {
|
|
|
|
|
$conn->setOption( Redis::OPT_READ_TIMEOUT, $timeout ?: $this->readTimeout );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-05 22:18:07 +00:00
|
|
|
/**
|
2021-11-19 23:19:42 +00:00
|
|
|
* Make sure connections are closed
|
2013-11-05 22:18:07 +00:00
|
|
|
*/
|
2019-12-05 17:52:55 +00:00
|
|
|
public function __destruct() {
|
2013-11-05 22:18:07 +00:00
|
|
|
foreach ( $this->connections as $server => &$serverConnections ) {
|
|
|
|
|
foreach ( $serverConnections as $key => &$connection ) {
|
2017-08-13 14:18:18 +00:00
|
|
|
try {
|
|
|
|
|
/** @var Redis $conn */
|
|
|
|
|
$conn = $connection['conn'];
|
|
|
|
|
$conn->close();
|
|
|
|
|
} catch ( RedisException $e ) {
|
|
|
|
|
// The destructor can be called on shutdown when random parts of the system
|
|
|
|
|
// have been destructed already, causing weird errors. Ignore them.
|
|
|
|
|
}
|
2014-07-07 03:36:24 +00:00
|
|
|
}
|
2013-11-05 22:43:49 +00:00
|
|
|
}
|
2012-12-12 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
}
|