From 1b7bfb5a880ea81dd32acb29cdb391229b50bb6f Mon Sep 17 00:00:00 2001 From: petter Date: Fri, 20 Feb 2009 13:45:35 +0000 Subject: [PATCH] Open the socket non-blocking so we can time out if the management console doesn't print a banner git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3793 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-tools/webgui/Varnish/Management.pm | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/varnish-tools/webgui/Varnish/Management.pm b/varnish-tools/webgui/Varnish/Management.pm index 9e3b2dc3..455b1d09 100644 --- a/varnish-tools/webgui/Varnish/Management.pm +++ b/varnish-tools/webgui/Varnish/Management.pm @@ -2,6 +2,8 @@ package Varnish::Management; 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 @@ use Varnish::Util qw(set_error get_error no_error); 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 @@ use Varnish::Util qw(set_error get_error no_error); 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}; -- 2.39.5