Skip to content

Commit

Permalink
less fugly
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Feb 18, 2010
1 parent 3c60835 commit 4146ee1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 68 deletions.
9 changes: 0 additions & 9 deletions lib/Plack/Handler/Twiggy.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ sub run {


my $class = $ENV{SERVER_STARTER_PORT} ? my $class = $ENV{SERVER_STARTER_PORT} ?
'Twiggy::Server::SS' : 'Twiggy::Server'; '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"; eval "require $class";
die if $@; die if $@;


Expand Down
61 changes: 51 additions & 10 deletions lib/Twiggy/Server.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ sub _create_tcp_server {
$port = $listen; $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; $self->_accept_prepare_handler;
} }


Expand All @@ -85,7 +86,7 @@ sub _accept_prepare_handler {
sub _accept_handler { sub _accept_handler {
my ( $self, $app, $is_tcp ) = @_; my ( $self, $app, $is_tcp ) = @_;


return sub { my $cb = sub {
my ( $sock, $peer_host, $peer_port ) = @_; my ( $sock, $peer_host, $peer_port ) = @_;


DEBUG && warn "[$$] $sock Accepted connection from $peer_host:$peer_port\n"; DEBUG && warn "[$$] $sock Accepted connection from $peer_host:$peer_port\n";
Expand Down Expand Up @@ -147,6 +148,22 @@ sub _accept_handler {
$self->_bad_request($sock); $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 # returns a closure that tries to parse
Expand Down Expand Up @@ -544,15 +561,39 @@ sub run {
my $self = shift; my $self = shift;
$self->register_service(@_); $self->register_service(@_);


my $exit = $self->{exit_guard} = AE::cv { if ($self->{workers}) {
# Make sure that we are not listening on a socket anymore, while require Parallel::Prefork;
# other events are being flushed
delete $self->{listen_guards};
};
$exit->begin;


my $w; $w = AE::signal QUIT => sub { $exit->end; undef $w }; $self->{max_requests} ||= 1000;
$exit->recv; 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; package Twiggy::Writer;
Expand Down
49 changes: 0 additions & 49 deletions lib/Twiggy/Server/PreFork.pm

This file was deleted.

1 change: 1 addition & 0 deletions t/anyevent_max_requests.t
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,6 @@
use strict; use strict;
use warnings; use warnings;
use Test::Requires qw(Parallel::Prefork);
use Test::More qw(no_diag); use Test::More qw(no_diag);
use Test::TCP; use Test::TCP;
use IO::Socket::INET; use IO::Socket::INET;
Expand Down

0 comments on commit 4146ee1

Please sign in to comment.