Skip to content

Commit

Permalink
improved Mojo::IOLoop::Stream to only emit close events once
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 25, 2011
1 parent f5fe794 commit 6191af6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.11 2011-10-25 00:00:00
- Improved Mojo::IOLoop::Stream to only emit close events once.

2.10 2011-10-25 00:00:00
- Added EXPERIMENTAL send_frame method to
Mojo::Transaction::WebSocket.
Expand Down
13 changes: 9 additions & 4 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -20,7 +20,7 @@ sub DESTROY {
$self->pause if $self->{iowatcher};
return unless my $handle = $self->{handle};
close $handle;
$self->emit_safe('close');
$self->_close;
}

sub new {
Expand Down Expand Up @@ -74,6 +74,11 @@ sub write {
$self->iowatcher->writing($self->{handle}) if $self->{handle};
}

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

sub _read {
my $self = shift;

Expand All @@ -87,14 +92,14 @@ sub _read {
return if $! ~~ [EAGAIN, EINTR, EWOULDBLOCK];

# Closed
return $self->emit_safe('close') if $! == ECONNRESET;
return $self->_close if $! == ECONNRESET;

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

# EOF
return $self->emit_safe('close') if $read == 0;
return $self->_close if $read == 0;

# Handle read
$self->emit_safe(read => $buffer);
Expand All @@ -115,7 +120,7 @@ sub _write {
return if $! ~~ [EAGAIN, EINTR, EWOULDBLOCK];

# Closed
return $self->emit_safe('close') if $! ~~ [ECONNRESET, EPIPE];
return $self->_close if $! ~~ [ECONNRESET, EPIPE];

# Write error
return $self->emit_safe(error => $!);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -35,7 +35,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Leaf Fluttering In Wind';
our $VERSION = '2.10';
our $VERSION = '2.11';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
21 changes: 20 additions & 1 deletion t/mojo/ioloop.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 17;
use Test::More tests => 19;

# "Marge, you being a cop makes you the man!
# Which makes me the woman, and I have no interest in that,
Expand Down Expand Up @@ -162,3 +162,22 @@ $loop->connect(
$loop->start;
ok !$connected, 'not connected';
ok $error, 'has error';

# Dropped connection
$port = Mojo::IOLoop->generate_port;
my ($server_close, $client_close);
Mojo::IOLoop->listen(
address => 'localhost',
port => $port,
on_close => sub { $server_close++ }
);
Mojo::IOLoop->connect(
address => 'localhost',
port => $port,
on_close => sub { $client_close++ },
on_connect => sub { shift->drop(shift) }
);
Mojo::IOLoop->timer('0.5' => sub { shift->stop });
Mojo::IOLoop->start;
is $server_close, 1, 'server emitted close event once';
is $client_close, 1, 'client emitted close event once';
4 changes: 1 addition & 3 deletions t/mojo/ioloop_tls.t
Expand Up @@ -69,10 +69,8 @@ is $client, 'test321', 'right content';
$loop = Mojo::IOLoop->singleton;
$port = Mojo::IOLoop->generate_port;
$server = $client = '';
my $server_close = my $client_close = 0;
my ($drop, $running);
my ($drop, $running, $error, $server_close, $client_close);
Mojo::IOLoop->drop(Mojo::IOLoop->recurring(0 => sub { $drop++ }));
my $error = '';
$loop->listen(
port => $port,
tls => 1,
Expand Down

0 comments on commit 6191af6

Please sign in to comment.