Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ignore first SIGTERM in worker processes while handling an HTTP request

  • Loading branch information...
commit fc4dcc061d6a2e1a8b37aa0e899f1e25c1eceb79 1 parent 0d392f7
@kazuho authored
Showing with 17 additions and 3 deletions.
  1. +1 −1  lib/Plack/Handler/Starlet.pm
  2. +16 −2 lib/Starlet/Server.pm
View
2  lib/Plack/Handler/Starlet.pm
@@ -45,7 +45,7 @@ sub run {
max_workers => $self->{max_workers},
trap_signals => {
TERM => 'TERM',
- HUP => 'TERM',
+ HUP => 'TERM',
},
});
while ($pm->signal_received ne 'TERM') {
View
18 lib/Starlet/Server.pm
@@ -125,15 +125,25 @@ sub accept_loop {
sub handle_connection {
my($self, $env, $conn, $app, $use_keepalive, $is_keepalive) = @_;
-
+
my $buf = '';
my $res = [ 400, [ 'Content-Type' => 'text/plain' ], [ 'Bad Request' ] ];
-
+
+ my $can_exit = 1;
+ my $term_received = 0;
+ local $SIG{TERM} = sub {
+ $term_received++;
+ exit 0
+ if ($is_keepalive && $can_exit) || $term_received > 1;
+ # warn "server termination delayed while handling current HTTP request";
+ };
+
while (1) {
my $rlen = $self->read_timeout(
$conn, \$buf, MAX_REQUEST_SIZE - length($buf), length($buf),
$is_keepalive ? $self->{keepalive_timeout} : $self->{timeout},
) or return;
+ undef $can_exit;
my $reqlen = parse_http_request($buf, $env);
if ($reqlen >= 0) {
# handle request
@@ -186,6 +196,10 @@ sub handle_connection {
die "Bad response $res";
}
+ if ($term_received) {
+ exit 0;
+ }
+
return $use_keepalive;
}
Please sign in to comment.
Something went wrong with that request. Please try again.