From 5b550351e7270d92db7e79759de8e91c7e7177b6 Mon Sep 17 00:00:00 2001 From: Sebastian Riedel Date: Tue, 12 Jan 2010 03:16:57 +0100 Subject: [PATCH] added the ability to run multiple parallel ioloops that block each --- Changes | 2 ++ lib/Mojo/Client.pm | 2 +- lib/Mojo/IOLoop.pm | 12 ++++++++---- lib/Mojo/Server/Daemon.pm | 5 ++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Changes b/Changes index a22517806b..ec005ac51e 100644 --- a/Changes +++ b/Changes @@ -25,6 +25,8 @@ This file documents the revision history for Perl extension Mojo. - Added version command. - Added after_build_tx plugin hook. - Added timer support to Mojo::IOLoop. + - Added the ability to run multiple parallel ioloops that block each + other. - Made all Mojolicious after_* plugin hooks run in reverse order. - Made param decoding more defensive and allow malformed data to pass through for debugging. diff --git a/lib/Mojo/Client.pm b/lib/Mojo/Client.pm index 4170bed389..68e36c0937 100644 --- a/lib/Mojo/Client.pm +++ b/lib/Mojo/Client.pm @@ -19,7 +19,7 @@ use Socket; __PACKAGE__->attr([qw/app default_cb tls_ca_file tls_verify_cb/]); __PACKAGE__->attr([qw/continue_timeout max_keep_alive_connections/] => 5); __PACKAGE__->attr(cookie_jar => sub { Mojo::CookieJar->new }); -__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->new }); +__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->singleton }); __PACKAGE__->attr(keep_alive_timeout => 15); __PACKAGE__->attr(max_redirects => 0); diff --git a/lib/Mojo/IOLoop.pm b/lib/Mojo/IOLoop.pm index 3aea439bde..69c3eaff27 100644 --- a/lib/Mojo/IOLoop.pm +++ b/lib/Mojo/IOLoop.pm @@ -59,13 +59,11 @@ __PACKAGE__->attr( # Singleton our $LOOP; -# Instantiate singleton sub new { - my $self = $LOOP ||= shift->SUPER::new(@_); + my $self = shift->SUPER::new(@_); - # Signals + # Ignore pipe $SIG{PIPE} = 'IGNORE'; - $SIG{HUP} = sub { $self->_running(0) }; return $self; } @@ -299,6 +297,8 @@ sub remote_info { return {address => $socket->peerhost, port => $socket->peerport}; } +sub singleton { $LOOP ||= shift->new(@_) } + sub start { my $self = shift; @@ -950,6 +950,10 @@ following new ones. my $info = $loop->remote_info($id); +=head2 C + + my $loop = Mojo::IOLoop->singleton; + =head2 C $loop->start; diff --git a/lib/Mojo/Server/Daemon.pm b/lib/Mojo/Server/Daemon.pm index 6a0cff6459..64662f4690 100644 --- a/lib/Mojo/Server/Daemon.pm +++ b/lib/Mojo/Server/Daemon.pm @@ -18,7 +18,7 @@ use Scalar::Util qw/isweak weaken/; use Sys::Hostname 'hostname'; __PACKAGE__->attr([qw/group listen listen_queue_size user/]); -__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->new }); +__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->singleton }); __PACKAGE__->attr(keep_alive_timeout => 15); __PACKAGE__->attr( lock_file => sub { @@ -65,6 +65,9 @@ sub prepare_ioloop { # Max clients $self->ioloop->max_connections($self->max_clients); + + # Stop ioloop on HUP + $SIG{HUP} = sub { $self->ioloop->stop }; } sub prepare_lock_file {