Mojolicious
view release on metacpan or search on metacpan
lib/Mojo/IOLoop.pm view on Meta::CPAN
1;
=encoding utf8
=head1 NAME
Mojo::IOLoop - Minimalistic event loop
=head1 SYNOPSIS
use Mojo::IOLoop;
# Listen on port 3000
Mojo::IOLoop->server({port => 3000} => sub ($loop, $stream, $id) {
$stream->on(read => sub ($stream, $bytes) {
# Process input chunk
say $bytes;
# Write response
$stream->write('HTTP/1.1 200 OK');
});
});
# Connect to port 3000
my $id = Mojo::IOLoop->client({port => 3000} => sub ($loop, $err, $stream) {
$stream->on(read => sub ($stream, $bytes) {
# Process input
say "Input: $bytes";
});
# Write request
$stream->write("GET / HTTP/1.1\x0d\x0a\x0d\x0a");
});
# Add a timer
Mojo::IOLoop->timer(5 => sub ($loop) { $loop->remove($id) });
# Start event loop if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head1 DESCRIPTION
L<Mojo::IOLoop> is a very minimalistic event loop based on L<Mojo::Reactor>, it has been reduced to the absolute
minimal feature set required to build solid and scalable non-blocking clients and servers.
Depending on operating system, the default per-process and system-wide file descriptor limits are often very low and
need to be tuned for better scalability. The C<LIBEV_FLAGS> environment variable should also be used to select the best
possible L<EV> backend, which usually defaults to the not very scalable C<select>.
LIBEV_FLAGS=1 # select
LIBEV_FLAGS=2 # poll
LIBEV_FLAGS=4 # epoll (Linux)
LIBEV_FLAGS=8 # kqueue (*BSD, OS X)
LIBEV_FLAGS=64 # Linux AIO
The event loop will be resilient to time jumps if a monotonic clock is available through L<Time::HiRes>. A TLS
certificate and key are also built right in, to make writing test servers as easy as possible. Also note that for
convenience the C<PIPE> signal will be set to C<IGNORE> when L<Mojo::IOLoop> is loaded.
For better scalability (epoll, kqueue) and to provide non-blocking name resolution, SOCKS5 as well as TLS support, the
optional modules L<EV> (4.32+), L<Net::DNS::Native> (0.15+), L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL>
(2.009+) will be used automatically if possible. Individual features can also be disabled with the C<MOJO_NO_NNR>,
C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment variables.
See L<Mojolicious::Guides::Cookbook/"REAL-TIME WEB"> for more.
=head1 EVENTS
L<Mojo::IOLoop> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
=head2 finish
$loop->on(finish => sub ($loop) {...});
Emitted when the event loop wants to shut down gracefully and is just waiting for all existing connections to be
closed.
=head2 reset
$loop->on(reset => sub ($loop) {...});
Emitted when the event loop is reset, this usually happens after the process is forked to clean up resources that
cannot be shared.
=head1 ATTRIBUTES
L<Mojo::IOLoop> implements the following attributes.
=head2 max_accepts
my $max = $loop->max_accepts;
$loop = $loop->max_accepts(1000);
The maximum number of connections this event loop is allowed to accept, before shutting down gracefully without
interrupting existing connections, defaults to C<0>. Setting the value to C<0> will allow this event loop to accept new
connections indefinitely. Note that up to half of this value can be subtracted randomly to improve load balancing
between multiple server processes, and to make sure that not all of them restart at the same time.
=head2 max_connections
my $max = $loop->max_connections;
$loop = $loop->max_connections(100);
The maximum number of accepted connections this event loop is allowed to handle concurrently, before stopping to accept
new incoming connections, defaults to C<1000>.
=head2 reactor
my $reactor = $loop->reactor;
$loop = $loop->reactor(Mojo::Reactor->new);
Low-level event reactor, usually a L<Mojo::Reactor::Poll> or L<Mojo::Reactor::EV> object with a default subscriber to
the event L<Mojo::Reactor/"error">.
# Watch if handle becomes readable or writable
Mojo::IOLoop->singleton->reactor->io($handle => sub ($reactor, $writable) {
say $writable ? 'Handle is writable' : 'Handle is readable';
});
# Change to watching only if handle becomes writable
Mojo::IOLoop->singleton->reactor->watch($handle, 0, 1);
( run in 2.850 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )