Skip to content
This repository
Browse code

less fugly

  • Loading branch information...
commit 4146ee1362a9ccd36d57ebc1cdef67d96e647825 1 parent 3c60835
lestrrat lestrrat authored
9 lib/Plack/Handler/Twiggy.pm
@@ -11,15 +11,6 @@ sub run {
11 11
12 12 my $class = $ENV{SERVER_STARTER_PORT} ?
13 13 'Twiggy::Server::SS' : 'Twiggy::Server';
14   - if (exists $self->{workers} && $self->{workers} > 0) {
15   - my $parent = $class;
16   - eval "require $parent";
17   - die if $@;
18   -
19   - $class = 'Twiggy::Server::PreFork';
20   - no strict 'refs';
21   - unshift @{$class . '::ISA'}, $parent;
22   - }
23 14 eval "require $class";
24 15 die if $@;
25 16
61 lib/Twiggy/Server.pm
@@ -61,7 +61,8 @@ sub _create_tcp_server {
61 61 $port = $listen;
62 62 }
63 63
64   - return tcp_server $host, $port, $self->_accept_handler($app, $is_tcp),
  64 + return tcp_server $host, $port,
  65 + $self->_accept_handler($app, $is_tcp),
65 66 $self->_accept_prepare_handler;
66 67 }
67 68
@@ -85,7 +86,7 @@ sub _accept_prepare_handler {
85 86 sub _accept_handler {
86 87 my ( $self, $app, $is_tcp ) = @_;
87 88
88   - return sub {
  89 + my $cb = sub {
89 90 my ( $sock, $peer_host, $peer_port ) = @_;
90 91
91 92 DEBUG && warn "[$$] $sock Accepted connection from $peer_host:$peer_port\n";
@@ -147,6 +148,22 @@ sub _accept_handler {
147 148 $self->_bad_request($sock);
148 149 }
149 150 };
  151 + if ($self->{workers}) {
  152 + return sub {
  153 + $self->{reqs_per_child}++;
  154 + eval {
  155 + $cb->(@_);
  156 + };
  157 + my $e = $@;
  158 +
  159 + if ($self->{reqs_per_child} > $self->{max_requests}) {
  160 + Twiggy::Server::DEBUG && warn "[$$] max requests ( $self->{max_requests}) reached";
  161 + my $cv = $self->{exit_guard};
  162 + $cv->end;
  163 + }
  164 + };
  165 + }
  166 + return $cb;
150 167 }
151 168
152 169 # returns a closure that tries to parse
@@ -544,15 +561,39 @@ sub run {
544 561 my $self = shift;
545 562 $self->register_service(@_);
546 563
547   - my $exit = $self->{exit_guard} = AE::cv {
548   - # Make sure that we are not listening on a socket anymore, while
549   - # other events are being flushed
550   - delete $self->{listen_guards};
551   - };
552   - $exit->begin;
  564 + if ($self->{workers}) {
  565 + require Parallel::Prefork;
553 566
554   - my $w; $w = AE::signal QUIT => sub { $exit->end; undef $w };
555   - $exit->recv;
  567 + $self->{max_requests} ||= 1000;
  568 + my $pm = Parallel::Prefork->new({
  569 + max_workers => $self->{workers},
  570 + trap_signals => {
  571 + TERM => 'TERM',
  572 + HUP => 'TERM',
  573 + },
  574 + });
  575 + while ($pm->signal_received ne 'TERM') {
  576 + $pm->start and next;
  577 + Twiggy::Server::DEBUG && warn "[$$] start";
  578 + my $exit = $self->{exit_guard} = AE::cv;
  579 + $exit->begin;
  580 + my $w; $w = AE::signal TERM => sub { $exit->end; undef $w };
  581 + $exit->recv;
  582 + Twiggy::Server::DEBUG && warn "[$$] finish";
  583 + $pm->finish;
  584 + }
  585 + $pm->wait_all_children;
  586 + } else {
  587 + my $exit = $self->{exit_guard} = AE::cv {
  588 + # Make sure that we are not listening on a socket anymore, while
  589 + # other events are being flushed
  590 + delete $self->{listen_guards};
  591 + };
  592 + $exit->begin;
  593 +
  594 + my $w; $w = AE::signal QUIT => sub { $exit->end; undef $w };
  595 + $exit->recv;
  596 + }
556 597 }
557 598
558 599 package Twiggy::Writer;
49 lib/Twiggy/Server/PreFork.pm
... ... @@ -1,49 +0,0 @@
1   -package Twiggy::Server::PreFork;
2   -use strict;
3   -use Parallel::Prefork;
4   -
5   -sub _accept_handler {
6   - my $self = shift;
7   -
8   - my $cb = $self->SUPER::_accept_handler(@_);
9   - return sub {
10   - $self->{reqs_per_child}++;
11   - eval {
12   - $cb->(@_);
13   - };
14   - my $e = $@;
15   -
16   - if ($self->{reqs_per_child} > $self->{max_requests}) {
17   - Twiggy::Server::DEBUG && warn "[$$] max requests ( $self->{max_requests}) reached";
18   - my $cv = $self->{exit_guard};
19   - $cv->end;
20   - }
21   - };
22   -}
23   -
24   -sub run {
25   - my $self = shift;
26   - $self->register_service(@_);
27   -
28   - $self->{max_requests} ||= 1000;
29   - my $pm = Parallel::Prefork->new({
30   - max_workers => $self->{workers},
31   - trap_signals => {
32   - TERM => 'TERM',
33   - HUP => 'TERM',
34   - },
35   - });
36   - while ($pm->signal_received ne 'TERM') {
37   - $pm->start and next;
38   - Twiggy::Server::DEBUG && warn "[$$] start";
39   - my $exit = $self->{exit_guard} = AE::cv;
40   - $exit->begin;
41   - my $w; $w = AE::signal TERM => sub { $exit->end; undef $w };
42   - $exit->recv;
43   - Twiggy::Server::DEBUG && warn "[$$] finish";
44   - $pm->finish;
45   - }
46   - $pm->wait_all_children;
47   -}
48   -
49   -1;
1  t/anyevent_max_requests.t
... ... @@ -1,5 +1,6 @@
1 1 use strict;
2 2 use warnings;
  3 +use Test::Requires qw(Parallel::Prefork);
3 4 use Test::More qw(no_diag);
4 5 use Test::TCP;
5 6 use IO::Socket::INET;

1 comment on commit 4146ee1

Ask Bjørn Hansen
abh commented on 4146ee1

Any plans for merging this into the main branch? Or should/will Starman get an Anyevent mode instead? It'd make Twiggy useful/deployable for a bit more applications...

Please sign in to comment.
Something went wrong with that request. Please try again.