Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed small bug where Mojo::Server::Daemon was too eager to reconfigu…
…re Mojo::IOLoop
  • Loading branch information
kraih committed Jun 27, 2014
1 parent d28b63a commit 205a5e7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

5.10 2014-06-25
5.10 2014-06-27
- Fixed small bug where Mojo::Server::Daemon was too eager to reconfigure
Mojo::IOLoop.

5.09 2014-06-24
- Improved .ep templates to make the current controller available as $c.
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -8,12 +8,11 @@ use Scalar::Util 'weaken';
use constant DEBUG => $ENV{MOJO_DAEMON_DEBUG} || 0;

has acceptors => sub { [] };
has [qw(backlog silent)];
has [qw(backlog max_clients silent)];
has inactivity_timeout => sub { $ENV{MOJO_INACTIVITY_TIMEOUT} // 15 };
has ioloop => sub { Mojo::IOLoop->singleton };
has listen => sub { [split ',', $ENV{MOJO_LISTEN} || 'http://*:3000'] };
has max_clients => 1000;
has max_requests => 25;
has max_requests => 25;
has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };

sub DESTROY {
Expand Down Expand Up @@ -41,7 +40,7 @@ sub start {

# Start listening
else { $self->_listen($_) for @{$self->listen} }
$loop->max_connections($self->max_clients);
if (my $max = $self->max_clients) { $loop->max_connections($max) }

return $self;
}
Expand Down Expand Up @@ -401,7 +400,8 @@ TLS verification mode, defaults to C<0x03>.
my $max = $daemon->max_clients;
$daemon = $daemon->max_clients(1000);
Maximum number of concurrent client connections, defaults to C<1000>.
Maximum number of concurrent client connections, passed along to
L<Mojo::IOLoop/"max_connections">.
=head2 max_requests
Expand Down
63 changes: 36 additions & 27 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -243,42 +243,45 @@ L<Mojolicious::Guides::Cookbook/"Hypnotoad"> for examples.
accept_interval => 0.5
Interval in seconds for trying to reacquire the accept mutex, defaults to
C<0.025>. Note that changing this value can affect performance and idle CPU
usage.
Interval in seconds for trying to reacquire the accept mutex, defaults to the
value of L<Mojo::IOLoop/"accept_interval">. Note that changing this value can
affect performance and idle CPU usage.
=head2 accepts
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.
gracefully, defaults to the value of L<Mojo::Server::Prefork/"accepts">.
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 backlog
backlog => 128
Listen backlog size, defaults to C<SOMAXCONN>.
Listen backlog size, defaults to the value of
L<Mojo::Server::Daemon/"backlog">.
=head2 clients
clients => 100
Maximum number of concurrent client connections per worker process, defaults
to C<1000>. Note that high concurrency works best with applications that
perform mostly non-blocking operations, to optimize for blocking operations
you can decrease this value and increase L</"workers"> instead for better
performance.
to the value of L<Mojo::IOLoop/"max_connections">. Note that high concurrency
works best with applications that perform mostly non-blocking operations, to
optimize for blocking operations you can decrease this value and increase
L</"workers"> instead for better performance.
=head2 graceful_timeout
graceful_timeout => 15
Maximum amount of time in seconds stopping a worker gracefully may take before
being forced, defaults to C<20>.
being forced, defaults to the value of
L<Mojo::Server::Prefork/"graceful_timeout">.
=head2 group
Expand All @@ -290,28 +293,31 @@ Group name for worker processes.
heartbeat_interval => 3
Heartbeat interval in seconds, defaults to C<5>.
Heartbeat interval in seconds, defaults to the value of
L<Mojo::Server::Prefork/"heartbeat_interval">.
=head2 heartbeat_timeout
heartbeat_timeout => 2
Maximum amount of time in seconds before a worker without a heartbeat will be
stopped gracefully, defaults to C<20>.
stopped gracefully, defaults to the value of
L<Mojo::Server::Prefork/"heartbeat_timeout">.
=head2 inactivity_timeout
inactivity_timeout => 10
Maximum amount of time in seconds a connection can be inactive before getting
closed, defaults to C<15>. Setting the value to C<0> will allow connections to
be inactive indefinitely.
closed, defaults to the value of L<Mojo::Server::Daemon/"inactivity_timeout">.
Setting the value to C<0> will allow connections to be inactive indefinitely.
=head2 keep_alive_requests
keep_alive_requests => 50
Number of keep-alive requests per connection, defaults to C<25>.
Number of keep-alive requests per connection, defaults to the value of
L<Mojo::Server::Daemon/"max_requests">.
=head2 listen
Expand All @@ -325,21 +331,23 @@ also L<Mojo::Server::Daemon/"listen"> for more examples.
lock_file => '/tmp/hypnotoad.lock'
Full path of accept mutex lock file prefix, to which the process id will be
appended, defaults to a random temporary path.
appended, defaults to the value of L<Mojo::Server::Prefork/"lock_file">.
=head2 lock_timeout
lock_timeout => 0.5
Maximum amount of time in seconds a worker may block when waiting for the
accept mutex, defaults to C<1>. Note that changing this value can affect
performance and idle CPU usage.
accept mutex, defaults to the value of
L<Mojo::Server::Prefork/"lock_timeout">. Note that changing this value can
affect performance and idle CPU usage.
=head2 multi_accept
multi_accept => 100
Number of connections to accept at once, defaults to C<50>.
Number of connections to accept at once, defaults to the value of
L<Mojo::IOLoop/"multi_accept">.
=head2 pid_file
Expand All @@ -361,7 +369,7 @@ C<X-Forwarded-Proto> headers to be picked up automatically.
upgrade_timeout => 45
Maximum amount of time in seconds a zero downtime software upgrade may take
before getting canceled, defaults to C<60>.
before getting canceled, defaults to the value of L</"upgrade_timeout">.
=head2 user
Expand All @@ -373,10 +381,11 @@ Username for worker processes.
workers => 10
Number of worker processes, defaults to C<4>. A good rule of thumb is two
worker processes per CPU core for applications that perform mostly
non-blocking operations, blocking operations often require more and benefit
from decreasing the number of concurrent L</"clients"> (often as low as C<1>).
Number of worker processes, defaults to the value of
L<Mojo::Server::Prefork/"workers">. A good rule of thumb is two worker
processes per CPU core for applications that perform mostly non-blocking
operations, blocking operations often require more and benefit from decreasing
the number of concurrent L</"clients"> (often as low as C<1>).
=head1 ATTRIBUTES
Expand Down
25 changes: 13 additions & 12 deletions lib/Mojo/Server/Prefork.pm
Expand Up @@ -10,13 +10,12 @@ use POSIX 'WNOHANG';
use Scalar::Util 'weaken';
use Time::HiRes ();

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

Expand Down Expand Up @@ -68,7 +67,7 @@ sub run {
# Prepare lock file and event loop
$self->{lock_file} = $self->lock_file . ".$$";
my $loop = $self->ioloop->max_accepts($self->accepts);
$loop->$_($self->$_) for qw(accept_interval multi_accept);
$loop->$_($self->$_ // $loop->$_) 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 @@ -388,20 +387,21 @@ and implements the following new ones.
my $interval = $prefork->accept_interval;
$prefork = $prefork->accept_interval(0.5);
Interval in seconds for trying to reacquire the accept mutex, defaults to
C<0.025>. Note that changing this value can affect performance and idle CPU
usage.
Interval in seconds for trying to reacquire the accept mutex, passed along to
L<Mojo::IOLoop/"accept_interval">. 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.
gracefully, passed along to L<Mojo::IOLoop/"max_accepts">, 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
Expand Down Expand Up @@ -448,7 +448,8 @@ performance and idle CPU usage.
my $multi = $prefork->multi_accept;
$prefork = $prefork->multi_accept(100);
Number of connections to accept at once, defaults to C<50>.
Number of connections to accept at once, passed along to
L<Mojo::IOLoop/"multi_accept">.
=head2 pid_file
Expand Down
5 changes: 4 additions & 1 deletion t/mojo/daemon.t
Expand Up @@ -221,7 +221,9 @@ $daemon = Mojo::Server::Daemon->new(
silent => 1
);
is scalar @{$daemon->acceptors}, 0, 'no active acceptors';
$daemon->ioloop->max_connections(500);
$daemon->start;
is $daemon->ioloop->max_connections, 500, 'right number';
is scalar @{$daemon->acceptors}, 1, 'one active acceptor';
is $daemon->app->moniker, 'mojolicious', 'right moniker';
$port = Mojo::IOLoop->acceptor($daemon->acceptors->[0])->handle->sockport;
Expand All @@ -235,7 +237,8 @@ $tx = $ua->inactivity_timeout(0.5)
->get("http://127.0.0.1:$port/throttle2" => {Connection => 'close'});
ok !$tx->success, 'not successful';
is $tx->error->{message}, 'Inactivity timeout', 'right error';
$daemon->start;
$daemon->max_clients(600)->start;
is $daemon->ioloop->max_connections, 600, 'right number';
$tx = $ua->inactivity_timeout(10)
->get("http://127.0.0.1:$port/throttle3" => {Connection => 'close'});
ok $tx->success, 'successful';
Expand Down

0 comments on commit 205a5e7

Please sign in to comment.