[LON-CAPA-cvs] cvs: loncom /lonnet/perl lonnet.pm

foxr lon-capa-cvs@mail.lon-capa.org
Wed, 06 Oct 2004 09:48:39 -0000


foxr		Wed Oct  6 05:48:39 2004 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  Add connection retries to lonc for up to 10 seconds/10 times (1 retry/sec).
  This may compensate for short lonc outages, but probably is not strictly needed.
  The retry count can be tuned via $max_connection_retries at the top of the file.
  I'm not sure this warrants a configuration entry in loncapa's config files,
  if so, by all means go for it.
  
  
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.549 loncom/lonnet/perl/lonnet.pm:1.550
--- loncom/lonnet/perl/lonnet.pm:1.549	Tue Oct  5 07:24:34 2004
+++ loncom/lonnet/perl/lonnet.pm	Wed Oct  6 05:48:39 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.549 2004/10/05 11:24:34 foxr Exp $
+# $Id: lonnet.pm,v 1.550 2004/10/06 09:48:39 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -52,6 +52,7 @@
 use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw);
 use Time::HiRes qw( gettimeofday tv_interval );
 my $readit;
+my $max_connection_retries = 10;     # Or some such value.
 
 =pod
 
@@ -126,16 +127,30 @@
 	sleep(1);
     }
     # At this point, either a loncnew parent is listening or an old lonc
-    # or loncnew child is listening so we can connect.
+    # or loncnew child is listening so we can connect or everything's dead.
     #
-    my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
-                                     Type    => SOCK_STREAM,
-                                     Timeout => 10)
-       or return "con_lost";
-    print $client "$cmd\n";
-    my $answer=<$client>;
-    if (!$answer) { $answer="con_lost"; }
-    chomp($answer);
+    #   We'll give the connection a few tries before abandoning it.  If
+    #   connection is not possible, we'll con_lost back to the client.
+    #   
+    my $client;
+    for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
+	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
+				      Type    => SOCK_STREAM,
+				      Timeout => 10);
+	if($client) {
+	    last;		# Connected!
+	}
+	sleep(1);		# Try again later if failed connection.
+    }
+    my $answer;
+    if ($client) {
+	print $client "$cmd\n";
+	$answer=<$client>;
+	if (!$answer) { $answer="con_lost"; }
+	chomp($answer);
+    } else {
+	$answer = 'con_lost';	# Failed connection.
+    }
     return $answer;
 }