Skip to content

Commit

Permalink
improve support for epoll/kqueue in Mojo::IOLoop::Client
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 15, 2015
1 parent a4fe6ff commit 2b8e730
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

6.20 2015-09-13
6.20 2015-09-15
- Improved support for epoll/kqueue in Mojo::IOLoop::Client.

6.19 2015-09-12
- Added code of conduct to Mojolicious::Guides::Contributing.
Expand Down
23 changes: 17 additions & 6 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -86,10 +86,7 @@ sub _connect {
}
$handle->blocking(0);

# Wait for handle to become writable
weaken $self;
$self->reactor->io($handle => sub { $self->_ready($args) })
->watch($handle, 0, 1);
$self->_wait($handle, $args);
}

sub _port { $_[0]{socks_port} || $_[0]{port} || ($_[0]{tls} ? 443 : 80) }
Expand All @@ -99,8 +96,15 @@ sub _ready {

# Retry or handle exceptions
my $handle = $self->{handle};
return $! == EINPROGRESS ? undef : $self->emit(error => $!)
if $handle->isa('IO::Socket::IP') && !$handle->connect;

# Handle changes in between attempts and needs to be re-added for epoll/kqueue
if ($handle->isa('IO::Socket::IP')) {
$self->reactor->remove($handle);
my $res = $handle->connect;
$self->_wait($handle, $args);
return $! == EINPROGRESS ? undef : $self->emit(error => $!) unless $res;
}

return $self->emit(error => $! || 'Not connected') unless $handle->connected;

# Disable Nagle's algorithm
Expand Down Expand Up @@ -186,6 +190,13 @@ sub _try_tls {
$reactor->io($handle => sub { $self->_tls })->watch($handle, 0, 1);
}

sub _wait {
my ($self, $handle, $args) = @_;
weaken $self;
$self->reactor->io($handle => sub { $self->_ready($args) })
->watch($handle, 0, 1);
}

1;

=encoding utf8
Expand Down

0 comments on commit 2b8e730

Please sign in to comment.