MediaWiki master
RedisConnRef.php
Go to the documentation of this file.
1<?php
8
9use Psr\Log\LoggerAwareInterface;
10use Psr\Log\LoggerInterface;
11use Redis;
12use RedisException;
13
25class RedisConnRef implements LoggerAwareInterface {
27 protected $pool;
29 protected $conn;
31 protected $server;
33 protected $lastError;
34
38 protected $logger;
39
43 private const AUTH_NO_ERROR = 200;
44
48 private const AUTH_ERROR_TEMPORARY = 201;
49
53 private const AUTH_ERROR_PERMANENT = 202;
54
61 public function __construct(
62 RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
63 ) {
64 $this->pool = $pool;
65 $this->server = $server;
66 $this->conn = $conn;
67 $this->logger = $logger;
68 }
69
70 public function setLogger( LoggerInterface $logger ): void {
71 $this->logger = $logger;
72 }
73
78 public function getServer() {
79 return $this->server;
80 }
81
85 public function getLastError() {
86 return $this->lastError;
87 }
88
89 public function clearLastError() {
90 $this->lastError = null;
91 }
92
101 public function __call( $name, $arguments ) {
102 // Work around https://github.com/nicolasff/phpredis/issues/70
103 $lname = strtolower( $name );
104 if (
105 ( $lname === 'blpop' || $lname === 'brpop' || $lname === 'brpoplpush' )
106 && count( $arguments ) > 1
107 ) {
108 // Get timeout off the end since it is always required and argument length can vary
109 $timeout = end( $arguments );
110 // Only give the additional one second buffer if not requesting an infinite timeout
111 $this->pool->resetTimeout( $this->conn, ( $timeout > 0 ? $timeout + 1 : $timeout ) );
112 }
113
114 return $this->tryCall( $name, $arguments );
115 }
116
125 private function tryCall( $method, $arguments ) {
126 $this->conn->clearLastError();
127 try {
128 $res = $this->conn->$method( ...$arguments );
129 $authError = $this->checkAuthentication();
130 if ( $authError === self::AUTH_ERROR_TEMPORARY ) {
131 $res = $this->conn->$method( ...$arguments );
132 }
133 if ( $authError === self::AUTH_ERROR_PERMANENT ) {
134 throw new RedisException( "Failure reauthenticating to Redis." );
135 }
136 return $res;
137 } finally {
138 $this->postCallCleanup();
139 }
140 }
141
152 public function scan( &$iterator, $pattern = null, $count = null ) {
153 return $this->tryCall( 'scan', [ &$iterator, $pattern, $count ] );
154 }
155
167 public function sScan( $key, &$iterator, $pattern = null, $count = null ) {
168 return $this->tryCall( 'sScan', [ $key, &$iterator, $pattern, $count ] );
169 }
170
182 public function hScan( $key, &$iterator, $pattern = null, $count = null ) {
183 return $this->tryCall( 'hScan', [ $key, &$iterator, $pattern, $count ] );
184 }
185
197 public function zScan( $key, &$iterator, $pattern = null, $count = null ) {
198 return $this->tryCall( 'zScan', [ $key, &$iterator, $pattern, $count ] );
199 }
200
206 private function checkAuthentication() {
207 $lastError = $this->conn->getLastError();
208 if ( $lastError && preg_match( '/^ERR operation not permitted\b/', $lastError ) ) {
209 if ( !$this->pool->reauthenticateConnection( $this->server, $this->conn ) ) {
210 return self::AUTH_ERROR_PERMANENT;
211 }
212 $this->conn->clearLastError();
213 $this->logger->info(
214 "Used automatic re-authentication for Redis.",
215 [ 'redis_server' => $this->server ]
216 );
217 return self::AUTH_ERROR_TEMPORARY;
218 }
219 return self::AUTH_NO_ERROR;
220 }
221
227 private function postCallCleanup() {
228 $this->lastError = $this->conn->getLastError() ?: $this->lastError;
229
230 // Restore original timeout in the case of blocking calls.
231 $this->pool->resetTimeout( $this->conn );
232 }
233
241 public function luaEval( $script, array $params, $numKeys ) {
242 $sha1 = sha1( $script ); // 40 char hex
243 $conn = $this->conn; // convenience
244 $server = $this->server; // convenience
245
246 // Try to run the server-side cached copy of the script
247 $conn->clearLastError();
248 $res = $conn->evalSha( $sha1, $params, $numKeys );
249 // If we got a permission error reply that means that (a) we are not in
250 // multi()/pipeline() and (b) some connection problem likely occurred. If
251 // the password the client gave was just wrong, an exception should have
252 // been thrown back in getConnection() previously.
253 $lastError = $conn->getLastError();
254 if ( $lastError && preg_match( '/^ERR operation not permitted\b/', $lastError ) ) {
255 $this->pool->reauthenticateConnection( $server, $conn );
256 $conn->clearLastError();
257 $res = $conn->eval( $script, $params, $numKeys );
258 $this->logger->info(
259 "Used automatic re-authentication for Lua script '$sha1'.",
260 [ 'redis_server' => $server ]
261 );
262 }
263 // If the script is not in cache, use eval() to retry and cache it
264 $lastError = $conn->getLastError();
265 if ( $lastError && preg_match( '/^NOSCRIPT/', $lastError ) ) {
266 $conn->clearLastError();
267 $res = $conn->eval( $script, $params, $numKeys );
268 $this->logger->info(
269 "Used eval() for Lua script '$sha1'.",
270 [ 'redis_server' => $server ]
271 );
272 }
273
274 $lastError = $conn->getLastError();
275 if ( $lastError ) { // script bug?
276 $this->logger->error(
277 'Lua script error on server "{redis_server}": {lua_error}',
278 [
279 'redis_server' => $server,
280 'lua_error' => $lastError
281 ]
282 );
283 $this->lastError = $lastError;
284 }
285
286 return $res;
287 }
288
293 public function isConnIdentical( Redis $conn ) {
294 return $this->conn === $conn;
295 }
296
297 public function __destruct() {
298 $this->pool->freeConnection( $this->server, $this->conn );
299 }
300}
301
303class_alias( RedisConnRef::class, 'RedisConnRef' );
Wrapper class for Redis connections that automatically reuses connections (via RAII pattern)
hScan( $key, &$iterator, $pattern=null, $count=null)
Hash Scan Handle this explicitly due to needing the iterator passed by reference.
setLogger(LoggerInterface $logger)
scan(&$iterator, $pattern=null, $count=null)
Key Scan Handle this explicitly due to needing the iterator passed by reference.
zScan( $key, &$iterator, $pattern=null, $count=null)
Sorted Set Scan Handle this explicitly due to needing the iterator passed by reference.
__call( $name, $arguments)
Magic __call handler for most Redis functions.
sScan( $key, &$iterator, $pattern=null, $count=null)
Set Scan Handle this explicitly due to needing the iterator passed by reference.
__construct(RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger)
luaEval( $script, array $params, $numKeys)
Manage one or more Redis client connection.