develooper Front page | perl.libwww | Postings from April 2001

Timeout

From:
Ron.W.Flolid
Date:
April 26, 2001 07:27
Subject:
Timeout
Message ID:
OF518F279E.7DEF3780-ON86256A3A.004E0582@deluxe.com
I would like to use an alarm to detect if a Web server is failing. From my
experience with LWP, the time-out value supplied to the  UserAgent pertains
to the TCP time-out values. I have servers that have exceptionally long
disconnect times, (e.g., service disconnects in excess of 15 minutes), so
my HTTP requests will hang for 15+ minutes waiting for the response as a
connection has been made to the server. I setup an alarm before I invoke
the request module, but the alarm function doesn't appear to be working.
Any suggestions on how to detect this sort of situation?

In a test that I ran with the code below I set the timeout value to 60 and
set my alarm duration  at 1 second.  I then created a situation were the
HTTP server would hang on the request and the disconnect timer would be
exercised, (i.e., wait the 60 seconds). I expected that the alarm would
fail the script as I had set the alarm duration to 1 second. Why didn't the
alarm fail the script?

Code with alarm:

################################################################################
#  Check URL subroutine
#     --> Pass the URL to check in with $raw_url
#         Return ResponseTimeMeasured
#         Return ResponseErrorLine if error
#         URL Failures: 500 Server Error
#                       4xx Client Error
#                             404 and other  Allowed with content
#         Note: Need to check for 0 count on the return becuase a redirect
#               will cause an error. For 500 errors 0 count will fail.
################################################################################
sub CheckURL{
  $SIG{ALRM} = sub {die "timeout"};                # setup the Alarm
handler
  eval {
     #alarm($URLTimeoutFile{$raw_url} + 5);        # Timeout will be 5
seconds longer than TCP timout
     $TestTime=$URLTimeoutFile{$raw_url} + 5;      # Test TEST value
     print "Timeout value for hung server $raw_url : $TestTime \n";
     alarm(1);                     # TEST  - alarm should be killed within
1 second
     print "End of the routine - TEST \n";

     $ContentFailure="";                 # Content of failed URL
     if   ($raw_url =~ /https:\/\//){
          $SSLType=1;                    # Secured request
     } elsif ($raw_url =~ /http:\/\//){
          $SSLType=0;                    # Non-secured request
     }

          my   $ua = new  LWP::UserAgent;
     $ua->agent("HTTPCheckFail Monitor v1.0");     # Name the agent

     $ua->timeout($URLTimeoutFile{$raw_url});      # Setup the timeout
value
     #$ua->timeout($DefaultTimeOut);          # Setup the timeout value

     if ($SSLType){                      # Secured transaction
          use LWP::Protocol::https;
     } else {                      #  HTTP Transactions
          use LWP::Protocol::http;
     }
     my   $req = new HTTP::Request('GET',$raw_url);
     my      $res = $ua->request($req);


     my $content = $res->content();
          my $status = $res->status_line();
          my $code = $res->code();


     @TempMessage=split(/\,/,$status);        # Clean up the message
     if ($#TempMessage < 0) {
          $ResponseErrorLine=$status;         # Formatted error status if
error seen
     } else {
          $ResponseErrorLine=$TempMessage[0];      # Will remove beyond ,
          $status=$TempMessage[0];
     }

     $StatusCode="Status-$status Code-$code";      # Save combined status &
code
     $ResponseErrorLine=$StatusCode;               # Save the Response
Error Line

          my $bytes = length $content;
          my $count = ($content =~ tr/\n/\n/);          # Count the CR or
lines

     if ($code =~ /^(4..|5..)/ || $count==0) {     # ONLY allow HTTP
Response 2xx 3xx  or  content
          #@ResponseLine=split("/,/",(sprintf "%s",
$response->status_line));# Format the Line
          #$ResponseErrorLine=$ResponseLine[0];    # Format the line for
the message
          $NoResponse=1;                 # Initialize the NO Response
Switch
     }

     $KeyWordFailure=0;                  # Clear the key word failure
switch
     unless ($NoResponse){                    # Only check if content
returned for valid
          if ($KeyWordFile{$raw_url} !~/NO KEYWORD/){   # Check for keyword
if neeed
               if ($content !~ /$KeyWordFile{$raw_url}/i){
                    $ContentFailure=$content;      # Save for message
                    $KeyWordFailure=1;        # Signal the keyword failure
                    $NoResponse=1;            # Initialize the NO Response
Switch
                                   # New message for keyword faiure
                    $ResponseErrorLine="Keyword Failure($KeyWordFile
{$raw_url})".$ResponseErrorLine;
               }
          }
     }

     if ($Debug) {  # Testing Option
          print OUTfile "\n\nNEW URL CHECK STATUS\n####--$raw_url--#####\n
";
          print OUTfile "\t->>$raw_url<<-  \n ";
          print OUTfile  "\tRESPONSE: \n ";
          print OUTfile  "\tSTATUS CODE:\n ";
          print OUTfile  "$StatusCode\n ";
          print OUTfile  "\tCONTENT\n ";
          print OUTfile  "$content\n ";
          print OUTfile "####--$raw_url--#####\n ";
     }
     print "End of the routine - TEST \n";
        alarm(0);                        # reset the alarm
       };    # End the routine that is timed _ Note the semicolon following
the brace
       #  Alarm check routine
       if ($@) {                         # Check if anything in the eval
return
          if ($@ =~ /timeout/ ){
               print "timeout exceeded the internal timer plus 5 seconds
\n";
               print LOGfile "timeout exceeded the internal timer plus 5
seconds \n";
          } else {
               alarm(0);           # Reset the timer
               print "timeout NOT seen but eval return positive contents
($@)\n";
               print LOGfile"timeout NOT seen but eval return positive
contents ($@)\n";
          }
     }                             # End of the check eval routine


}   # End of CheckURL subroutine





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About