Skip to content

Commit

Permalink
move (most of) _read to channel
Browse files Browse the repository at this point in the history
  • Loading branch information
jberger committed Dec 14, 2015
1 parent 8a6a0be commit ee08396
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 11 additions & 0 deletions lib/Mojo/Channel/HTTP/Client.pm
Expand Up @@ -9,6 +9,17 @@ use constant DEBUG => $ENV{MOJO_USERAGENT_DEBUG};

has [qw/ioloop tx/];

sub read {
my ($self, $id, $chunk) = @_;
return unless my $tx = $self->tx;

# Process incoming data
warn term_escape "-- Client <<< Server (@{[_url($tx)]})\n$chunk\n" if DEBUG;
$tx->client_read($chunk);
if ($tx->is_finished) { $self->emit(finished => $id) }
elsif ($tx->is_writing) { $self->write($id) }
}

sub write {
my ($self, $id) = @_;
return unless my $tx = $self->tx;
Expand Down
13 changes: 7 additions & 6 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -146,6 +146,8 @@ sub _connect_proxy {
my $c = Mojo::Channel::HTTP::Client->new(cb => $cb, ioloop => $loop, tx => $old);
$id = $self->_connect($c, 0, $handle,
sub { shift->_start($loop, $old->connection($id), $cb) });
weaken $self;
$c->on(finished => sub { $self->_finish($id) });
$self->{connections}{$id} = $c;
}
);
Expand Down Expand Up @@ -190,6 +192,8 @@ sub _connection {
# Connect
my $c = Mojo::Channel::HTTP::Client->new(cb => $cb, ioloop => $loop, tx => $tx);
$id = $self->_connect($c, 1, undef, \&_connected);
weaken $self;
$c->on(finished => sub { $self->_finish($id) });
warn "-- Connect $id ($proto://$host:$port)\n" if DEBUG;
$self->{connections}{$id} = $c;

Expand Down Expand Up @@ -257,10 +261,7 @@ sub _read {
return $self->_remove($id) unless my $tx = $c->tx;

# Process incoming data
warn term_escape "-- Client <<< Server (@{[_url($tx)]})\n$chunk\n" if DEBUG;
$tx->client_read($chunk);
if ($tx->is_finished) { $self->_finish($id) }
elsif ($tx->is_writing) { $c->write($id) }
$c->read($id, $chunk);
}

sub _redirect {
Expand All @@ -282,7 +283,8 @@ sub _reuse {

# Connection close
my $c = $self->{connections}{$id};
my $tx = delete $c->{tx};
my $tx = $c->tx;
$c->tx(undef);
my $max = $self->max_connections;
return $self->_remove($id)
if $close || !$tx || !$max || !$tx->keep_alive || $tx->error;
Expand Down Expand Up @@ -313,7 +315,6 @@ sub _start {
$c->{timeout} = $c->ioloop
->timer($timeout => sub { $self->_error($id, 'Request timeout') });
}
$c->on(finish => sub { $self->_finish($id) });

return $id;
}
Expand Down

0 comments on commit ee08396

Please sign in to comment.