Navigation Menu

Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 18, 2012
1 parent 27e51d5 commit 7b1fea5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
3 changes: 1 addition & 2 deletions lib/Mojo/Content.pm
Expand Up @@ -128,8 +128,7 @@ sub parse {
# Chunked or relaxed content
if ($self->is_chunked || $self->relaxed) {
$self->{size} += length($self->{buffer} //= '');
$self->emit(read => $self->{buffer});
$self->{buffer} = '';
$self->emit(read => $self->{buffer})->{buffer} = '';
}

# Normal content
Expand Down
71 changes: 35 additions & 36 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -178,7 +178,7 @@ sub _cleanup {
$loop->remove($_->[1]) for @{$self->{cache} || []};
}

sub _client {
sub _connect {
my ($self, $scheme, $host, $port, $handle, $cb) = @_;

# Open connection
Expand Down Expand Up @@ -208,40 +208,11 @@ sub _client {
$stream->on(read => sub { $self->_read($id => pop) });

# Connection established
$cb->(@_);
$cb->();
}
);
}

sub _connect {
my ($self, $tx, $cb) = @_;

# Reuse connection
my $id = $tx->connection;
my ($scheme, $host, $port) = $self->transactor->endpoint($tx);
$id ||= $self->_cache("$scheme:$host:$port");
if ($id && !ref $id) {
warn "-- Reusing connection ($scheme:$host:$port)\n" if DEBUG;
$self->{connections}{$id} = {cb => $cb, tx => $tx};
$tx->kept_alive(1) unless $tx->connection;
$self->_connected($id);
return $id;
}

# CONNECT request to proxy required
return if $tx->req->method ne 'CONNECT' && $self->_connect_proxy($tx, $cb);

# Connect
warn "-- Connect ($scheme:$host:$port)\n" if DEBUG;
($scheme, $host, $port) = $self->transactor->peer($tx);
weaken $self;
$id = $self->_client(
($scheme, $host, $port, $id) => sub { $self->_connected($id) });
$self->{connections}{$id} = {cb => $cb, tx => $tx};

return $id;
}

sub _connect_proxy {
my ($self, $old, $cb) = @_;

Expand Down Expand Up @@ -271,9 +242,9 @@ sub _connect_proxy {
my $c = delete $self->{connections}{$id};
$loop->remove($id);
weaken $self;
$id = $self->_client($self->transactor->endpoint($old),
$id = $self->_connect($self->transactor->endpoint($old),
$handle, sub { $self->_start($old->connection($id), $cb) });
return $self->{connections}{$id} = $c;
$self->{connections}{$id} = $c;
}
);
}
Expand All @@ -298,6 +269,35 @@ sub _connected {
$self->_write($id);
}

sub _connection {
my ($self, $tx, $cb) = @_;

# Reuse connection
my $id = $tx->connection;
my ($scheme, $host, $port) = $self->transactor->endpoint($tx);
$id ||= $self->_cache("$scheme:$host:$port");
if ($id && !ref $id) {
warn "-- Reusing connection ($scheme:$host:$port)\n" if DEBUG;
$self->{connections}{$id} = {cb => $cb, tx => $tx};
$tx->kept_alive(1) unless $tx->connection;
$self->_connected($id);
return $id;
}

# CONNECT request to proxy required
return if $tx->req->method ne 'CONNECT' && $self->_connect_proxy($tx, $cb);

# Connect
warn "-- Connect ($scheme:$host:$port)\n" if DEBUG;
($scheme, $host, $port) = $self->transactor->peer($tx);
weaken $self;
$id = $self->_connect(
($scheme, $host, $port, $id) => sub { $self->_connected($id) });
$self->{connections}{$id} = {cb => $cb, tx => $tx};

return $id;
}

sub _error {
my ($self, $id, $err, $emit) = @_;
if (my $tx = $self->{connections}{$id}{tx}) { $tx->res->error($err) }
Expand Down Expand Up @@ -467,9 +467,8 @@ sub _start {
# Inject cookies
if (my $jar = $self->cookie_jar) { $jar->inject($tx) }

# Connect
$self->emit(start => $tx);
return unless my $id = $self->_connect($tx, $cb);
# Connection
return unless my $id = $self->emit(start => $tx)->_connection($tx, $cb);

# Request timeout
if (my $t = $self->request_timeout) {
Expand Down

0 comments on commit 7b1fea5

Please sign in to comment.