Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
small optimizations
  • Loading branch information
kraih committed Mar 29, 2013
1 parent f2cb9cd commit fc1cb8f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
9 changes: 4 additions & 5 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -13,9 +13,7 @@ has timeout => 15;

sub DESTROY { shift->close }

sub new {
shift->SUPER::new(handle => shift, buffer => '', active => steady_time);
}
sub new { shift->SUPER::new(handle => shift, buffer => '') }

sub close {
my $self = shift;
Expand Down Expand Up @@ -105,12 +103,13 @@ sub _startup {
weaken $self;
$self->{timer} = $reactor->recurring(
0.5 => sub {
return unless my $t = $self->timeout;
return unless my $timeout = $self->timeout;
$self->emit_safe('timeout')->close
if (steady_time - $self->{active}) >= $t;
if (steady_time - $self->{active}) >= $timeout;
}
);

$self->{active} = steady_time;
$reactor->io($self->{handle}, sub { pop() ? $self->_write : $self->_read });
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -105,11 +105,11 @@ sub _sandbox {
sub _timer {
my ($self, $recurring, $after, $cb) = @_;

my $timers = $self->{timers} //= {};
my $id;
do { $id = md5_sum('t' . steady_time . rand 999) }
while $self->{timers}{$id};
my $t = $self->{timers}{$id} = {cb => $cb, time => steady_time + $after};
$t->{recurring} = $after if $recurring;
do { $id = md5_sum('t' . steady_time . rand 999) } while $timers->{$id};
my $timer = $timers->{$id} = {cb => $cb, time => steady_time + $after};
$timer->{recurring} = $after if $recurring;

return $id;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -460,10 +460,10 @@ sub _start {

# Connect and add request timeout if necessary
my $id = $self->emit(start => $tx)->_connection($tx, $cb);
if (my $t = $self->request_timeout) {
if (my $timeout = $self->request_timeout) {
weaken $self;
$self->{connections}{$id}{timeout} = $self->_loop->timer(
$t => sub { $self->_error($id => 'Request timeout') });
$timeout => sub { $self->_error($id => 'Request timeout') });
}

return $id;
Expand Down

0 comments on commit fc1cb8f

Please sign in to comment.