Hey guys...

What I'm attempting below is to protect against:

  a) long connects to a remote site (through Timeout=>)
  b) slow network response with alarm()'s.

Unfortunately, I'm noticing:

  a) the alarms only seem to work some of the times.
  b) io::socket is noisy when it comes to being interuppted.

My code is thus (with an alarm of 1 second to force timeouts):

      # our socket holder.
      my $socket;

      # do the actual call in a eval loop so that we can
      # time out the operation within 30 seconds of nothing.
      eval {

         # install an alarm handler.
         local $SIG{ALRM} = sub { die "timeout"; };

         # set the alarm to timeout slow networks.
         alarm 1;

         # create our socket. we return if we can't.
         $socket = IO::Socket::INET->new(PeerAddr => $host,
                                         PeerPort => $port,
                                         Proto => 'tcp',
                                         Timeout => 15) or return 0;
      };

      # unset alarm and return if timed out.
      alarm 0; if ($@) { return 0; }

I'll receive this as my output (the above being a small part of a large app):

  Downloading applesurf.xml - local copy doesn't exist.
  Downloading slashdotnewsfornerds.xml - local copy doesn't exist.
  # IO::Socket::INET: Interrupted system call
  File ':./lib::lib:WWW.pl'; Line 319
  Downloading themotleyfool.xml - local copy doesn't exist.
  # IO::Socket::INET: Interrupted system call
  File ':./lib::lib:WWW.pl'; Line 319
  Downloading tomalaksrealm.xml - local copy doesn't exist.
  Downloading webreferencenews.xml - local copy doesn't exist.
  # IO::Socket::INET: Interrupted system call
  File ':./lib::lib:WWW.pl'; Line 319
  Downloading wirednews.xml - local copy doesn't exist.

WWW.pl is the name of the library the code above is located in, and line
319 is the actual IO::Socket call shown above. As is typical when sending
results to a list, this time, it looks like the alarms worked decently [but
please double check the code for errors.]

So, my first question is: is my code correct? Is there anything that could
cause me seeing some alarms working, and others not, as my "tests before
sending it off to the list" showed?

And the second question is: how can I shut up the IO::Socket errors? Should
I eval the IO::Socket line, within the outer eval? Isn't that weird?

-- 
Morbus Iff

             Here we have MorbusBird - Able to fly across all boundaries,
    ^         migrate his way unfailingly into every inbox and browser
  /( )\      across the land, with the freedom to (anywhere he wants to!)
   /.\      drop his shat... Devil Shat:<http://www.disobey.com/devilshat/>

-06--- <\/> ---- <http://www.disobey.com/> --- Bad Ascii, Short Notice ----

Reply via email to