Skip to content

Commit

Permalink
make Morbo restart a little faster
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 22, 2015
1 parent 7c6d324 commit cd309ef
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

5.82 2015-02-22
- Deprecated keep_alive_requests setting in Hypnotoad in favor of requests.
- Improved Morbo to restart slightly faster.

5.81 2015-02-20
- Deprecated object-oriented Mojo::Loader API.
Expand Down
29 changes: 17 additions & 12 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -7,7 +7,7 @@ use Cwd 'abs_path';
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use Mojo::Server::Prefork;
use Mojo::Util 'steady_time';
use Mojo::Util qw(deprecated steady_time);
use Scalar::Util 'weaken';

has prefork => sub { Mojo::Server::Prefork->new };
Expand All @@ -22,11 +22,16 @@ sub configure {
$c->{listen} ||= ['http://*:8080'];
$self->upgrade_timeout($c->{upgrade_timeout}) if $c->{upgrade_timeout};

# Prefork settings
$prefork->reverse_proxy($c->{proxy}) if defined $c->{proxy};
$prefork->max_clients($c->{clients}) if $c->{clients};
$prefork->max_requests($c->{keep_alive_requests})
# DEPRECATED in Tiger Face!
deprecated
'The keep_alive_requests setting is DEPRECATED in favor of requests'
and $c->{requests} = $c->{keep_alive_requests}
if $c->{keep_alive_requests};

# Prefork settings
$prefork->reverse_proxy($c->{proxy}) if defined $c->{proxy};
$prefork->max_clients($c->{clients}) if $c->{clients};
$prefork->max_requests($c->{requests}) if $c->{requests};
defined $c->{$_} and $prefork->$_($c->{$_})
for qw(accepts backlog graceful_timeout group heartbeat_interval),
qw(heartbeat_timeout inactivity_timeout listen multi_accept pid_file),
Expand Down Expand Up @@ -302,13 +307,6 @@ Maximum amount of time in seconds a connection can be inactive before getting
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 the value of
L<Mojo::Server::Daemon/"max_requests">.
=head2 listen
listen => ['http://*:80']
Expand Down Expand Up @@ -339,6 +337,13 @@ Activate reverse proxy support, which allows for the C<X-Forwarded-For> and
C<X-Forwarded-Proto> headers to be picked up automatically, defaults to the
value of L<Mojo::Server/"reverse_proxy">.
=head2 requests
requests => 50
Number of keep-alive requests per connection, defaults to the value of
L<Mojo::Server::Daemon/"max_requests">.
=head2 upgrade_timeout
upgrade_timeout => 45
Expand Down
9 changes: 3 additions & 6 deletions lib/Mojo/Server/Morbo.pm
Expand Up @@ -40,7 +40,7 @@ sub run {
$self->{modified} = 1;

# Prepare and cache listen sockets for smooth restarting
my $daemon = Mojo::Server::Daemon->new(silent => 1)->start->stop;
$self->{daemon} = Mojo::Server::Daemon->new->start->stop;

$self->_manage while !$self->{finished} || $self->{worker};
exit 0;
Expand Down Expand Up @@ -82,18 +82,15 @@ sub _spawn {

# Manager
my $manager = $$;
$ENV{MORBO_REV}++;
die "Can't fork: $!" unless defined(my $pid = $self->{worker} = fork);
return if $pid;

# Worker
$SIG{CHLD} = 'DEFAULT';
$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { $self->{finished} = 1 };
my $daemon = Mojo::Server::Daemon->new;
my $daemon = $self->{daemon};
$daemon->load_app($self->watch->[0]);
$daemon->silent(1) if $ENV{MORBO_REV} > 1;
$daemon->start;
my $loop = $daemon->ioloop;
my $loop = $daemon->start->ioloop;
$loop->recurring(
1 => sub { shift->stop if !kill(0, $manager) || $self->{finished} });
$loop->start;
Expand Down
32 changes: 16 additions & 16 deletions t/mojo/hypnotoad.t
Expand Up @@ -20,22 +20,22 @@ use Mojo::Util qw(slurp spurt);
{
my $hypnotoad = Mojo::Server::Hypnotoad->new;
$hypnotoad->prefork->app->config->{myserver} = {
accepts => 13,
backlog => 43,
clients => 1,
graceful_timeout => 23,
group => 'testers',
heartbeat_interval => 7,
heartbeat_timeout => 9,
keep_alive_requests => 3,
inactivity_timeout => 5,
listen => ['http://*:8081'],
multi_accept => 16,
pid_file => '/foo/bar.pid',
proxy => 1,
upgrade_timeout => 45,
user => 'tester',
workers => 7
accepts => 13,
backlog => 43,
clients => 1,
graceful_timeout => 23,
group => 'testers',
heartbeat_interval => 7,
heartbeat_timeout => 9,
inactivity_timeout => 5,
listen => ['http://*:8081'],
multi_accept => 16,
pid_file => '/foo/bar.pid',
proxy => 1,
requests => 3,
upgrade_timeout => 45,
user => 'tester',
workers => 7
};
is $hypnotoad->upgrade_timeout, 60, 'right default';
$hypnotoad->configure('test');
Expand Down

0 comments on commit cd309ef

Please sign in to comment.