Skip to content

Instantly share code, notes, and snippets.

/ndn.diff Secret

Created November 4, 2014 17:56
Show Gist options
  • Save anonymous/0617eda395b430195f55 to your computer and use it in GitHub Desktop.
Save anonymous/0617eda395b430195f55 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/IOLoop/Client.pm b/lib/Mojo/IOLoop/Client.pm
index 9f3c9cb..cef3c8e 100644
--- a/lib/Mojo/IOLoop/Client.pm
+++ b/lib/Mojo/IOLoop/Client.pm
@@ -11,7 +11,7 @@ use Socket qw(IPPROTO_TCP SO_ERROR TCP_NODELAY);
use constant NDN => $ENV{MOJO_NO_NDN}
? 0
: eval 'use Net::DNS::Native 0.10 (); 1';
-my $NDN = NDN ? Net::DNS::Native->new : undef;
+my ($NDN, $PID);
# TLS support requires IO::Socket::SSL
use constant TLS => $ENV{MOJO_NO_TLS}
@@ -49,13 +49,13 @@ sub connect {
# Non-blocking name resolution
my $handle = $self->{dns}
- = $NDN->getaddrinfo($address, _port($args), {protocol => IPPROTO_TCP});
+ = _ndn()->getaddrinfo($address, _port($args), {protocol => IPPROTO_TCP});
$reactor->io(
$handle => sub {
my $reactor = shift;
$reactor->remove($self->{dns});
- my ($err, @res) = $NDN->get_result(delete $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;
@@ -67,7 +67,7 @@ sub connect {
sub _cleanup {
my $self = shift;
return $self unless my $reactor = $self->reactor;
- $NDN->timedout($self->{dns}) if $self->{dns};
+ _ndn()->timedout($self->{dns}) if $self->{dns};
$self->{$_} && $reactor->remove(delete $self->{$_}) for qw(dns timer handle);
return $self;
}
@@ -96,6 +96,13 @@ sub _connect {
->watch($handle, 0, 1);
}
+sub _ndn {
+ return $NDN if $NDN && $PID == $$;
+ warn "NEW!\n";
+ ($NDN, $PID) = (Net::DNS::Native->new(pool => 5, extra_thread => 1), $$);
+ return $NDN;
+}
+
sub _port { $_[0]->{socks_port} || $_[0]->{port} || ($_[0]->{tls} ? 443 : 80) }
sub _ready {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment