* Replaced the two-second sleep (and associated race condition) for the
authorArt Cancro <ajc@citadel.org>
Fri, 21 Nov 2003 18:22:19 +0000 (18:22 +0000)
committerArt Cancro <ajc@citadel.org>
Fri, 21 Nov 2003 18:22:19 +0000 (18:22 +0000)
  session proxy to start, with a loop that attempts connection ten times
  a second for ten seconds, resulting in faster, more reliable startup.

ctdlphp/ChangeLog
ctdlphp/ctdlsession.php

index 9ed300cad15b965020ed458c376e9c330e0ca98c..6155da42b793b77d5903ddf46703b26e56f013e3 100644 (file)
@@ -1,4 +1,9 @@
  $Log$
+ Revision 1.19  2003/11/21 18:22:19  ajc
+ * Replaced the two-second sleep (and associated race condition) for the
+   session proxy to start, with a loop that attempts connection ten times
+   a second for ten seconds, resulting in faster, more reliable startup.
+
  Revision 1.18  2003/11/18 04:58:34  ajc
  * Added ctdlelements.php ... this is to be used for functions which fetch
    Citadel protocol data and convert it to insertable HTML.
@@ -78,3 +83,4 @@
 
  Revision 1.1  2003/10/31 03:47:13  ajc
  * Initial CVS import
+
index f2ac6f5237f052c468bd58766c7beb82ad993857..a120094023eac2a073c69cf551af9421d0006bfa 100644 (file)
@@ -47,40 +47,42 @@ function establish_citadel_session() {
                        " </dev/null >/dev/null 2>&1 " .
                        " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
                exec($cmd);
-               sleep(2);
 
-               // Ok, now try again.
-               $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
+               // Keep attempting connections 10 times per second up to 100 times
+               $attempts = 0;
+               while (!$clientsocket) {
+                       usleep(100);
+                       $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
+                       $attempts += 1;
+                       if ($attempts > 100) {
+                               echo "ERROR: unable to start connection proxy. ";
+                               echo "Please contact your system administrator.<BR>\n";
+                               flush();
+                               exit(1);
+                       }
+               }
 
-               // Try to log the user back in and go back to the correct room.
-               if ($clientsocket) {
+               // At this point we have a good connection to Citadel.
 
-                       ctdl_iden();    // Identify client
+               ctdl_iden();    // Identify client
 
-                       if ($_SESSION["username"]) {
-                               login_existing_user(
-                                       $_SESSION["username"],
-                                       $_SESSION["password"]
-                               );
-                       }
-
-                       if ($_SESSION["room"]) {
-                               ctdl_goto($_SESSION["room"]);
-                       }
-                       else {
-                               ctdl_goto("_BASEROOM_");
-                       }
+               if ($_SESSION["username"]) {
+                       login_existing_user(
+                               $_SESSION["username"],
+                               $_SESSION["password"]
+                       );
                }
-       }
 
-       if ($clientsocket) {
-               if (!$_SESSION["serv_humannode"]) {
-                       ctdl_get_serv_info();
+               if ($_SESSION["room"]) {
+                       ctdl_goto($_SESSION["room"]);
+               }
+               else {
+                       ctdl_goto("_BASEROOM_");
                }
        }
-       else {
-               echo "ERROR: no Citadel socket!<BR>\n";
-               flush();
+
+       if (!$_SESSION["serv_humannode"]) {
+               ctdl_get_serv_info();
        }
 
        // If the user is trying to call up any page other than