Redis: Improve error handling and phpredis 5.X support (#6888)

pull/6950/head
Aleksander Machniak 5 years ago
parent 4d7ce46fc0
commit c6de97c5a8

@ -2,6 +2,7 @@ CHANGELOG Roundcube Webmail
===========================
- Elastic: Resizeable columns (#6929)
- Redis: Improve error handling and phpredis 5.X support (#6888)
RELEASE 1.4-rc2
---------------

@ -96,7 +96,7 @@ class rcube_cache_memcache extends rcube_cache
if (!$seen["$host:$port"]++) {
$available--;
rcube::raise_error(array(
'code' => 604, 'type' => 'db',
'code' => 604, 'type' => 'memcache',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Memcache failure on host $host:$port"),
true, false);

@ -111,6 +111,7 @@ class rcube_cache_redis extends rcube_cache
}
self::$redis = new Redis;
$failures = 0;
foreach ($hosts as $redis_host) {
// explode individual fields
@ -128,45 +129,38 @@ class rcube_cache_redis extends rcube_cache
try {
if (self::$redis->connect($host, $port) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not connect to Redis server. Please check host and port"
),
true, true);
throw new Exception("Could not connect to Redis server. Please check host and port.");
}
if ($password !== null && self::$redis->auth($password) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not authenticate with Redis server. Please check password."
),
true, true);
throw new Exception("Could not authenticate with Redis server. Please check password.");
}
if ($database !== null && self::$redis->select($database) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not select Redis database. Please check database setting."
),
true, true);
throw new Exception("Could not select Redis database. Please check database setting.");
}
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
rcube::raise_error($e, true, false);
$failures++;
}
}
if (count($hosts) === $failures) {
self::$redis = false;
}
if (self::$redis->ping() !== "+PONG") {
if (self::$redis) {
try {
$ping = self::$redis->ping();
if ($ping !== true && $ping !== "+PONG") {
throw new Exception("Redis connection failure. Ping failed.");
}
}
catch (Exception $e) {
self::$redis = false;
rcube::raise_error($e, true, false);
}
}
return self::$redis;
@ -201,7 +195,13 @@ class rcube_cache_redis extends rcube_cache
return false;
}
try {
$data = self::$redis->get($key);
}
catch (Exception $e) {
raise_error($e, true, false);
return false;
}
if ($this->debug) {
$this->debug('get', $key, $data);
@ -224,7 +224,13 @@ class rcube_cache_redis extends rcube_cache
return false;
}
try {
$result = self::$redis->setEx($key, $this->ttl, $data);
}
catch (Exception $e) {
raise_error($e, true, false);
return false;
}
if ($this->debug) {
$this->debug('set', $key, $data, $result);
@ -246,8 +252,14 @@ class rcube_cache_redis extends rcube_cache
return false;
}
try {
$fname = method_exists(self::$redis, 'del') ? 'del' : 'delete';
$result = self::$redis->$fname($key);
}
catch (Exception $e) {
raise_error($e, true, false);
return false;
}
if ($this->debug) {
$this->debug('delete', $key, null, $result);

@ -48,7 +48,7 @@ class rcube_session_memcache extends rcube_session
if (!$this->memcache) {
rcube::raise_error(array(
'code' => 604, 'type' => 'db',
'code' => 604, 'type' => 'memcache',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to connect to memcached. Please check configuration"),
true, true);

@ -48,7 +48,7 @@ class rcube_session_memcached extends rcube_session
if (!$this->memcache) {
rcube::raise_error(array(
'code' => 604, 'type' => 'db',
'code' => 604, 'type' => 'memcache',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to connect to memcached. Please check configuration"),
true, true);

@ -42,6 +42,14 @@ class rcube_session_redis extends rcube_session {
$this->redis = rcube::get_instance()->get_redis();
$this->debug = $config->get('redis_debug');
if (!$this->redis) {
rcube::raise_error(array(
'code' => 604, 'type' => 'redis',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to connect to redis. Please check configuration"),
true, true);
}
// register sessions handler
$this->register_session_handler();
}
@ -79,8 +87,13 @@ class rcube_session_redis extends rcube_session {
public function destroy($key)
{
if ($key) {
try {
$fname = method_exists($this->redis, 'del') ? 'del' : 'delete';
$result = $this->redis->$fname($key);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('delete', $key, null, $result);
@ -99,7 +112,18 @@ class rcube_session_redis extends rcube_session {
*/
public function read($key)
{
if ($value = $this->redis->get($key)) {
try {
$value = $this->redis->get($key);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('get', $key, $value);
}
if ($value) {
$arr = unserialize($value);
$this->changed = $arr['changed'];
$this->ip = $arr['ip'];
@ -107,10 +131,6 @@ class rcube_session_redis extends rcube_session {
$this->key = $key;
}
if ($this->debug) {
$this->debug('get', $key, $value);
}
return $this->vars ?: '';
}
@ -129,7 +149,13 @@ class rcube_session_redis extends rcube_session {
if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
$data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars));
try {
$result = $this->redis->setex($key, $this->lifetime + 60, $data);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('set', $key, $data, $result);
@ -155,8 +181,13 @@ class rcube_session_redis extends rcube_session {
return true;
}
try {
$data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars));
$result = $this->redis->setex($key, $this->lifetime + 60, $data);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('set', $key, $data, $result);

Loading…
Cancel
Save