Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 4, 2013
1 parent 03f6d26 commit 1f76807
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.98 2013-05-04
3.98 2013-05-05
- Added is_empty method to Mojo::Transaction::HTTP.
- Added close_gracefully method to Mojo::IOLoop::Stream.
- Removed deprecated after_static_dispatch hook.
Expand Down
47 changes: 24 additions & 23 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -50,15 +50,34 @@ sub is_writing {

sub start {
my $self = shift;
return $self->_startup unless $self->{startup}++;
return unless delete $self->{paused};
$self->reactor->watch($self->{handle}, 1, $self->is_writing);

my $reactor = $self->reactor;
unless ($self->{timer}) {

# Timeout (ignore 0 timeout)
weaken $self;
$self->{timer} = $reactor->recurring(
1 => sub {
return unless my $timeout = $self->timeout;
my $diff = steady_time - $self->{active};
$self->emit_safe('timeout')->close if $diff >= $timeout;
}
);

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

# Resume
$reactor->watch($self->{handle}, 1, $self->is_writing)
if delete $self->{paused};
}

sub stop {
my $self = shift;
return if $self->{paused}++;
$self->reactor->watch($self->{handle}, 0, $self->is_writing);
$self->reactor->watch($self->{handle}, 0, $self->is_writing)
unless $self->{paused}++;
}

sub steal_handle {
Expand Down Expand Up @@ -101,24 +120,6 @@ sub _read {
$self->emit_safe(read => $buffer)->{active} = steady_time;
}

sub _startup {
my $self = shift;

# Timeout (ignore 0 timeout)
my $reactor = $self->reactor;
weaken $self;
$self->{timer} = $reactor->recurring(
1 => sub {
return unless my $timeout = $self->timeout;
$self->emit_safe('timeout')->close
if (steady_time - $self->{active}) >= $timeout;
}
);

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

sub _write {
my $self = shift;

Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -332,8 +332,9 @@ sub _handle {
my ($self, $id, $close) = @_;

# Remove request timeout
return unless my $loop = $self->_loop;
my $c = $self->{connections}{$id};
$self->_loop->remove($c->{timeout}) if $c->{timeout};
$loop->remove($c->{timeout}) if $c->{timeout};

# Finish WebSocket
my $old = $c->{tx};
Expand Down

0 comments on commit 1f76807

Please sign in to comment.