Delay_Management class added to avoid wrong timeout after temporarly server hungs
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Tue, 8 Jan 2013 18:23:29 +0000 (19:23 +0100)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Tue, 8 Jan 2013 18:24:13 +0000 (19:24 +0100)
web/Obj/brisk.phh
web/briskin5/Obj/briskin5.phh

index 5f87709..caf9727 100644 (file)
@@ -58,7 +58,7 @@ define('EXPIRE_TIME_WAG', 10);
 define('WAKEUP_TIME', 12);
 // BAN_TIME da allineare anche in commons.js
 define('BAN_TIME', 3600);
-define('GARBAGE_TIMEOUT', 10);
+define('GARBAGE_TIMEOUT', 5);
 define('NICKSERV', "<i>BriskServ</i>");
 
 define('LOCK_SHARE_MAX', 10000);
@@ -646,7 +646,54 @@ class Table {
 } // end class Table
 
 
+class Delay_Manager
+{
+    var $delta;
+    var $lastckeck;
+    var $triglevel;
+
+    function Delay_Manager($triglevel)
+    {
+        $this->triglevel = $triglevel;
+        $this->delta = array();
+        $this->lastcheck = 0;
+    }
+
+    function delta_get($curtime)
+    {
+        // clean too old delta items
+        for ($i = 0 ; $i < count($this->delta) ; $i++) {
+            if ($this->delta[$i][0] < $curtime) {
+                array_splice($this->delta, $i, 1);
+                $i--;
+            }
+        }
+
+        // add new delta items if delay exceeded $this->triglevel sec
+        if ($curtime > $this->lastcheck + $this->triglevel && $curtime < $this->lastcheck + 1200.0) {
+            $delta = $curtime - $this->lastcheck - $this->triglevel;
+            array_push($this->delta, array($curtime + $delta , $delta));
+            // fprintf(STDERR, "DELTA: add new delta [%f] [%f] [%f]\n", $this->triglevel, $curtime + $delta, $delta);
+        }
+
+        // extract the maximum valid delta
+        $delta_max = 0.0;
+        for ($i = 0 ; $i < count($this->delta) ; $i++) {
+            $delta_cur = $this->delta[$i][1];
+            if ($delta_max < $delta_cur)
+                $delta_max = $delta_cur;
+        }
 
+        // fprintf(STDERR, "DELTA: status %d, delta_max: %f\n", count($this->delta), $delta_max);
+
+        return ($delta_max);
+    }
+
+    function lastcheck_set($curtime)
+    {
+        $this->lastcheck = $curtime;
+    }
+}
 
 class Room
 {
@@ -661,6 +708,8 @@ class Room
     var $garbage_timeout;
     var $shm_sz;
     
+    var $delay_mgr;
+
     function Room ($crystal_filename) {
         $this->crystal_filename = $crystal_filename;
         $this->user  = array();
@@ -690,6 +739,8 @@ class Room
         }
         $this->garbage_timeout = 0;
         $this->shm_sz = SHM_DIMS_MIN;
+
+        $this->delay_mgr = new Delay_Manager(1.5);
     }
 
   function garbage_manager($force)
@@ -701,9 +752,12 @@ class Room
     log_rd2("garbage_manager START");
 
     /* Garbage collector degli utenti in timeout */
-    $curtime = time();
+    $curtime = microtime(TRUE);
+
+    $delta = $this->delay_mgr->delta_get($curtime);
 
     if (!$force && !($this->garbage_timeout < $curtime)) {
+        $this->delay_mgr->lastcheck_set($curtime);
         return ($ismod);
     }
       
@@ -727,7 +781,6 @@ class Room
                     $bri = FALSE;
                 }
 
-
                 if ($bri != FALSE) {
                     //
                     //  SPAWN: JOIN
@@ -818,9 +871,9 @@ class Room
        if ($user_cur->sess == "") 
             continue;
        
-       if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
+       if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) {
             // Auto logout dell'utente
-            log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
+            log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime - delta ".($curtime - $delta));
             
             if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
                 log_auth($user_cur->sess, "Autologout session.");
@@ -837,7 +890,7 @@ class Room
             }
        }
 
-       if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
+       if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < ($curtime - $delta)) { // lo rimettiamo in piedi
             if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
                 $this->room_wakeup($user_cur);
                 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
@@ -852,6 +905,7 @@ class Room
     $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
     $ismod = TRUE;
 
+    $this->delay_mgr->lastcheck_set($curtime);
     return ($ismod);
   }
 
@@ -1861,6 +1915,7 @@ class Room
       }
       else {
           fprintf(STDERR, "ROOM FROM FILE\n");
+          rename($crystal_filename, $crystal_filename.".old");
       }
 
     return $room;
index 11c1d5b..354637a 100644 (file)
@@ -801,6 +801,8 @@ class Bin5 {
     var $the_end;
     var $tok;
 
+    var $delay_mgr;
+
     static function page_manager($room, $header_out, $path, $method, $addr, $get, $post, $cookie) 
     {
         switch ($path) {
@@ -852,6 +854,8 @@ class Bin5 {
         $this->table_token = $table_token;
         $this->garbage_timeout = 0;
         
+        $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
+
         log_wr("Bin5 constructor end");
     }
     
@@ -888,6 +892,9 @@ class Bin5 {
         /* Garbage collector degli utenti in timeout */
         $ismod = FALSE;
         $curtime = time();
+
+        $delta = $this->delay_mgr->delta_get($curtime);
+
         if ($force || $this->garbage_timeout < $curtime) {
             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
                 $user_cur = $this->user[$i];
@@ -895,7 +902,7 @@ class Bin5 {
                     ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
                     continue;
                 
-                if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
+                if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
                     log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
                     
                     if ($user_cur->stat == 'table') {
@@ -931,6 +938,7 @@ class Bin5 {
             $ismod = TRUE;
         }
         
+        $this->delay_mgr->lastcheck_set($curtime);
         return ($ismod);
     }