MediaWiki master
Wikimedia\ObjectCache\RedisConnectionPool Class Reference

Manage one or more Redis client connection. More...

Inherits LoggerAwareInterface.

Collaboration diagram for Wikimedia\ObjectCache\RedisConnectionPool:

Public Member Functions

 __destruct ()
 Make sure connections are closed.
 
 freeConnection ( $server, Redis $conn)
 Mark a connection to a server as free to return to the pool.
 
 getConnection ( $server, ?LoggerInterface $logger=null)
 Get a connection to a redis server.
 
 handleError (RedisConnRef $cref, RedisException $e)
 The redis extension throws an exception in response to various read, write and protocol errors.
 
 reauthenticateConnection ( $server, Redis $conn)
 Re-send an AUTH request to the redis server (useful after disconnects).
 
 resetTimeout (Redis $conn, $timeout=null)
 Adjust or reset the connection handle read timeout value.
 
 setLogger (LoggerInterface $logger)
 

Static Public Member Functions

static destroySingletons ()
 Destroy all singleton() instances.
 
static singleton (array $options)
 

Protected Member Functions

 __construct (array $options, $id)
 
 closeExcessIdleConections ()
 Close any extra idle connections if there are more than the limit.
 

Static Protected Member Functions

static applyDefaultConfig (array $options)
 

Protected Attributes

array $connections = []
 (server name => ((connection info array),...)
 
int $connectTimeout
 Connection timeout in seconds.
 
array $downServers = []
 (server name => UNIX timestamp)
 
string $id
 ID for persistent connections.
 
int $idlePoolSize = 0
 Current idle pool size.
 
LoggerInterface $logger
 
string string[] null $password
 Plaintext auth password or array containing username and password.
 
bool $persistent
 Whether connections persist.
 
string null $prefix
 Key prefix automatically added to all operations.
 
string $readTimeout
 Read timeout in seconds.
 
int $serializer
 Serializer to use (Redis::SERIALIZER_*)
 

Static Protected Attributes

static array $instances = []
 (pool ID => RedisConnectionPool)
 

Detailed Description

Manage one or more Redis client connection.

This can be used to get RedisConnRef objects that automatically reuses connections internally after the calling function has returned (and thus your RedisConnRef instance leaves scope/destructs).

This provides an easy way to cache connection handles that may also have state, such as a handle does between multi() and exec(), and without hoarding connections. The wrappers use PHP magic methods so that calling functions on them calls the function of the actual Redis object handle.

Since
1.21

Definition at line 47 of file RedisConnectionPool.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\ObjectCache\RedisConnectionPool::__construct ( array $options,
$id )
protected
Parameters
array$options
string$id
Exceptions
Exception

Definition at line 90 of file RedisConnectionPool.php.

References Wikimedia\ObjectCache\RedisConnectionPool\$id.

◆ __destruct()

Wikimedia\ObjectCache\RedisConnectionPool::__destruct ( )

Make sure connections are closed.

Definition at line 420 of file RedisConnectionPool.php.

Member Function Documentation

◆ applyDefaultConfig()

static Wikimedia\ObjectCache\RedisConnectionPool::applyDefaultConfig ( array $options)
staticprotected
Parameters
array$options
Returns
array

Definition at line 126 of file RedisConnectionPool.php.

◆ closeExcessIdleConections()

Wikimedia\ObjectCache\RedisConnectionPool::closeExcessIdleConections ( )
protected

Close any extra idle connections if there are more than the limit.

Definition at line 334 of file RedisConnectionPool.php.

◆ destroySingletons()

static Wikimedia\ObjectCache\RedisConnectionPool::destroySingletons ( )
static

Destroy all singleton() instances.

Since
1.27

Definition at line 178 of file RedisConnectionPool.php.

◆ freeConnection()

Wikimedia\ObjectCache\RedisConnectionPool::freeConnection ( $server,
Redis $conn )

Mark a connection to a server as free to return to the pool.

Parameters
string$server
Redis$conn
Returns
bool

Definition at line 315 of file RedisConnectionPool.php.

◆ getConnection()

Wikimedia\ObjectCache\RedisConnectionPool::getConnection ( $server,
?LoggerInterface $logger = null )

Get a connection to a redis server.

Based on code in RedisBagOStuff.php.

Parameters
string$serverA hostname/port combination or the absolute path of a UNIX socket. If a hostname is specified but no port, port 6379 will be used.
LoggerInterface | null$loggerPSR-3 logger instance. [optional]
Returns
RedisConnRef|Redis|false Returns false on failure
Exceptions
InvalidArgumentException

Definition at line 191 of file RedisConnectionPool.php.

◆ handleError()

Wikimedia\ObjectCache\RedisConnectionPool::handleError ( RedisConnRef $cref,
RedisException $e )

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.

Parameters
RedisConnRef$cref
RedisException$e

Definition at line 360 of file RedisConnectionPool.php.

References Wikimedia\ObjectCache\RedisConnRef\getServer(), and Wikimedia\ObjectCache\RedisConnRef\isConnIdentical().

◆ reauthenticateConnection()

Wikimedia\ObjectCache\RedisConnectionPool::reauthenticateConnection ( $server,
Redis $conn )

Re-send an AUTH request to the redis server (useful after disconnects).

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 also
https://github.com/nicolasff/phpredis/issues/403
Parameters
string$server
Redis$conn
Returns
bool Success

Definition at line 394 of file RedisConnectionPool.php.

◆ resetTimeout()

Wikimedia\ObjectCache\RedisConnectionPool::resetTimeout ( Redis $conn,
$timeout = null )

Adjust or reset the connection handle read timeout value.

Parameters
Redis$conn
int | null$timeoutOptional

Definition at line 413 of file RedisConnectionPool.php.

◆ setLogger()

Wikimedia\ObjectCache\RedisConnectionPool::setLogger ( LoggerInterface $logger)

Definition at line 118 of file RedisConnectionPool.php.

◆ singleton()

static Wikimedia\ObjectCache\RedisConnectionPool::singleton ( array $options)
static
Parameters
array$options$options include:
  • connectTimeout : The timeout for new connections, in seconds. Optional, default is 1 second.
  • readTimeout : The timeout for operation reads, in seconds. Commands like BLPOP can fail if told to wait longer than this. Optional, default is 1 second.
  • 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".
Returns
RedisConnectionPool

Definition at line 161 of file RedisConnectionPool.php.

Referenced by Wikimedia\ObjectCache\RedisBagOStuff\__construct(), and MediaWiki\JobQueue\JobQueueRedis\__construct().

Member Data Documentation

◆ $connections

array Wikimedia\ObjectCache\RedisConnectionPool::$connections = []
protected

(server name => ((connection info array),...)

Definition at line 70 of file RedisConnectionPool.php.

◆ $connectTimeout

int Wikimedia\ObjectCache\RedisConnectionPool::$connectTimeout
protected

Connection timeout in seconds.

Definition at line 49 of file RedisConnectionPool.php.

◆ $downServers

array Wikimedia\ObjectCache\RedisConnectionPool::$downServers = []
protected

(server name => UNIX timestamp)

Definition at line 72 of file RedisConnectionPool.php.

◆ $id

string Wikimedia\ObjectCache\RedisConnectionPool::$id
protected

ID for persistent connections.

Definition at line 61 of file RedisConnectionPool.php.

Referenced by Wikimedia\ObjectCache\RedisConnectionPool\__construct().

◆ $idlePoolSize

int Wikimedia\ObjectCache\RedisConnectionPool::$idlePoolSize = 0
protected

Current idle pool size.

Definition at line 64 of file RedisConnectionPool.php.

◆ $instances

array Wikimedia\ObjectCache\RedisConnectionPool::$instances = []
staticprotected

(pool ID => RedisConnectionPool)

Definition at line 75 of file RedisConnectionPool.php.

◆ $logger

LoggerInterface Wikimedia\ObjectCache\RedisConnectionPool::$logger
protected

Definition at line 83 of file RedisConnectionPool.php.

◆ $password

string string [] null Wikimedia\ObjectCache\RedisConnectionPool::$password
protected

Plaintext auth password or array containing username and password.

Definition at line 53 of file RedisConnectionPool.php.

◆ $persistent

bool Wikimedia\ObjectCache\RedisConnectionPool::$persistent
protected

Whether connections persist.

Definition at line 57 of file RedisConnectionPool.php.

◆ $prefix

string null Wikimedia\ObjectCache\RedisConnectionPool::$prefix
protected

Key prefix automatically added to all operations.

Definition at line 55 of file RedisConnectionPool.php.

◆ $readTimeout

string Wikimedia\ObjectCache\RedisConnectionPool::$readTimeout
protected

Read timeout in seconds.

Definition at line 51 of file RedisConnectionPool.php.

◆ $serializer

int Wikimedia\ObjectCache\RedisConnectionPool::$serializer
protected

Serializer to use (Redis::SERIALIZER_*)

Definition at line 59 of file RedisConnectionPool.php.


The documentation for this class was generated from the following file: