[RedisCache] Connections should not be shared by daemon threads

This commit is contained in:
Alexei Sorokin 2020-08-13 22:51:22 +03:00 committed by Diogo Peralta Cordeiro
parent a9ae96fa4c
commit 8fc807343b

View File

@ -15,7 +15,7 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Plugin inplementing Redis based caching * Plugin implementing Redis based caching
* *
* @category Files * @category Files
* @package GNUsocial * @package GNUsocial
@ -34,6 +34,10 @@ class RedisCachePlugin extends Plugin
{ {
const PLUGIN_VERSION = '0.1.0'; const PLUGIN_VERSION = '0.1.0';
public static $cacheInitialized = false;
public $persistent = null;
// settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]); // settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]);
public $server = null; public $server = null;
public $defaultExpiry = 86400; // 24h public $defaultExpiry = 86400; // 24h
@ -42,22 +46,29 @@ class RedisCachePlugin extends Plugin
public function onInitializePlugin() public function onInitializePlugin()
{ {
$this->_ensureConn(); if (self::$cacheInitialized) {
$this->persistent = true;
return true; } else {
} // If we're a parent command-line process we need
// to be able to close out the connection after
private function _ensureConn() // forking, so disable persistence.
{ //
if ($this->client === null) { // We'll turn it back on again the second time
$this->client = new Client($this->server); // through which will either be in a child process,
// or a single-process script which is switching
// configurations.
$this->persistent = (php_sapi_name() === 'cli') ? false : true;
} }
$this->ensureConn();
self::$cacheInitialized = true;
return true;
} }
public function onStartCacheGet($key, &$value) public function onStartCacheGet($key, &$value)
{ {
try { try {
$this->_ensureConn(); $this->ensureConn();
$data = $this->client->get($key); $data = $this->client->get($key);
} catch (PredisException $e) { } catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage()); common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -91,8 +102,7 @@ class RedisCachePlugin extends Plugin
} }
try { try {
$this->_ensureConn(); $this->ensureConn();
$ret = $this->client->setex($key, $expiry, serialize($value)); $ret = $this->client->setex($key, $expiry, serialize($value));
} catch (PredisException $e) { } catch (PredisException $e) {
$ret = false; $ret = false;
@ -116,7 +126,7 @@ class RedisCachePlugin extends Plugin
} }
try { try {
$this->_ensureConn(); $this->ensureConn();
$ret = $this->client->del($key); $ret = $this->client->del($key);
} catch (PredisException $e) { } catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage()); common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -129,7 +139,7 @@ class RedisCachePlugin extends Plugin
public function onStartCacheIncrement($key, $step, $value) public function onStartCacheIncrement($key, $step, $value)
{ {
try { try {
$this->_ensureConn(); $this->ensureConn();
$this->client->incrby($key, $step); $this->client->incrby($key, $step);
} catch (PredisException $e) { } catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage()); common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -139,6 +149,33 @@ class RedisCachePlugin extends Plugin
return false; return false;
} }
public function onStartCacheReconnect(bool &$success): bool
{
if (is_null($this->client)) {
// nothing to do
return true;
}
if ($this->persistent) {
common_log(LOG_ERR, 'Cannot close persistent Redis connection');
$success = false;
} else {
common_log(LOG_INFO, 'Closing Redis connection');
$success = $this->client->disconnect();
$this->client = null;
}
return false;
}
private function ensureConn(): void
{
if (is_null($this->client)) {
$this->client = new Client(
$this->server,
['persistent' => $this->persistent]
);
}
}
public function onPluginVersion(array &$versions): bool public function onPluginVersion(array &$versions): bool
{ {
$versions[] = [ $versions[] = [