Skip to content

Commit

Permalink
improved resilience of Mojo::IOLoop::Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 17, 2011
1 parent 8792dd8 commit 5648823
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.38 2011-12-17 00:00:00
- Changed Mojo::IOLoop->client arguments from ($loop, $stream, $err)
to ($loop, $err, $stream).
- Improved resilience of Mojo::IOLoop::Stream.
- Improved documentation. (marcus, sri)
- Improved tests.
- Fixed memory leaks caused by named capture groups bug in Perl.
Expand Down
39 changes: 18 additions & 21 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -17,15 +17,7 @@ has timeout => 15;
# Iran, Iraq, China, Mordor, the hoochies that laid low Tiger Woods,
# undesirable immigrants - by which I mean everyone that came after me,
# including my children..."
sub DESTROY {
my $self = shift;
return unless my $watcher = $self->{iowatcher};
return unless my $handle = $self->{handle};
$watcher->drop_handle($handle);
$watcher->drop_timer($self->{timer}) if $self->{timer};
close $handle;
$self->_close;
}
sub DESTROY { shift->_close }

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

Expand All @@ -38,7 +30,7 @@ sub is_readable {

sub is_writing {
my $self = shift;
return if $self->{closed} || $self->{error} || $self->{timed_out};
return if $self->{closed};
return length($self->{buffer}) || $self->has_subscribers('drain');
}

Expand All @@ -56,9 +48,8 @@ sub resume {
weaken $self;
$self->{timer} ||= $watcher->recurring(
'0.025' => sub {
return unless $self && (time - ($self->{active})) >= $self->timeout;
$self->emit_safe('timeout') unless $self->{timed_out}++;
$self->_close;
$self->emit_safe('timeout')->_close
if $self && (time - ($self->{active})) >= $self->timeout;
}
);

Expand Down Expand Up @@ -99,13 +90,19 @@ sub write {

sub _close {
my $self = shift;
$self->emit_safe('close') unless $self->{closed}++;
}

sub _error {
my $self = shift;
$self->{error}++;
$self->emit_safe(error => @_);
# Already closed
return if $self->{closed}++;

# Cleanup
return unless my $watcher = $self->{iowatcher};
return unless my $handle = $self->{handle};
$watcher->drop_handle($handle);
$watcher->drop_timer($self->{timer}) if $self->{timer};

# Close
close $handle;
$self->emit_safe('close');
}

sub _read {
Expand All @@ -124,7 +121,7 @@ sub _read {
return $self->_close if $! == ECONNRESET;

# Read error
return $self->_error($!);
return $self->emit_safe(error => $!)->_close;
}

# EOF
Expand Down Expand Up @@ -155,7 +152,7 @@ sub _write {
return $self->_close if $! ~~ [ECONNRESET, EPIPE];

# Write error
return $self->_error($!);
return $self->emit_safe(error => $!)->_close;
}

# Remove written chunk from buffer
Expand Down

0 comments on commit 5648823

Please sign in to comment.