Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow Mojo::Server::Prefork to restart workers automatically
  • Loading branch information
kraih committed Jan 16, 2013
1 parent c41bc64 commit b694b2b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
17 changes: 6 additions & 11 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -71,20 +71,15 @@ sub _config {
$prefork->pid_file($c->{pid_file}
|| catfile(dirname($ENV{HYPNOTOAD_APP}), 'hypnotoad.pid'));
my @settings = (
qw(backlog graceful_timeout group heartbeat_interval),
qw(heartbeat_timeout lock_file lock_timeout user workers)
qw(accept_interval accepts backlog graceful_timeout group),
qw(heartbeat_interval heartbeat_timeout inactivity_timeout lock_file),
qw(lock_timeout multi_accept user workers)
);
defined $c->{$_} and $prefork->$_($c->{$_}) for @settings;
$prefork->max_clients($c->{clients} || 1000);
$prefork->max_requests($c->{keep_alive_requests} || 25);
$prefork->inactivity_timeout($c->{inactivity_timeout} // 15);
$prefork->max_clients($c->{clients}) if $c->{clients};
$prefork->max_requests($c->{keep_alive_requests})
if $c->{keep_alive_requests};
$prefork->listen($c->{listen} || ['http://*:8080']);

# Event loop settings
my $loop = $prefork->ioloop;
$loop->max_accepts($c->{accepts} // 1000);
defined $c->{$_} and $loop->$_($c->{$_})
for qw(accept_interval multi_accept);
}

sub _exit { say shift and exit 0 }
Expand Down
32 changes: 32 additions & 0 deletions lib/Mojo/Server/Prefork.pm
Expand Up @@ -9,10 +9,13 @@ use POSIX qw(setsid WNOHANG);
use Scalar::Util 'weaken';
use Time::HiRes 'ualarm';

has accepts => 1000;
has accept_interval => 0.025;
has [qw(graceful_timeout heartbeat_timeout)] => 20;
has heartbeat_interval => 5;
has lock_file => sub { catfile tmpdir, 'hypnotoad.lock' };
has lock_timeout => 0.5;
has multi_accept => 50;
has pid_file => sub { catfile tmpdir, 'hypnotoad.pid' };
has workers => 4;

Expand Down Expand Up @@ -49,6 +52,8 @@ sub run {
# Preload application and start accepting connections
$self->{lock_file} = $self->lock_file . ".$$";
$self->start->app;
my $loop = $self->ioloop->max_accepts($self->accepts);
$loop->$_($self->$_) for qw(accept_interval multi_accept);

# Pipe for worker communication
pipe($self->{reader}, $self->{writer}) or die "Can't create pipe: $!";
Expand Down Expand Up @@ -362,6 +367,26 @@ Emitted when the manager starts waiting for new heartbeat messages.
L<Mojo::Server::Prefork> inherits all attributes from L<Mojo::Server::Daemon>
and implements the following new ones.
=head2 accept_interval
my $interval = $prefork->accept_interval;
$prefork = $prefork->accept_interval(0.5);
Interval in seconds for trying to reacquire the accept mutex and connection
management, defaults to C<0.025>. Note that changing this value can affect
performance and idle CPU usage.
=head2 accepts
my $accepts = $prefork->accepts;
$prefork = $prefork->accepts(100);
Maximum number of connections a worker is allowed to accept before stopping
gracefully, defaults to C<1000>. Setting the value to C<0> will allow workers
to accept new connections indefinitely. Note that up to half of this value can
be subtracted randomly to improve load balancing, and that worker processes
will stop sending heartbeat messages once the limit has been reached.
=head2 graceful_timeout
my $timeout = $prefork->graceful_timeout;
Expand Down Expand Up @@ -401,6 +426,13 @@ appended, defaults to a random temporary path.
Maximum amount of time in seconds a worker may block when waiting for the
accept mutex, defaults to C<0.5>.
=head2 multi_accept
my $multi = $prefork->multi_accept;
$prefork = $prefork->multi_accept(100);
Number of connections to accept at once, defaults to C<50>.
=head2 pid_file
my $file = $prefork->pid_file;
Expand Down
15 changes: 12 additions & 3 deletions lib/Mojolicious/Command/prefork.pm
Expand Up @@ -10,6 +10,10 @@ has usage => <<"EOF";
usage: $0 prefork [OPTIONS]
These options are available:
-A, --accepts <number> Set number of connections a worker is
allowed to accept, defaults to 1000.
-a, --accept-interval <seconds> Set interval for reacquiring the accept
mutex, defaults to 0.025.
-b, --backlog <size> Set listen backlog size, defaults to
SOMAXCONN.
-c, --clients <number> Set maximum number of concurrent
Expand All @@ -26,6 +30,8 @@ These options are available:
-l, --listen <location> Set one or more locations you want to
listen on, defaults to the value of
MOJO_LISTEN or "http://*:3000".
--multi-accept <number> Set number of connection to acceot at
once, defaults to 50.
-P, --pid-file <path> Set path to process id file, defaults
to a random file.
-p, --proxy Activate reverse proxy support,
Expand All @@ -43,6 +49,8 @@ sub run {
# Options
my $prefork = Mojo::Server::Prefork->new(app => $self->app);
GetOptionsFromArray \@args,
'A|accepts=i' => sub { $prefork->accepts($_[1]) },
'a|accept-interval=i' => sub { $prefork->accept_interval($_[1]) },
'b|backlog=i' => sub { $prefork->backlog($_[1]) },
'c|clients=i' => sub { $prefork->max_clients($_[1]) },
'G|graceful-timeout=i' => sub { $prefork->graceful_timeout($_[1]) },
Expand All @@ -52,9 +60,10 @@ sub run {
'i|inactivity=i' => sub { $prefork->inactivity_timeout($_[1]) },
'lock-file=i' => sub { $prefork->lock_file($_[1]) },
'L|lock-timeout=i' => sub { $prefork->lock_timeout($_[1]) },
'l|listen=s' => \my @listen,
'P|pid-file=i' => sub { $prefork->pid_file($_[1]) },
'p|proxy' => sub { $ENV{MOJO_REVERSE_PROXY} = 1 },
'l|listen=s' => \my @listen,
'multi-accept=i' => sub { $prefork->multi_accept($_[1]) },
'P|pid-file=i' => sub { $prefork->pid_file($_[1]) },
'p|proxy' => sub { $ENV{MOJO_REVERSE_PROXY} = 1 },
'r|requests=i' => sub { $prefork->max_requests($_[1]) },
'u|user=s' => sub { $prefork->user($_[1]) },
'w|workers=i' => sub { $prefork->workers($_[1]) };
Expand Down

0 comments on commit b694b2b

Please sign in to comment.