Skip to content

Commit

Permalink
improved Mojo::Reactor::EV to update the current time before starting…
Browse files Browse the repository at this point in the history
… a timer if EV is not running already
  • Loading branch information
kraih committed Jan 9, 2015
1 parent 2628b2a commit 8aefa99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Changes
@@ -1,8 +1,8 @@

5.72 2015-01-09
- Improved Mojo::Reactor::EV performance slightly.
- Improved Mojo::IOLoop::Client not to start timers before the event loop
is running.
- Improved Mojo::Reactor::EV to update the current time before starting a
timer if EV is not running already.

5.71 2015-01-01
- Updated Net::DNS::Native requirement to 0.15 for some important bug fixes.
Expand Down
59 changes: 27 additions & 32 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -33,8 +33,34 @@ sub DESTROY { shift->_cleanup }

sub connect {
my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});

# Timeout
weaken $self;
$self->reactor->next_tick(sub { $self && $self->_resolve($args) });
my $reactor = $self->reactor;
$self->{timer} = $reactor->timer($args->{timeout} || 10,
sub { $self->emit(error => 'Connect timeout') });

# Blocking name resolution
$_ && s/[[\]]//g for @$args{qw(address socks_address)};
my $address = $args->{socks_address} || ($args->{address} ||= '127.0.0.1');
return $reactor->next_tick(sub { $self && $self->_connect($args) })
if !NDN || $args->{handle};

# Non-blocking name resolution
my $handle = $self->{dns} = $NDN->getaddrinfo($address, _port($args),
{protocol => IPPROTO_TCP, socktype => SOCK_STREAM});
$reactor->io(
$handle => sub {
my $reactor = shift;

$reactor->remove($self->{dns});
my ($err, @res) = $NDN->get_result(delete $self->{dns});
return $self->emit(error => "Can't resolve: $err") if $err;

$args->{addr_info} = \@res;
$self->_connect($args);
}
)->watch($handle, 1, 0);
}

sub _cleanup {
Expand Down Expand Up @@ -83,37 +109,6 @@ sub _ready {
$self->_try_socks($args);
}

sub _resolve {
my ($self, $args) = @_;

# Timeout
weaken $self;
my $reactor = $self->reactor;
$self->{timer} = $reactor->timer($args->{timeout} || 10,
sub { $self->emit(error => 'Connect timeout') });

# Blocking name resolution
$_ && s/[[\]]//g for @$args{qw(address socks_address)};
my $address = $args->{socks_address} || ($args->{address} ||= '127.0.0.1');
return $self->_connect($args) if !NDN || $args->{handle};

# Non-blocking name resolution
my $handle = $self->{dns} = $NDN->getaddrinfo($address, _port($args),
{protocol => IPPROTO_TCP, socktype => SOCK_STREAM});
$reactor->io(
$handle => sub {
my $reactor = shift;

$reactor->remove($self->{dns});
my ($err, @res) = $NDN->get_result(delete $self->{dns});
return $self->emit(error => "Can't resolve: $err") if $err;

$args->{addr_info} = \@res;
$self->_connect($args);
}
)->watch($handle, 1, 0);
}

sub _socks {
my ($self, $args) = @_;

Expand Down
3 changes: 3 additions & 0 deletions lib/Mojo/Reactor/EV.pm
Expand Up @@ -58,6 +58,9 @@ sub _timer {
my ($self, $recurring, $after, $cb) = @_;
$after ||= 0.0001 if $recurring;

# Update current time if EV is not running already
EV::now_update() unless $self->is_running;

my $id = $self->SUPER::_timer(0, 0, $cb);
weaken $self;
$self->{timers}{$id}{watcher} = EV::timer(
Expand Down

0 comments on commit 8aefa99

Please sign in to comment.