Skip to content

Commit

Permalink
fixed small bug in Mojo::UserAgent that prevented port reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 10, 2012
1 parent a6ef98c commit 1d55e71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
3.06 2012-07-10
- Improved documentation.
- Improved tests.
- Fixed small bug in Mojo::UserAgent that prevented port reuse.

3.05 2012-07-08
- Reduced default graceful_timeout from 30 to 20 seconds in
Expand Down
23 changes: 11 additions & 12 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -103,8 +103,8 @@ sub start {
unless ($self->{nb}) {
croak 'Blocking request in progress' if keys %{$self->{connections}};
warn "-- Switching to non-blocking mode\n" if DEBUG;
$self->_cleanup;
$self->{nb} = 1;
$self->{nb}++;
$self->_cleanup(1);
}
return $self->_start($tx, $cb);
}
Expand All @@ -114,7 +114,7 @@ sub start {
if (delete $self->{nb}) {
croak 'Non-blocking requests in progress' if keys %{$self->{connections}};
warn "-- Switching to blocking mode\n" if DEBUG;
$self->_cleanup;
$self->_cleanup(1);
}
$self->_start($tx, sub { $tx = $_[1] });

Expand Down Expand Up @@ -163,17 +163,18 @@ sub _cache {
}

sub _cleanup {
my $self = shift;
my ($self, $restart) = @_;
return unless my $loop = $self->_loop;

# Stop server
delete $self->{server};

# Clean up active connections
$loop->remove($_) for keys %{$self->{connections} || {}};
$loop->remove($_) for keys %{delete $self->{connections} || {}};

# Clean up keep alive connections
$loop->remove($_->[1]) for @{$self->{cache} || []};
$loop->remove($_->[1]) for @{delete $self->{cache} || []};

# Stop or restart server
delete $self->{server};
$self->_server if $restart;
}

sub _connect {
Expand Down Expand Up @@ -415,7 +416,7 @@ sub _server {
my $loop = $self->_loop;
my $server = $self->{server}
= Mojo::Server::Daemon->new(ioloop => $loop, silent => 1);
my $port = $self->{port} = $loop->generate_port;
my $port = $self->{port} ||= $loop->generate_port;
die "Couldn't find a free TCP port for testing.\n" unless $port;
$self->{scheme} = $scheme ||= 'http';
$server->listen(["$scheme://127.0.0.1:$port"])->start;
Expand Down Expand Up @@ -816,8 +817,6 @@ C<MOJO_APP> environment variable or a L<Mojo::HelloWorld> object.
my $url = $ua->app_url('https');
Get absolute L<Mojo::URL> object for C<app> and switch protocol if necessary.
Note that the port may change when switching from blocking to non-blocking
requests.
# Port currently used for processing relative URLs
say $ua->app_url->port;
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/user_agent.t
Expand Up @@ -292,15 +292,15 @@ is $stream, 1, 'no leaking subscribers';
# Nested keep alive
my @kept_alive;
$ua->get(
'/' => sub {
$ua->app_url => sub {
my ($self, $tx) = @_;
push @kept_alive, $tx->kept_alive;
$self->get(
'/' => sub {
my ($self, $tx) = @_;
push @kept_alive, $tx->kept_alive;
$self->get(
'/' => sub {
$ua->app_url => sub {
my ($self, $tx) = @_;
push @kept_alive, $tx->kept_alive;
Mojo::IOLoop->stop;
Expand Down

0 comments on commit 1d55e71

Please sign in to comment.