r3793 - trunk/varnish-tools/webgui/Varnish

petter at projects.linpro.no petter at projects.linpro.no
Fri Feb 20 14:45:36 CET 2009


Author: petter
Date: 2009-02-20 14:45:35 +0100 (Fri, 20 Feb 2009)
New Revision: 3793

Modified:
   trunk/varnish-tools/webgui/Varnish/Management.pm
Log:
Open the socket non-blocking so we can time out if the management console doesn't print a banner


Modified: trunk/varnish-tools/webgui/Varnish/Management.pm
===================================================================
--- trunk/varnish-tools/webgui/Varnish/Management.pm	2009-02-20 11:45:30 UTC (rev 3792)
+++ trunk/varnish-tools/webgui/Varnish/Management.pm	2009-02-20 13:45:35 UTC (rev 3793)
@@ -2,6 +2,8 @@
 
 use strict;
 use IO::Socket::INET;
+use IO::Select;
+use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
 use Exporter;
 use List::Util qw(first);
 use Varnish::Util qw(set_error get_error no_error);
@@ -24,8 +26,11 @@
 
 	sub _read_cli_response {
 		my ($socket) = @_;
+		
+		my $status_line = <$socket>;
+		return (undef, undef) if !defined($status_line);
+		my ($status_code, $response_size) = $status_line =~ m/^(\d+) (\d+)/;
 
-		my ($status_code, $response_size) = <$socket> =~ m/^(\d+) (\d+)/;
 		my $response;
 		my $remaining_bytes = $response_size;
 		while ($remaining_bytes > 0 ) {
@@ -46,11 +51,21 @@
 			my $socket = new IO::Socket::INET->new(
 					PeerPort => $port_of{$self},
 					Proto	 => 'tcp',
-					PeerAddr => $hostname_of{$self}
+					PeerAddr => $hostname_of{$self},
+					Blocking => 0,
+
 					);
 			return ("666", "Could not connect to node") if (!$socket);
-# skip the banner
-			_read_cli_response($socket);
+
+			my $select = IO::Select->new();
+			$select->add($socket);
+			# wait 100ms, tops, before assuming we don't get a banner
+			if ($select->can_read(0.1)) {
+				_read_cli_response($socket);
+			}
+			my $flags = fcntl($socket, F_GETFL, 0);
+			$flags = fcntl($socket, F_SETFL, $flags & ~O_NONBLOCK);
+
 			$socket_of{$self} = $socket;
 		}
 		my $socket = $socket_of{$self};



More information about the varnish-commit mailing list