Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/469dce107a3b3d7fb1ec to your computer and use it in GitHub Desktop.
Save anonymous/469dce107a3b3d7fb1ec to your computer and use it in GitHub Desktop.
diff --git a/Changes b/Changes
index 5ed655e..ef47e22 100644
--- a/Changes
+++ b/Changes
@@ -1,7 +1,5 @@
5.80 2015-02-17
- - Deprecated Mojo::IOLoop::max_connections in favor of
- Mojo::IOLoop::concurrency.
- Removed accept_interval, lock and unlock attributes from Mojo::IOLoop.
- Removed accept_interval, lock_file and lock_timeout attributes from
Mojo::Server::Prefork.
@@ -9,6 +7,7 @@
prefork command.
- Removed accept_interval, lock_file and lock_timeout parameters from
Hypntoad.
+ - Added stop_gracefully method to Mojo::IOLoop.
- Reduced idle CPU usage of Mojo::IOLoop and Mojo::Server::Prefork.
- Improved app generator command to use current best practices.
- Fixed url_for to handle paths without trailing slash correctly in embedded
diff --git a/lib/Mojo/IOLoop.pm b/lib/Mojo/IOLoop.pm
index b349c20..3e2f776 100644
--- a/lib/Mojo/IOLoop.pm
+++ b/lib/Mojo/IOLoop.pm
@@ -9,14 +9,15 @@ use Mojo::IOLoop::Delay;
use Mojo::IOLoop::Server;
use Mojo::IOLoop::Stream;
use Mojo::Reactor::Poll;
-use Mojo::Util qw(deprecated md5_sum steady_time);
+use Mojo::Util qw(md5_sum steady_time);
use Scalar::Util qw(blessed weaken);
use constant DEBUG => $ENV{MOJO_IOLOOP_DEBUG} || 0;
-has max_accepts => 0;
-has multi_accept => 50;
-has reactor => sub {
+has max_accepts => 0;
+has max_connections => 1000;
+has multi_accept => 50;
+has reactor => sub {
my $class = Mojo::Reactor::Poll->detect;
warn "-- Reactor initialized ($class)\n" if DEBUG;
return $class->new->catch(sub { warn "@{[blessed $_[0]]}: $_[1]" });
@@ -73,14 +74,6 @@ sub client {
return $id;
}
-sub concurrency {
- my $self = _instance(shift);
- return $self->{concurrency} //= 1000 unless @_;
- my $concurrency = $self->{concurrency} = shift;
- $self->{stop} ||= $self->recurring(1 => \&_graceful) if $concurrency == 0;
- return $self;
-}
-
sub delay {
my $delay = Mojo::IOLoop::Delay->new;
weaken $delay->ioloop(_instance(shift))->{ioloop};
@@ -89,13 +82,6 @@ sub delay {
sub is_running { _instance(shift)->reactor->is_running }
-# DEPRECATED in Tiger Face!
-sub max_connections {
- deprecated 'Mojo::IOLoop::max_connections is DEPRECATED in favor of'
- . ' Mojo::IOLoop::concurrency';
- shift->concurrency(@_);
-}
-
sub next_tick {
my ($self, $cb) = (_instance(shift), @_);
weaken $self;
@@ -136,7 +122,7 @@ sub server {
# Enforce connection limit (randomize to improve load balancing)
if (my $max = $self->max_accepts) {
$self->{accepts} //= $max - int rand $max / 2;
- $self->concurrency(0) if ($self->{accepts} -= 1) <= 0;
+ $self->stop_gracefully if ($self->{accepts} -= 1) <= 0;
}
my $stream = Mojo::IOLoop::Stream->new(pop);
@@ -158,6 +144,11 @@ sub start {
sub stop { _instance(shift)->reactor->stop }
+sub stop_gracefully {
+ my $self = _instance(shift)->max_connections(0);
+ $self->{stop} ||= $self->recurring(1 => \&_graceful);
+}
+
sub stream {
my ($self, $stream) = (_instance(shift), @_);
return ($self->{connections}{$stream} || {})->{stream} unless ref $stream;
@@ -183,7 +174,7 @@ sub _id {
sub _instance { ref $_[0] ? $_[0] : $_[0]->singleton }
-sub _limit { keys %{$_[0]->{connections}} >= $_[0]->concurrency }
+sub _limit { keys %{$_[0]->{connections}} >= $_[0]->max_connections }
sub _maybe_accepting {
my $self = shift;
@@ -193,7 +184,7 @@ sub _maybe_accepting {
# Check if multi-accept is desirable
my $m = $self->multi_accept;
- $m = 1 if $self->concurrency < $m;
+ $m = 1 if $self->max_connections < $m;
$_->multi_accept($m)->start for values %{$self->{acceptors} || {}};
$self->{accepting} = 1;
}
@@ -337,6 +328,15 @@ 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.
+=head2 max_connections
+
+ my $max = $loop->max_connections;
+ $loop = $loop->max_connections(1000);
+
+The maximum number of concurrent connections this event loop is allowed to
+handle before stopping to accept new incoming connections, defaults to
+C<1000>.
+
=head2 multi_accept
my $multi = $loop->multi_accept;
@@ -394,18 +394,6 @@ L<Mojo::IOLoop::Client/"connect">.
...
});
-=head2 concurrency
-
- my $max = Mojo::IOLoop->concurrency;
- my $max = $loop->concurrency;
- $loop = $loop->concurrency(1000);
-
-The maximum number of concurrent connections this event loop is allowed to
-handle before stopping to accept new incoming connections, defaults to
-C<1000>. Setting the value to C<0> will make this event loop stop accepting
-new connections and allow it to shut down gracefully without interrupting
-existing connections.
-
=head2 delay
my $delay = Mojo::IOLoop->delay;
@@ -595,6 +583,14 @@ some reactors stop automatically if there are no events being watched anymore.
Stop the event loop, this will not interrupt any existing connections and the
event loop can be restarted by running L</"start"> again.
+=head2 stop_gracefully
+
+ Mojo::IOLoop->stop_gracefully;
+ $loop->stop_gracefully;
+
+Stop accepting new connections and shut down the event loop gracefully without
+interrupting existing connections.
+
=head2 stream
my $stream = Mojo::IOLoop->stream($id);
diff --git a/lib/Mojo/Server/Daemon.pm b/lib/Mojo/Server/Daemon.pm
index 087126e..1c45296 100644
--- a/lib/Mojo/Server/Daemon.pm
+++ b/lib/Mojo/Server/Daemon.pm
@@ -41,7 +41,7 @@ sub start {
# Start listening
else { $self->_listen($_) for @{$self->listen} }
- if (my $max = $self->max_clients) { $loop->concurrency($max) }
+ if (my $max = $self->max_clients) { $loop->max_connections($max) }
return $self;
}
diff --git a/lib/Mojo/Server/Prefork.pm b/lib/Mojo/Server/Prefork.pm
index ae5964d..3484bd6 100644
--- a/lib/Mojo/Server/Prefork.pm
+++ b/lib/Mojo/Server/Prefork.pm
@@ -154,14 +154,14 @@ sub _spawn {
# Heartbeat messages
weaken $self;
- my $cb = sub { $self->_heartbeat(shift->concurrency ? 0 : 1) };
+ my $cb = sub { $self->_heartbeat(shift->max_connections ? 0 : 1) };
my $loop = $self->ioloop;
$loop->next_tick($cb);
$loop->recurring($self->heartbeat_interval => $cb);
# Clean worker environment
$SIG{$_} = 'DEFAULT' for qw(CHLD INT TERM TTIN TTOU);
- $SIG{QUIT} = sub { $loop->concurrency(0) };
+ $SIG{QUIT} = sub { $loop->stop_gracefully };
delete @$self{qw(poll reader)};
srand;
diff --git a/t/mojo/ioloop.t b/t/mojo/ioloop.t
index f067e5c..6589af6 100644
--- a/t/mojo/ioloop.t
+++ b/t/mojo/ioloop.t
@@ -243,15 +243,16 @@ ok length($server) > length($server_after), 'stream has been resumed';
is $client, $client_after, 'stream was writable while paused';
is $client, 'works!', 'full message has been written';
-# Graceful shutdown (concurrency)
+# Graceful shutdown
$err = '';
-$loop = Mojo::IOLoop->new->concurrency(0);
+$loop = Mojo::IOLoop->new;
+$loop->stop_gracefully;
$loop->remove(
$loop->client({port => Mojo::IOLoop::Server->generate_port} => sub { }));
$loop->timer(3 => sub { shift->stop; $err = 'failed' });
$loop->start;
ok !$err, 'no error';
-is $loop->concurrency, 0, 'right value';
+is $loop->max_connections, 0, 'right value';
# Graceful shutdown (max_accepts)
$err = '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment