Permalink
Browse files

Implement psgix.harakiri. #329

  • Loading branch information...
1 parent f38a83c commit 64fd25c85d833c80f1862b103adbd8c5abc8ca76 @miyagawa miyagawa committed Sep 19, 2012
Showing with 65 additions and 0 deletions.
  1. +17 −0 lib/HTTP/Server/PSGI.pm
  2. +48 −0 t/HTTP-Server-PSGI/harakiri.t
View
@@ -120,12 +120,14 @@ sub accept_loop {
'psgi.multiprocess' => Plack::Util::FALSE,
'psgi.streaming' => Plack::Util::TRUE,
'psgi.nonblocking' => Plack::Util::FALSE,
+ 'psgix.harakiri' => Plack::Util::TRUE,
'psgix.input.buffered' => Plack::Util::TRUE,
'psgix.io' => $conn,
};
$self->handle_connection($env, $conn, $app);
$conn->close;
+ last if $env->{'psgix.harakiri.commit'};
}
}
}
@@ -326,6 +328,21 @@ ready for a production use.
L<HTTP::Server::PSGI> does B<NOT> support preforking. See L<Starman>
or L<Starlet> if you want a multi-process prefork web servers.
+=head1 HARAKIRI SUPPORT
+
+This web server supports `psgix.harakiri` extension defined in the
+L<PSGI::Extensions>.
+
+This application is a non-forking single process web server
+(i.e. `psgi.multiprocess` is false), and if your application commits
+harakiri, the entire web server stops too. In case this behavior is
+not what you want, be sure to check `psgi.multiprocess` as well to
+enable harakiri only in the preforking servers such as L<Starman>.
+
+On the other hand, this behavior might be handy if you want to embed
+this module in your application and serve HTTP requests for only short
+period of time, then go back to your main program.
+
=head1 AUTHOR
Kazuho Oku
@@ -0,0 +1,48 @@
+use strict;
+use warnings;
+
+use LWP::UserAgent;
+use Plack::Runner;
+use Test::More;
+use Test::TCP;
+
+test_tcp(
+ server => sub {
+ my $port = shift;
+ my $runner = Plack::Runner->new;
+ $runner->parse_options("--port" => $port, "-E", "dev", "-s", "HTTP::Server::PSGI");
+ $runner->run(
+ sub {
+ my $env = shift;
+ if ($env->{PATH_INFO} eq '/kill') {
+ $env->{'psgix.harakiri.commit'} = 1;
+ }
+ return [
+ 200,
+ [ 'Content-Type' => 'text/plain' ],
+ [ "Hi" ],
+ ];
+ },
+ );
+ sleep 5; # to block
+ },
+ client => sub {
+ my $port = shift;
+ my $ua = LWP::UserAgent->new;
+ my $res = $ua->get("http://127.0.0.1:$port/");
+ ok $res->is_success;
+ is $res->code, 200;
+ is $res->content, 'Hi';
+
+ $res = $ua->get("http://127.0.0.1:$port/kill");
+ ok $res->is_success;
+ is $res->code, 200;
+
+ note 'check that the server is dead';
+ $res = $ua->get("http://127.0.0.1:$port/");
+
+ ok !$res->is_success, "no response";
+ },
+);
+
+done_testing;

0 comments on commit 64fd25c

Please sign in to comment.