From 21030324d032cda4322d4fe6bf10e7c74d05c673 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Mon, 05 Sep 2011 15:58:11 -0400
Subject: [PATCH] Improved memcache connection procedure from release-0.6; use call_user_func to trigger session gc handlers

---
 program/include/rcmail.php        |   31 ++++++++++++++++++++++++++-----
 program/include/rcube_session.php |    2 +-
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 7967f94..f126a91 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -337,21 +337,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);
+    }
+  }
 
 
   /**
diff --git a/program/include/rcube_session.php b/program/include/rcube_session.php
index 0a67480..3986c89 100644
--- a/program/include/rcube_session.php
+++ b/program/include/rcube_session.php
@@ -314,7 +314,7 @@
   public function gc()
   {
     foreach ($this->gc_handlers as $fct)
-      $fct();
+      call_user_func($fct);
   }
 
 

--
Gitblit v1.9.1