Skip to content

Instantly share code, notes, and snippets.

/connect.diff Secret

Created September 15, 2015 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c4a19aeb1c5cdbedc2c2 to your computer and use it in GitHub Desktop.
Save anonymous/c4a19aeb1c5cdbedc2c2 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/IOLoop/Client.pm b/lib/Mojo/IOLoop/Client.pm
index 85b504c..4db1b96 100644
--- a/lib/Mojo/IOLoop/Client.pm
+++ b/lib/Mojo/IOLoop/Client.pm
@@ -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->_watch($handle, $args);
}
sub _port { $_[0]{socks_port} || $_[0]{port} || ($_[0]{tls} ? 443 : 80) }
@@ -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->_watch($handle, $args);
+ return $! == EINPROGRESS ? undef : $self->emit(error => $!) unless $res;
+ }
+
return $self->emit(error => $! || 'Not connected') unless $handle->connected;
# Disable Nagle's algorithm
@@ -186,6 +190,13 @@ sub _try_tls {
$reactor->io($handle => sub { $self->_tls })->watch($handle, 0, 1);
}
+sub _watch {
+ my ($self, $handle, $args) = @_;
+ weaken $self;
+ $self->reactor->io($handle => sub { $self->_ready($args) })
+ ->watch($handle, 0, 1);
+}
+
1;
=encoding utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment