Skip to content

Commit

Permalink
added experimental hosts attribute to Mojo::IOLoop::Resolver and fixe…
Browse files Browse the repository at this point in the history
…d many small memory leaks
  • Loading branch information
kraih committed Nov 16, 2011
1 parent 05e6e80 commit 550273a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -10,12 +10,14 @@ This file documents the revision history for Perl extension Mojolicious.
- Deprecated on_* methods in Mojo::IOLoop.
- Replaced servers method in Mojo::IOLoop::Resolver with servers
attribute.
- Added EXPERIMENTAL hosts attribute to Mojo::IOLoop::Resolver.
- Added EXPERIMENTAL is_readable method to Mojo::IOLoop::Stream.
- Added EXPERIMENTAL charset method to Mojo::Content.
- Added EXPERIMENTAL write event to Mojo::IOLoop::Stream.
- Added EXPERIMENTAL connection event to Mojo::Transaction.
- Improved documentation.
- Improved CSS of some built-in templates.
- Fixed many small memory leaks.
- Fixed multiple drain callback bugs.
- Fixed small attribute selector bug in Mojo::DOM::CSS. (tladesignz)

Expand Down
1 change: 1 addition & 0 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -311,6 +311,7 @@ sub stream {
weaken $stream->{iowatcher};

# Events
weaken $self;
$stream->on(close => sub { $self->{connections}->{$id}->{finish} = 1 });
$stream->on(error => sub { $self->{connections}->{$id}->{finish} = 1 });
$stream->on(read => sub { $self->{connections}->{$id}->{active} = time });
Expand Down
23 changes: 10 additions & 13 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -32,20 +32,17 @@ sub connect {
my $args = ref $_[0] ? $_[0] : {@_};
$args->{address} ||= 'localhost';

# Lookup
if (!$args->{handle} && (my $address = $args->{address})) {
$self->resolver->lookup(
$address => sub {
$args->{address} = $_[1] || $args->{address};
$self->_connect($args);
}
);
}

# Connect
else {
$self->resolver->ioloop->defer(sub { $self->_connect($args) });
}
return $self->resolver->ioloop->timer(0 => sub { $self->_connect($args) })
unless !$args->{handle} && (my $address = $args->{address});

# Lookup
$self->resolver->lookup(
$address => sub {
$args->{address} = $_[1] || $args->{address};
$self->_connect($args);
}
);
}

sub _cleanup {
Expand Down
42 changes: 22 additions & 20 deletions lib/Mojo/IOLoop/Resolver.pm
Expand Up @@ -28,6 +28,7 @@ if (-r '/etc/resolv.conf') {
}
unshift @$SERVERS, $ENV{MOJO_DNS_SERVER} if $ENV{MOJO_DNS_SERVER};

has hosts => sub { {localhost => '127.0.0.1'} };
has ioloop => sub {
require Mojo::IOLoop;
Mojo::IOLoop->singleton;
Expand Down Expand Up @@ -67,9 +68,6 @@ my $DNS_TYPES = {
TXT => 0x0010
};

# "localhost"
our $LOCALHOST = '127.0.0.1';

sub DESTROY { shift->_cleanup }

sub build {
Expand Down Expand Up @@ -112,10 +110,11 @@ sub is_ipv6 { pop =~ $IPV6_RE }
sub lookup {
my ($self, $name, $cb) = @_;

# "localhost"
# Known hosts
weaken $self;
return $self->ioloop->defer(sub { $self->$cb($LOCALHOST) })
if $name eq 'localhost';
my $hosts = $self->hosts;
return $self->ioloop->defer(sub { $self->$cb($hosts->{$name}) })
if $hosts->{$name};

# Resolve
$self->resolve(
Expand Down Expand Up @@ -211,18 +210,18 @@ sub _cleanup {
my ($self, $next) = @_;

# Next server
push @{$self->servers}, shift @{$self->servers} if $next;

# Socket
if ($next) {
push @{$self->servers}, shift @{$self->servers};
warn "NEXT SERVER (" . $self->servers->[0] . ")\n" if DEBUG;
}
delete $self->{server};
delete $self->{started};
return unless my $loop = $self->ioloop;
$loop->drop(delete $self->{id}) if $self->{id};

# Requests
for my $id (keys %{$self->{requests}}) {
my $r = delete $self->{requests}->{$id};
$r->{cb}->($self, []);
}
# Finish requests
return unless my $requests = delete $self->{requests};
$requests->{$_}->{cb}->($self, []) for keys %$requests;
}

sub _parse_answer {
Expand Down Expand Up @@ -311,12 +310,7 @@ sub _start {
my ($loop, $stream, $error) = @_;
return $self->_cleanup(1) if $error;
$stream->on(close => sub { $self->_cleanup });
$stream->on(
error => sub {
warn "RESOLVE FAILURE ($server)\n" if DEBUG;
$self->_cleanup(1);
}
);
$stream->on(error => sub { $self->_cleanup(1) });
$stream->on(
read => sub {
my ($id, $answers) = $self->parse(pop);
Expand All @@ -326,6 +320,7 @@ sub _start {
$r->{cb}->($self, $answers);
}
);
$stream->resume;
$self->{started}++;
$self->_write;
}
Expand Down Expand Up @@ -374,6 +369,13 @@ Note that this module is EXPERIMENTAL and might change without warning!
L<Mojo::IOLoop::Resolver> implements the following attributes.
=head2 C<hosts>
my $hosts = $resolver->hosts;
$resolver = $resolver->hosts({localhost => '127.0.0.1'});
Known hosts map used by C<lookup>.
=head2 C<ioloop>
my $ioloop = $resolver->ioloop;
Expand Down
6 changes: 4 additions & 2 deletions t/mojo/resolver.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 25;
use Test::More tests => 26;

# "Oh, I'm in no condition to drive. Wait a minute.
# I don't have to listen to myself. I'm drunk."
Expand Down Expand Up @@ -51,6 +51,8 @@ is $r2->servers->[0], '8.8.4.4', 'right server';

# Lookup "localhost" (pass through)
my $result;
is $r->hosts->{localhost}, '127.0.0.1', 'right default';
$r->hosts->{localhost} = '127.0.0.23';
$r->lookup(
'localhost',
sub {
Expand All @@ -60,4 +62,4 @@ $r->lookup(
}
);
Mojo::IOLoop->start;
is $result, '127.0.0.1', 'got an address';
is $result, '127.0.0.23', 'got an address';

0 comments on commit 550273a

Please sign in to comment.