Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

less fugly

  • Loading branch information...
commit 4146ee1362a9ccd36d57ebc1cdef67d96e647825 1 parent 3c60835
@lestrrat lestrrat authored
View
9 lib/Plack/Handler/Twiggy.pm
@@ -11,15 +11,6 @@ sub run {
my $class = $ENV{SERVER_STARTER_PORT} ?
'Twiggy::Server::SS' : 'Twiggy::Server';
- if (exists $self->{workers} && $self->{workers} > 0) {
- my $parent = $class;
- eval "require $parent";
- die if $@;
-
- $class = 'Twiggy::Server::PreFork';
- no strict 'refs';
- unshift @{$class . '::ISA'}, $parent;
- }
eval "require $class";
die if $@;
View
61 lib/Twiggy/Server.pm
@@ -61,7 +61,8 @@ sub _create_tcp_server {
$port = $listen;
}
- return tcp_server $host, $port, $self->_accept_handler($app, $is_tcp),
+ return tcp_server $host, $port,
+ $self->_accept_handler($app, $is_tcp),
$self->_accept_prepare_handler;
}
@@ -85,7 +86,7 @@ sub _accept_prepare_handler {
sub _accept_handler {
my ( $self, $app, $is_tcp ) = @_;
- return sub {
+ my $cb = sub {
my ( $sock, $peer_host, $peer_port ) = @_;
DEBUG && warn "[$$] $sock Accepted connection from $peer_host:$peer_port\n";
@@ -147,6 +148,22 @@ sub _accept_handler {
$self->_bad_request($sock);
}
};
+ if ($self->{workers}) {
+ return sub {
+ $self->{reqs_per_child}++;
+ eval {
+ $cb->(@_);
+ };
+ my $e = $@;
+
+ if ($self->{reqs_per_child} > $self->{max_requests}) {
+ Twiggy::Server::DEBUG && warn "[$$] max requests ( $self->{max_requests}) reached";
+ my $cv = $self->{exit_guard};
+ $cv->end;
+ }
+ };
+ }
+ return $cb;
}
# returns a closure that tries to parse
@@ -544,15 +561,39 @@ sub run {
my $self = shift;
$self->register_service(@_);
- my $exit = $self->{exit_guard} = AE::cv {
- # Make sure that we are not listening on a socket anymore, while
- # other events are being flushed
- delete $self->{listen_guards};
- };
- $exit->begin;
+ if ($self->{workers}) {
+ require Parallel::Prefork;
- my $w; $w = AE::signal QUIT => sub { $exit->end; undef $w };
- $exit->recv;
+ $self->{max_requests} ||= 1000;
+ my $pm = Parallel::Prefork->new({
+ max_workers => $self->{workers},
+ trap_signals => {
+ TERM => 'TERM',
+ HUP => 'TERM',
+ },
+ });
+ while ($pm->signal_received ne 'TERM') {
+ $pm->start and next;
+ Twiggy::Server::DEBUG && warn "[$$] start";
+ my $exit = $self->{exit_guard} = AE::cv;
+ $exit->begin;
+ my $w; $w = AE::signal TERM => sub { $exit->end; undef $w };
+ $exit->recv;
+ Twiggy::Server::DEBUG && warn "[$$] finish";
+ $pm->finish;
+ }
+ $pm->wait_all_children;
+ } else {
+ my $exit = $self->{exit_guard} = AE::cv {
+ # Make sure that we are not listening on a socket anymore, while
+ # other events are being flushed
+ delete $self->{listen_guards};
+ };
+ $exit->begin;
+
+ my $w; $w = AE::signal QUIT => sub { $exit->end; undef $w };
+ $exit->recv;
+ }
}
package Twiggy::Writer;
View
49 lib/Twiggy/Server/PreFork.pm
@@ -1,49 +0,0 @@
-package Twiggy::Server::PreFork;
-use strict;
-use Parallel::Prefork;
-
-sub _accept_handler {
- my $self = shift;
-
- my $cb = $self->SUPER::_accept_handler(@_);
- return sub {
- $self->{reqs_per_child}++;
- eval {
- $cb->(@_);
- };
- my $e = $@;
-
- if ($self->{reqs_per_child} > $self->{max_requests}) {
- Twiggy::Server::DEBUG && warn "[$$] max requests ( $self->{max_requests}) reached";
- my $cv = $self->{exit_guard};
- $cv->end;
- }
- };
-}
-
-sub run {
- my $self = shift;
- $self->register_service(@_);
-
- $self->{max_requests} ||= 1000;
- my $pm = Parallel::Prefork->new({
- max_workers => $self->{workers},
- trap_signals => {
- TERM => 'TERM',
- HUP => 'TERM',
- },
- });
- while ($pm->signal_received ne 'TERM') {
- $pm->start and next;
- Twiggy::Server::DEBUG && warn "[$$] start";
- my $exit = $self->{exit_guard} = AE::cv;
- $exit->begin;
- my $w; $w = AE::signal TERM => sub { $exit->end; undef $w };
- $exit->recv;
- Twiggy::Server::DEBUG && warn "[$$] finish";
- $pm->finish;
- }
- $pm->wait_all_children;
-}
-
-1;
View
1  t/anyevent_max_requests.t
@@ -1,5 +1,6 @@
use strict;
use warnings;
+use Test::Requires qw(Parallel::Prefork);
use Test::More qw(no_diag);
use Test::TCP;
use IO::Socket::INET;
Please sign in to comment.
Something went wrong with that request. Please try again.