Skip to content

Commit

Permalink
allow more workers to be spawned temporarily if there is a need
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 4, 2017
1 parent c85b189 commit f5fecd3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@

7.35 2017-07-04
- Removed deprecated watch attribute from Mojo::Server::Morbo.
- Added overload attribute to Mojo::Server::Prefork.
- Added -o option to prefork command.
- Added overload setting to Hypnotoad.
- Increased default graceful_timeout from 20 to 60 seconds in
Mojo::Server::Prefork.

Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -26,7 +26,7 @@ sub configure {
$prefork->max_requests($c->{requests}) if $c->{requests};
defined $c->{$_} and $prefork->$_($c->{$_})
for qw(accepts backlog graceful_timeout heartbeat_interval),
qw(heartbeat_timeout inactivity_timeout listen pid_file workers);
qw(heartbeat_timeout inactivity_timeout listen overload pid_file workers);
}

sub run {
Expand Down Expand Up @@ -315,6 +315,14 @@ Setting the value to C<0> will allow connections to be inactive indefinitely.
Array reference with one or more locations to listen on, defaults to
C<http://*:8080>. See also L<Mojo::Server::Daemon/"listen"> for more examples.
=head2 overload
overload => 4
Temporarily spawn up to this number of additional workers if there is a need,
for example if too many workers are shutting down gracefully at the same time,
defaults to the value of L<Mojo::Server::Prefork/"overload">.
=head2 pid_file
pid_file => '/var/run/hypnotoad.pid'
Expand Down
16 changes: 15 additions & 1 deletion lib/Mojo/Server/Prefork.pm
Expand Up @@ -13,6 +13,7 @@ has cleanup => 1;
has graceful_timeout => 60;
has heartbeat_timeout => 20;
has heartbeat_interval => 5;
has overload => 2;
has pid_file => sub { path(tmpdir, 'prefork.pid')->to_string };
has workers => 4;

Expand Down Expand Up @@ -92,7 +93,11 @@ sub _manage {

# Spawn more workers if necessary and check PID file
if (!$self->{finished}) {
$self->_spawn while keys %{$self->{pool}} < $self->workers;
my $graceful = grep { $_->{graceful} } values %{$self->{pool}};
my $overload = $self->overload;
$overload = $graceful ? $graceful > $overload ? $overload : $graceful : 0;
my $need = ($self->workers - keys %{$self->{pool}}) + $overload;
$self->_spawn while $need-- > 0;
$self->ensure_pid_file($$);
}

Expand Down Expand Up @@ -397,6 +402,15 @@ stopped gracefully, defaults to C<20>. Note that this value should usually be a
little larger than the maximum amount of time you expect any one operation to
block the event loop.
=head2 overload
my $overload = $prefork->overload;
$prefork = $prefork->overload(4);
Temporarily spawn up to this number of additional workers if there is a need,
for example if too many workers are shutting down gracefully at the same time,
defaults to C<2>.
=head2 pid_file
my $file = $prefork->pid_file;
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Command/prefork.pm
Expand Up @@ -21,6 +21,7 @@ sub run {
'H|heartbeat-timeout=i' => sub { $prefork->heartbeat_timeout($_[1]) },
'i|inactivity-timeout=i' => sub { $prefork->inactivity_timeout($_[1]) },
'l|listen=s' => \my @listen,
'o|overload=i' => sub { $prefork->overload($_[1]) },
'P|pid-file=s' => sub { $prefork->pid_file($_[1]) },
'p|proxy' => sub { $prefork->reverse_proxy(1) },
'r|requests=i' => sub { $prefork->max_requests($_[1]) },
Expand Down Expand Up @@ -70,6 +71,8 @@ Mojolicious::Command::prefork - Pre-fork command
-m, --mode <name> Operating mode for your application,
defaults to the value of
MOJO_MODE/PLACK_ENV or "development"
-o, --overload <number> Temporarily spawn up to this number of
additional workers, defaults to 2
-P, --pid-file <path> Path to process id file, defaults to
"prefork.pid" in a temporary diretory
-p, --proxy Activate reverse proxy support,
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/hypnotoad.t
Expand Up @@ -26,6 +26,7 @@ use Mojo::UserAgent;
heartbeat_timeout => 9,
inactivity_timeout => 5,
listen => ['http://*:8081'],
overload => 4,
pid_file => '/foo/bar.pid',
proxy => 1,
requests => 3,
Expand All @@ -45,6 +46,7 @@ use Mojo::UserAgent;
is_deeply $hypnotoad->prefork->listen, ['http://*:8081'], 'right value';
is $hypnotoad->prefork->max_clients, 1, 'right value';
is $hypnotoad->prefork->max_requests, 3, 'right value';
is $hypnotoad->prefork->overload, 4, 'right value';
is $hypnotoad->prefork->pid_file, '/foo/bar.pid', 'right value';
ok $hypnotoad->prefork->reverse_proxy, 'reverse proxy enabled';
is $hypnotoad->prefork->workers, 7, 'right value';
Expand Down

0 comments on commit f5fecd3

Please sign in to comment.