MediaWiki  1.27.2
RedisConnectionPool Class Reference

Helper class to manage Redis connections. More...

Inheritance diagram for RedisConnectionPool:
Collaboration diagram for RedisConnectionPool:

Public Member Functions

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

Static Public Member Functions

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

Public Attributes

const SERVER_DOWN_TTL = 30
 integer; seconds to cache servers as "down". More...
 

Protected Member Functions

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

Static Protected Member Functions

static applyDefaultConfig (array $options)
 

Protected Attributes

array $connections = []
 (server name => ((connection info array),...) More...
 
array $downServers = []
 (server name => UNIX timestamp) More...
 
int $idlePoolSize = 0
 Current idle pool size. More...
 
LoggerInterface $logger
 
Pool settings.

Settings there are shared for any connection made in this pool.

See the singleton() method documentation for more details.

string $connectTimeout
 Connection timeout in seconds. More...
 
string $readTimeout
 Read timeout in seconds. More...
 
string $password
 Plaintext auth password. More...
 
bool $persistent
 Whether connections persist. More...
 
int $serializer
 Serializer to use (Redis::SERIALIZER_*) More...
 

Static Protected Attributes

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

Detailed Description

Helper class to manage Redis connections.

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, 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 42 of file RedisConnectionPool.php.

Constructor & Destructor Documentation

RedisConnectionPool::__construct ( array  $options)
protected
Parameters
array$options
Exceptions
Exception

Definition at line 84 of file RedisConnectionPool.php.

References setLogger().

RedisConnectionPool::__destruct ( )

Make sure connections are closed for sanity.

Definition at line 418 of file RedisConnectionPool.php.

References $key, and as.

Member Function Documentation

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

Definition at line 121 of file RedisConnectionPool.php.

References $options.

RedisConnectionPool::closeExcessIdleConections ( )
protected

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

Definition at line 315 of file RedisConnectionPool.php.

References $key, and as.

Referenced by freeConnection().

static RedisConnectionPool::destroySingletons ( )
static

Destroy all singleton() instances.

Since
1.27

Definition at line 173 of file RedisConnectionPool.php.

Referenced by ForkController\prepareEnvironment().

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 296 of file RedisConnectionPool.php.

References $idlePoolSize, as, and closeExcessIdleConections().

RedisConnectionPool::getConnection (   $server)

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.
Returns
RedisConnRef|bool Returns false on failure
Exceptions
MWException

Definition at line 185 of file RedisConnectionPool.php.

References $e, $idlePoolSize, as, false, list, and IP\splitHostAndPort().

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 356 of file RedisConnectionPool.php.

References $key, as, RedisConnRef\getServer(), and RedisConnRef\isConnIdentical().

Referenced by handleException().

RedisConnectionPool::handleException (   $server,
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
string$server
RedisConnRef$cref
RedisException$e
Deprecated:
since 1.23

Definition at line 343 of file RedisConnectionPool.php.

References handleError().

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 390 of file RedisConnectionPool.php.

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

Adjust or reset the connection handle read timeout value.

Parameters
Redis$conn
int$timeoutOptional

Definition at line 411 of file RedisConnectionPool.php.

RedisConnectionPool::setLogger ( LoggerInterface  $logger)
Parameters
LoggerInterface$logger
Returns
null

Definition at line 113 of file RedisConnectionPool.php.

References $logger.

Referenced by __construct().

static 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 153 of file RedisConnectionPool.php.

References $options, and serialize().

Referenced by JobQueueAggregatorRedis\__construct(), RedisBagOStuff\__construct(), RedisLockManager\__construct(), PoolCounterRedis\__construct(), JobQueueRedis\__construct(), and RedisPubSubFeedEngine\send().

Member Data Documentation

array RedisConnectionPool::$connections = []
protected

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

Definition at line 65 of file RedisConnectionPool.php.

string RedisConnectionPool::$connectTimeout
protected

Connection timeout in seconds.

Definition at line 50 of file RedisConnectionPool.php.

array RedisConnectionPool::$downServers = []
protected

(server name => UNIX timestamp)

Definition at line 67 of file RedisConnectionPool.php.

int RedisConnectionPool::$idlePoolSize = 0
protected

Current idle pool size.

Definition at line 62 of file RedisConnectionPool.php.

Referenced by freeConnection(), and getConnection().

array RedisConnectionPool::$instances = []
staticprotected

(pool ID => RedisConnectionPool)

Definition at line 70 of file RedisConnectionPool.php.

LoggerInterface RedisConnectionPool::$logger
protected

Definition at line 78 of file RedisConnectionPool.php.

Referenced by setLogger().

string RedisConnectionPool::$password
protected

Plaintext auth password.

Definition at line 54 of file RedisConnectionPool.php.

bool RedisConnectionPool::$persistent
protected

Whether connections persist.

Definition at line 56 of file RedisConnectionPool.php.

string RedisConnectionPool::$readTimeout
protected

Read timeout in seconds.

Definition at line 52 of file RedisConnectionPool.php.

int RedisConnectionPool::$serializer
protected

Serializer to use (Redis::SERIALIZER_*)

Definition at line 58 of file RedisConnectionPool.php.

const RedisConnectionPool::SERVER_DOWN_TTL = 30

integer; seconds to cache servers as "down".

Definition at line 73 of file RedisConnectionPool.php.


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