thomascube
2011-08-24 fe91e2f31de584f0178e06d9b03c587cec700f8e
Improve memcache connection and failure handling; add debug option to trace memcache activity for session storage

2 files modified
52 ■■■■ changed files
program/include/rcmail.php 31 ●●●● patch | view | raw | blame | history
program/include/rcube_session.php 21 ●●●● patch | view | raw | blame | history
program/include/rcmail.php
@@ -336,21 +336,42 @@
      }
      $this->memcache = new Memcache;
      $mc_available = 0;
      $this->mc_available = 0;
      // add alll configured hosts to pool
      $pconnect = $this->config->get('memcache_pconnect', true);
      foreach ($this->config->get('memcache_hosts', array()) as $host) {
        list($host, $port) = explode(':', $host);
        if (!$port) $port = 11211;
        // add server and attempt to connect if not already done yet
        if ($this->memcache->addServer($host, $port) && !$mc_available)
          $mc_available += intval($this->memcache->connect($host, $port));
        $this->mc_available += intval($this->memcache->addServer($host, $port, $pconnect, 1, 1, 15, false, array($this, 'memcache_failure')));
      }
      // test connection and failover (will result in $this->mc_available == 0 on complete failure)
      $this->memcache->increment('__CONNECTIONTEST__', 1);  // NOP if key doesn't exist
      if (!$mc_available)
      if (!$this->mc_available)
        $this->memcache = false;
    }
    return $this->memcache;
  }
  /**
   * Callback for memcache failure
   */
  public function memcache_failure($host, $port)
  {
    static $seen = array();
    // only report once
    if (!$seen["$host:$port"]++) {
      $this->mc_available--;
      raise_error(array('code' => 604, 'type' => 'db',
        'line' => __LINE__, 'file' => __FILE__,
        'message' => "Memcache failure on host $host:$port"),
        true, false);
    }
  }
  /**
program/include/rcube_session.php
@@ -55,6 +55,7 @@
    $this->start = microtime(true);
    $this->ip = $_SERVER['REMOTE_ADDR'];
    $this->logging = $config->get('log_session', false);
    $this->mc_debug = $config->get('memcache_debug', false);
    $lifetime = $config->get('session_lifetime', 1) * 60;
    $this->set_lifetime($lifetime);
@@ -255,8 +256,9 @@
   */
  public function mc_read($key)
  {
    if ($value = $this->memcache->get($key)) {
      $arr = unserialize($value);
    $value = $this->memcache->get($key);
    if ($this->mc_debug) write_log('memcache', "get($key): " . strlen($value));
    if ($value && ($arr = unserialize($value))) {
      $this->changed = $arr['changed'];
      $this->ip      = $arr['ip'];
      $this->vars    = $arr['vars'];
@@ -289,8 +291,15 @@
    $newvars = $oldvars !== false ? $this->_fixvars($vars, $oldvars) : $vars;
    
    if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 2)
      return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)), MEMCACHE_COMPRESSED, $this->lifetime);
    if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 2) {
      $value = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars));
      $ret = $this->memcache->set($key, $value, MEMCACHE_COMPRESSED, $this->lifetime);
      if ($this->mc_debug) {
        write_log('memcache', "set($key): " . strlen($value) . ": " . ($ret ? 'OK' : 'ERR'));
        write_log('memcache', "... get($key): " . strlen($this->memcache->get($key)));
      }
      return $ret;
    }
    
    return true;
  }
@@ -303,7 +312,9 @@
   */
  public function mc_destroy($key)
  {
    return $this->memcache->delete($key);
    $ret = $this->memcache->delete($key);
    if ($this->mc_debug) write_log('memcache', "delete($key): " . ($ret ? 'OK' : 'ERR'));
    return $ret;
  }