Skip to content

Instantly share code, notes, and snippets.

Created November 29, 2016 13: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/3c6775028f35610a1835ec640eada5e8 to your computer and use it in GitHub Desktop.
Save anonymous/3c6775028f35610a1835ec640eada5e8 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Server/Daemon.pm b/lib/Mojo/Server/Daemon.pm
index 39882df..cecdb5b 100644
--- a/lib/Mojo/Server/Daemon.pm
+++ b/lib/Mojo/Server/Daemon.pm
@@ -25,6 +25,13 @@ sub DESTROY {
$loop->remove($_) for keys %{$self->{connections} || {}}, @{$self->acceptors};
}
+sub close_idle {
+ my $self = shift;
+ my $c = $self->{connections};
+ my $loop = $self->ioloop;
+ !$c->{$_}{tx} and $c->{$_}{requests} and $loop->remove($_) for keys %$c;
+}
+
sub run {
my $self = shift;
@@ -455,6 +462,12 @@ Disable console messages.
L<Mojo::Server::Daemon> inherits all methods from L<Mojo::Server> and
implements the following new ones.
+=head2 close_idle
+
+ $daemon->close_idle;
+
+Close all connections without active requests.
+
=head2 run
$daemon->run;
diff --git a/lib/Mojo/Server/Prefork.pm b/lib/Mojo/Server/Prefork.pm
index bee879d..4cacd85 100644
--- a/lib/Mojo/Server/Prefork.pm
+++ b/lib/Mojo/Server/Prefork.pm
@@ -148,6 +148,7 @@ sub _spawn {
# Clean worker environment
$SIG{$_} = 'DEFAULT' for qw(CHLD INT TERM TTIN TTOU);
$SIG{QUIT} = sub { $loop->stop_gracefully };
+ $loop->on(finish => sub { $self->max_requests(1)->close_idle });
delete $self->{reader};
srand;
diff --git a/t/mojo/hypnotoad.t b/t/mojo/hypnotoad.t
index 2b17c99..bcabbf1 100644
--- a/t/mojo/hypnotoad.t
+++ b/t/mojo/hypnotoad.t
@@ -130,6 +130,12 @@ app->start;
EOF
open my $hot_deploy, '-|', $^X, "$prefix/hypnotoad", $script;
+# Wait for hot deployment to fail
+while (1) {
+ last if slurp($log) =~ qr/Zero downtime software upgrade failed/;
+ sleep 1;
+}
+
# Connection did not get lost
$tx = $ua->get("http://127.0.0.1:$port1/hello");
ok $tx->is_finished, 'transaction is finished';
@@ -146,12 +152,6 @@ ok $tx->kept_alive, 'connection was kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Hello Hypnotoad!', 'right content';
-# Wait for hot deployment to fail
-while (1) {
- last if slurp($log) =~ qr/Zero downtime software upgrade failed/;
- sleep 1;
-}
-
# Update script
spurt <<EOF, $script;
use Mojolicious::Lite;
@@ -178,25 +178,6 @@ app->start;
EOF
open $hot_deploy, '-|', $^X, "$prefix/hypnotoad", $script;
-# Connection did not get lost
-$tx = $ua->get("http://127.0.0.1:$port1/hello");
-ok $tx->is_finished, 'transaction is finished';
-ok $tx->keep_alive, 'connection will be kept alive';
-ok $tx->kept_alive, 'connection was kept alive';
-is $tx->res->code, 200, 'right status';
-is $tx->res->body, 'Hello Hypnotoad!', 'right content';
-
-# Connection did not get lost (second port)
-$tx = $ua->get("http://127.0.0.1:$port2/hello");
-ok $tx->is_finished, 'transaction is finished';
-ok $tx->keep_alive, 'connection will be kept alive';
-ok $tx->kept_alive, 'connection was kept alive';
-is $tx->res->code, 200, 'right status';
-is $tx->res->body, 'Hello Hypnotoad!', 'right content';
-
-# Remove keep-alive connections
-$ua = Mojo::UserAgent->new;
-
# Wait for hot deployment to finish
while (1) {
sleep 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment