Skip to content

Commit

Permalink
handle all events the same
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 10, 2014
1 parent d2bbee2 commit 524bc73
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
7 changes: 2 additions & 5 deletions lib/Mojo.pm
Expand Up @@ -14,12 +14,9 @@ use Scalar::Util 'weaken';
has home => sub { Mojo::Home->new };
has log => sub { Mojo::Log->new };
has ua => sub {
my $self = shift;

my $ua = Mojo::UserAgent->new;
weaken $ua->server->app($self)->{app};
weaken $self;
return $ua->catch(sub { $self->log->error($_[1]) });
weaken $ua->server->app(shift)->{app};
return $ua;
};

sub build_tx { Mojo::Transaction::HTTP->new }
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -109,8 +109,7 @@ sub _tls {

# Connected
my $handle = $self->{handle};
return $self->_cleanup->emit_safe(connect => $handle)
if $handle->connect_SSL;
return $self->_cleanup->emit(connect => $handle) if $handle->connect_SSL;

# Switch between reading and writing
my $err = $IO::Socket::SSL::SSL_ERROR;
Expand Down Expand Up @@ -144,7 +143,7 @@ sub _try_tls {
my ($self, $args) = @_;

my $handle = $self->{handle};
return $self->_cleanup->emit_safe(connect => $handle)
return $self->_cleanup->emit(connect => $handle)
if !$args->{tls} || $handle->isa('IO::Socket::SSL');
return $self->emit(error => 'IO::Socket::SSL 1.84 required for TLS support')
unless TLS;
Expand Down Expand Up @@ -213,7 +212,7 @@ emit the following new ones.
...
});
Emitted safely once the connection is established.
Emitted once the connection is established.
=head2 error
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -125,7 +125,7 @@ sub _accept {
setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;

# Start TLS handshake
$self->emit_safe(accept => $handle) and next unless my $tls = $self->{tls};
$self->emit(accept => $handle) and next unless my $tls = $self->{tls};
$self->_handshake($self->{handles}{$handle} = $handle)
if $handle = IO::Socket::SSL->start_SSL($handle, %$tls, SSL_server => 1);
}
Expand All @@ -143,7 +143,7 @@ sub _tls {
# Accepted
if ($handle->accept_SSL) {
$self->reactor->remove($handle);
return $self->emit_safe(accept => delete $self->{handles}{$handle});
return $self->emit(accept => delete $self->{handles}{$handle});
}

# Switch between reading and writing
Expand Down Expand Up @@ -195,7 +195,7 @@ emit the following new ones.
...
});
Emitted safely for each accepted connection.
Emitted for each accepted connection.
=head1 ATTRIBUTES
Expand Down
26 changes: 13 additions & 13 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -16,7 +16,7 @@ sub close {
return unless my $handle = delete $self->timeout(0)->{handle};
$reactor->remove($handle);
close $handle;
$self->emit_safe('close');
$self->emit('close');
}

sub close_gracefully {
Expand Down Expand Up @@ -76,7 +76,7 @@ sub timeout {
return $self unless my $timeout = $self->{timeout} = shift;
weaken $self;
$self->{timer}
= $reactor->timer($timeout => sub { $self->emit_safe('timeout')->close });
= $reactor->timer($timeout => sub { $self->emit('timeout')->close });

return $self;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ sub _read {
my $read = $self->{handle}->sysread(my $buffer, 131072, 0);
return $self->_error unless defined $read;
return $self->close if $read == 0;
$self->emit_safe(read => $buffer)->_again;
$self->emit(read => $buffer)->_again;
}

sub _write {
Expand All @@ -123,13 +123,13 @@ sub _write {
if (length $self->{buffer}) {
my $written = $handle->syswrite($self->{buffer});
return $self->_error unless defined $written;
$self->emit_safe(write => substr($self->{buffer}, 0, $written, ''));
$self->emit(write => substr($self->{buffer}, 0, $written, ''));
$self->_again;
}

$self->emit_safe('drain') if !length $self->{buffer};
return if $self->is_writing;
return $self->close if $self->{graceful};
$self->emit('drain') if !length $self->{buffer};
return if $self->is_writing;
return $self->close if $self->{graceful};
$self->reactor->watch($handle, !$self->{paused}, 0) if $self->{handle};
}

Expand Down Expand Up @@ -184,7 +184,7 @@ emit the following new ones.
...
});
Emitted safely if the stream gets closed.
Emitted if the stream gets closed.
=head2 drain
Expand All @@ -193,7 +193,7 @@ Emitted safely if the stream gets closed.
...
});
Emitted safely once all data has been written.
Emitted once all data has been written.
=head2 error
Expand All @@ -211,7 +211,7 @@ Emitted if an error occurs on the stream, fatal if unhandled.
...
});
Emitted safely if new data arrives on the stream.
Emitted if new data arrives on the stream.
=head2 timeout
Expand All @@ -220,8 +220,8 @@ Emitted safely if new data arrives on the stream.
...
});
Emitted safely if the stream has been inactive for too long and will get
closed automatically.
Emitted if the stream has been inactive for too long and will get closed
automatically.
=head2 write
Expand All @@ -230,7 +230,7 @@ closed automatically.
...
});
Emitted safely if new data has been written to the stream.
Emitted if new data has been written to the stream.
=head1 ATTRIBUTES
Expand Down
8 changes: 5 additions & 3 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -515,9 +515,11 @@ created at startup time.

app->start;

Since timers and other low-level event watchers are also independent from
applications, errors can't get logged automatically, you can change that by
subscribing to the event L<Mojo::Reactor/"error">.
=head2 Exceptions in events

Since timers and other event watchers are independent from applications,
errors can't get logged automatically, you can change that by subscribing to
the event L<Mojo::Reactor/"error">.

# Forward error messages to the application log
Mojo::IOLoop->singleton->reactor->on(error => sub {
Expand Down
11 changes: 5 additions & 6 deletions t/mojo/user_agent.t
Expand Up @@ -179,14 +179,13 @@ is $tx->res->code, 200, 'right status';
is $tx->res->headers->connection, 'test', 'right "Connection" value';
is $tx->res->body, 'One!', 'right content';

# Error in callback is logged
app->ua->once(error => sub { Mojo::IOLoop->stop });
ok app->ua->has_subscribers('error'), 'has subscribers';
# Error in callback
Mojo::IOLoop->singleton->reactor->unsubscribe('error');
my $err;
my $msg = app->log->on(message => sub { $err .= pop });
Mojo::IOLoop->singleton->reactor->once(
error => sub { $err .= pop; Mojo::IOLoop->stop });
app->ua->get('/' => sub { die 'error event works' });
Mojo::IOLoop->start;
app->log->unsubscribe(message => $msg);
like $err, qr/error event works/, 'right error';

# Events
Expand Down Expand Up @@ -289,7 +288,7 @@ is $body, '{"hello":"world"}', 'right content';

# Built-in web server times out
my $log = '';
$msg = app->log->on(message => sub { $log .= pop });
my $msg = app->log->on(message => sub { $log .= pop });
$tx = $ua->get('/timeout?timeout=0.25');
app->log->unsubscribe(message => $msg);
ok !$tx->success, 'not successful';
Expand Down

0 comments on commit 524bc73

Please sign in to comment.