Skip to content

Commit

Permalink
Check if we have CLOCK_MONOTONIC at compile-time
Browse files Browse the repository at this point in the history
This avoids a subroutine call and also allows us to easily test the code
that doesn't depend on CLOCK_MONOTONIC with
HIJK_TEST_NO_CLOCK_MONOTONIC=1, e.g.:

    HIJK_TEST_NO_CLOCK_MONOTONIC=1 TEST_LIVE=1 prove -Ilib -j 10 -r  t
    HIJK_TEST_NO_CLOCK_MONOTONIC=0 TEST_LIVE=1 prove -Ilib -j 10 -r  t
  • Loading branch information
avar committed Mar 17, 2014
1 parent 2484f25 commit 7c1c31d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/Hijk.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ use warnings;
use POSIX qw(:errno_h);
use Socket qw(PF_INET SOCK_STREAM pack_sockaddr_in inet_ntoa $CRLF);
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
use Time::HiRes qw(time);
my $CLOCK = undef;
eval {
Time::HiRes::clock_gettime(Time::HiRes::CLOCK_MONOTONIC());
$CLOCK = sub { Time::HiRes::clock_gettime(Time::HiRes::CLOCK_MONOTONIC()) };
};
use Time::HiRes qw(time clock_gettime);
use constant ();
BEGIN {
my $has_CLOCK_MONOTONIC;
eval {
die "disabled for testing" if $ENV{HIJK_TEST_NO_CLOCK_MONOTONIC};
Time::HiRes->import(qw(CLOCK_MONOTONIC));
$has_CLOCK_MONOTONIC = 1;
};
constant->import(HAS_CLOCK_MONOTONIC => $has_CLOCK_MONOTONIC ? 1 : 0);
}

our $VERSION = "0.13";

Expand All @@ -30,11 +35,11 @@ sub read_http_message {
my ($body,$buf,$decapitated,$nbytes,$proto);
my $status_code = 0;
my $start = undef;
$start = $CLOCK->() if $CLOCK && defined($read_timeout);
$start = clock_gettime(CLOCK_MONOTONIC()) if HAS_CLOCK_MONOTONIC && defined($read_timeout);
vec(my $rin = '', $fd, 1) = 1;
do {
my $nfound = select($rin, undef, undef, $read_timeout);
$read_timeout -= $CLOCK->() - $start if defined($start);
$read_timeout -= clock_gettime(CLOCK_MONOTONIC()) - $start if defined($start);

return (undef,0,undef,undef, Hijk::Error::READ_TIMEOUT)
if ($nfound != 1 || (defined($read_timeout) && $read_timeout <= 0));
Expand Down Expand Up @@ -158,12 +163,11 @@ sub request {

vec(my $rout = '', fileno($soc), 1) = 1;
my $connect_timeout = selectable_timeout($args->{connect_timeout});
my $start = undef;
$start = $CLOCK->() if $CLOCK && defined($connect_timeout);
my $start; $start = clock_gettime(CLOCK_MONOTONIC()) if HAS_CLOCK_MONOTONIC && defined($connect_timeout);

while ($left > 0) {
my $nfound = select(undef,$rout, undef, $connect_timeout);
$connect_timeout -= $CLOCK->() - $start if defined($start);
$connect_timeout -= clock_gettime(CLOCK_MONOTONIC()) - $start if HAS_CLOCK_MONOTONIC and defined($start);

if ($nfound != 1 || (defined($connect_timeout) && $connect_timeout <= 0)) {
delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Expand Down

0 comments on commit 7c1c31d

Please sign in to comment.