Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This may work (I haven't tested it):

use Time::HiRes qw( ualarm tv_interval gettimeofday ); my $ua = LWP::UserAgent->new; my $fastest = undef; MIRROR: foreach my $mirror ( @mirrors ) { my $url = "$mirror/file.tar.bz2"; my $request = HTTP::Request->new(HEAD => $url); # if we have a possible fastest mirror, set an alarm ualarm( $fastest->{response_time} * 1_000_000 ) if defined $fastest; # attempt to fetch from this mirror in less time # than the fastest so far my ( $start_time, $response ); eval { local $SIG{ALRM} = sub { die 'alarm' }; $start_time = [gettimeofday]; $response = $ua->request($request); ualarm( 0 ); }; # if the alarm went off (or some other error), # try next mirror next MIRROR if $@; my $response_time = tv_interval( $start_time ); my $status = $response->status_line; # if this was successful, if ( $status == 200 ) { # if we haven't found a fast mirror yet, # or this one's faster, if ( ! defined $fastest || $fastest->{response_time} > $response_time ) { # store this one as the fastest $fastest = { mirror => $mirror, response_time => $response_time }; } } }

What's nice about this:

  • No forking.
  • If I've found a live site, I never wait for a dead one. In fact, I never wait for a site slower than the fastest one I've seen.
  • The $fastest that I find is a hash ref, so I can stash more info (such as the actual response) in there later, if I want.

I don't know how netselect works, so I can't speak to how well this works by comparison. I just hope to give you a good starting point.

In reply to Re: Selecting the "fastest" server listed in an array by kyle
in thread Selecting the "fastest" server listed in an array by hacker

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2020-11-25 02:44 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found