Skip to content

Commit

Permalink
added experimental error event to Mojo::IOWatcher and improved Mojo::…
Browse files Browse the repository at this point in the history
…IOLoop performance by increasing the default cleanup interval from 0 to 0.025 seconds
  • Loading branch information
kraih committed Nov 24, 2011
1 parent 0a29702 commit ae550a6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,7 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.32 2011-11-22 00:00:00
2.32 2011-11-24 00:00:00
- Added EXPERIMENTAL error event to Mojo::IOWatcher.
- Updated jQuery to version 1.7.1.
- Improved Mojo::IOLoop performance by changing the default cleanup
interval from 0 to 0.025 seconds.
- Improved documentation.

2.31 2011-11-21 00:00:00
Expand Down
9 changes: 6 additions & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -17,10 +17,13 @@ has client_class => 'Mojo::IOLoop::Client';
has iowatcher => sub {
my $class = Mojo::IOWatcher->detect;
warn "MAINLOOP ($class)\n" if DEBUG;
$class->new;
my $watcher = $class->new;
$watcher->on(error => sub { warn pop });
return $watcher;
};
has [qw/cleanup_interval max_accepts/] => 0;
has cleanup_interval => '0.025';
has [qw/lock unlock/];
has max_accepts => 0;
has max_connections => 1000;
has server_class => 'Mojo::IOLoop::Server';
has stream_class => 'Mojo::IOLoop::Stream';
Expand Down Expand Up @@ -555,7 +558,7 @@ Note that this attribute is EXPERIMENTAL and might change without warning!
my $interval = $loop->cleanup_interval;
$loop = $loop->cleanup_interval(1);
Connection cleanup interval in seconds, defaults to C<0>.
Connection cleanup interval in seconds, defaults to C<0.025>.
Note that this attribute is EXPERIMENTAL and might change without warning!
=head2 C<lock>
Expand Down
21 changes: 17 additions & 4 deletions lib/Mojo/IOWatcher.pm
@@ -1,5 +1,5 @@
package Mojo::IOWatcher;
use Mojo::Base -base;
use Mojo::Base 'Mojo::EventEmitter';

use IO::Poll qw/POLLERR POLLHUP POLLIN POLLOUT/;
use Mojo::Loader;
Expand Down Expand Up @@ -126,7 +126,8 @@ sub _sandbox {
my $self = shift;
my $desc = shift;
return unless my $cb = shift;
warn "$desc failed: $@" unless eval { $self->$cb(@_); 1 };
$self->emit_safe(error => "$desc failed: $@")
unless eval { $self->$cb(@_); 1 };
}

1;
Expand Down Expand Up @@ -165,10 +166,22 @@ foundation of L<Mojo::IOLoop>.
L<Mojo::IOWatcher::EV> is a good example for its extensibility.
Note that this module is EXPERIMENTAL and might change without warning!
=head1 EVENTS
L<Mojo::IOWatcher> can emit the following events.
=head2 C<error>
$watcher->on(error => sub {
my ($watcher, $error) = @_;
});
Emitted if an error happens.
=head1 METHODS
L<Mojo::IOWatcher> inherits all methods from L<Mojo::Base> and implements the
following new ones.
L<Mojo::IOWatcher> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 C<detect>
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojo/IOWatcher/EV.pm
Expand Up @@ -92,6 +92,10 @@ L<Mojo::IOWatcher::EV> is a minimalistic non-blocking I/O watcher with
C<libev> support.
Note that this module is EXPERIMENTAL and might change without warning!
=head1 EVENTS
L<Mojo::IOWatcher::EV> inherits all events from L<Mojo::IOWatcher>.
=head1 METHODS
L<Mojo::IOWatcher::EV> inherits all methods from L<Mojo::IOWatcher> and
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/app.t
Expand Up @@ -41,7 +41,7 @@ $id = Mojo::IOLoop->client(
my ($stream, $chunk) = @_;
$buffer .= $chunk;
Mojo::IOLoop->drop($id) and Mojo::IOLoop->stop
if $buffer =~ /Mojo is working!/;
if $buffer =~ s/ is working!$//;
$stream->write('4321')
if $buffer =~ m#HTTP/1.1 100 Continue.*\x0d\x0a\x0d\x0a#gs;
}
Expand All @@ -52,7 +52,7 @@ $id = Mojo::IOLoop->client(
}
);
Mojo::IOLoop->start;
like $buffer, qr#HTTP/1.1 100 Continue#, 'request was continued';
like $buffer, qr#HTTP/1.1 100 Continue.*Mojo$#s, 'request was continued';

# Pipelined
$buffer = '';
Expand All @@ -64,7 +64,7 @@ $id = Mojo::IOLoop->client(
my ($stream, $chunk) = @_;
$buffer .= $chunk;
Mojo::IOLoop->drop($id) and Mojo::IOLoop->stop
if $buffer =~ /Mojo.*Mojo/gs;
if $buffer =~ s/ is working!.*is working!$//gs;
}
);
$stream->write("GET /2/ HTTP/1.1\x0d\x0a"
Expand All @@ -74,7 +74,7 @@ $id = Mojo::IOLoop->client(
}
);
Mojo::IOLoop->start;
like $buffer, qr/Mojo/, 'transactions were pipelined';
like $buffer, qr/Mojo$/, 'transactions were pipelined';

# Normal request
my $tx = Mojo::Transaction::HTTP->new;
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/ioloop.t
Expand Up @@ -155,6 +155,8 @@ $stream->on(read => sub { $buffer .= pop });
$stream->write('hello');
ok Mojo::IOLoop->stream($id), 'stream exists';
Mojo::IOLoop->start;
Mojo::IOLoop->timer('0.25' => sub { Mojo::IOLoop->stop });
Mojo::IOLoop->start;
ok !Mojo::IOLoop->stream($id), 'stream does not exist anymore';
is $buffer, 'acceptedhelloworld', 'right result';

Expand Down
15 changes: 14 additions & 1 deletion t/mojo/iowatcher.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 52;
use Test::More tests => 53;

# "I don't mind being called a liar when I'm lying, or about to lie,
# or just finished lying, but NOT WHEN I'M TELLING THE TRUTH."
Expand Down Expand Up @@ -155,3 +155,16 @@ $watcher2->timer(0 => sub { shift->stop });
$watcher2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';

# Error
$watcher = Mojo::IOWatcher->new;
my $error;
$watcher->on(
error => sub {
shift->stop;
$error = pop;
}
);
$watcher->timer(0 => sub { die "works!\n" });
$watcher->start;
like $error, qr/works!/, 'right error';
15 changes: 14 additions & 1 deletion t/mojo/iowatcher_ev.t
Expand Up @@ -9,7 +9,7 @@ use Test::More;
plan skip_all => 'set TEST_EV to enable this test (developer only!)'
unless $ENV{TEST_EV};
plan skip_all => 'EV 4.0 required for this test!' unless eval 'use EV 4.0; 1';
plan tests => 52;
plan tests => 53;

use IO::Socket::INET;
use Mojo::IOLoop;
Expand Down Expand Up @@ -156,3 +156,16 @@ $watcher2->timer(0 => sub { shift->stop });
$watcher2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';

# Error
$watcher = Mojo::IOWatcher::EV->new;
my $error;
$watcher->on(
error => sub {
shift->stop;
$error = pop;
}
);
$watcher->timer(0 => sub { die "works!\n" });
$watcher->start;
like $error, qr/works!/, 'right error';

0 comments on commit ae550a6

Please sign in to comment.