| 1 | package Plack::Handler::Starlet; | ||||||||||
| 2 | |||||||||||
| 3 | 55 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 55 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 55 | ▶ | use Parallel::Prefork; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 55 | ▶ | use Server::Starter (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 55 | ▶ | use base qw(Starlet::Server); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 55 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=12 SCAR=25.3
Pod
undocumented
| |||||||||||
| 11 | 55 | my ($klass, %args) = @_; | |||||||||
| 12 | |||||||||||
| 13 | # setup before instantiation | ||||||||||
| 14 | 55 | ▶ | if (defined $ENV{SERVER_STARTER_PORT}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 16 | $args{listens} = []; | |||||||||
| 16 | 16 | my $server_ports = Server::Starter::server_ports(); | |||||||||
| 17 | 16 | for my $hostport (keys %$server_ports) { | |||||||||
| 18 | 34 | my $fd = $server_ports->{$hostport}; | |||||||||
| 19 | 34 | my $listen = {}; | |||||||||
| 20 | 34 | ▶ | if ($hostport =~ /(.*):(\d+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | $listen->{host} = $1; | |||||||||
| 22 | 0 | $listen->{port} = $2; | |||||||||
| 23 | } else { | ||||||||||
| 24 | 34 | $listen->{port} = $hostport; | |||||||||
| 25 | } | ||||||||||
| 26 | 34 | ▶ | $listen->{sock} = IO::Socket::INET->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | Proto => 'tcp', | ||||||||||
| 28 | ) or die "failed to create socket:$!"; | ||||||||||
| 29 | 34 | ▶ | $listen->{sock}->fdopen($fd, 'w') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | or die "failed to bind to listening socket:$!"; | ||||||||||
| 31 | 34 | ▶ | unless (@{$args{listens}}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 16 | $args{host} = $listen->{host}; | |||||||||
| 33 | 16 | $args{port} = $listen->{port}; | |||||||||
| 34 | } | ||||||||||
| 35 | 34 | $args{listens}[$fd] = $listen; | |||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | 55 | my $max_workers = 10; | |||||||||
| 39 | 55 | for (qw(max_workers workers)) { | |||||||||
| 40 | $max_workers = delete $args{$_} | ||||||||||
| 41 | 110 | ▶ | if defined $args{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 55 | ▶ | if ($args{child_exit}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 2 | ▶ | $args{child_exit} = eval $args{child_exit} unless ref($args{child_exit}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | ▶ | die "child_exit is defined but not a code block" if ref($args{child_exit}) ne 'CODE'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # instantiate and set the variables | ||||||||||
| 50 | 55 | my $self = $klass->SUPER::new(%args); | |||||||||
| 51 | 55 | $self->{is_multiprocess} = 1; | |||||||||
| 52 | 55 | $self->{max_workers} = $max_workers; | |||||||||
| 53 | |||||||||||
| 54 | 55 | $self; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 54 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||
| 58 | 54 | my($self, $app) = @_; | |||||||||
| 59 | 54 | $self->setup_listener(); | |||||||||
| 60 | 54 | ▶ | if ($self->{max_workers} != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | # use Parallel::Prefork | ||||||||||
| 62 | my %pm_args = ( | ||||||||||
| 63 | max_workers => $self->{max_workers}, | ||||||||||
| 64 | 52 | trap_signals => { | |||||||||
| 65 | TERM => 'TERM', | ||||||||||
| 66 | HUP => 'TERM', | ||||||||||
| 67 | }, | ||||||||||
| 68 | ); | ||||||||||
| 69 | 52 | ▶ | if (defined $self->{spawn_interval}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 52 | $pm_args{trap_signals}{USR1} = [ 'TERM', $self->{spawn_interval} ]; | |||||||||
| 71 | 52 | $pm_args{spawn_interval} = $self->{spawn_interval}; | |||||||||
| 72 | } | ||||||||||
| 73 | 52 | ▶ | if (defined $self->{err_respawn_interval}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | $pm_args{err_respawn_interval} = $self->{err_respawn_interval}; | |||||||||
| 75 | } | ||||||||||
| 76 | 52 | my $pm = Parallel::Prefork->new(\%pm_args); | |||||||||
| 77 | 52 | ▶ | while ($pm->signal_received !~ /^(TERM|USR1)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 52 | ▶ | $pm->start and next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 29 | srand((rand() * 2 ** 30) ^ $$ ^ time); | |||||||||
| 80 | 29 | $self->accept_loop($app, $self->_calc_reqs_per_child()); | |||||||||
| 81 | 14 | $pm->finish; | |||||||||
| 82 | } | ||||||||||
| 83 | 23 | my $timeout = 1; | |||||||||
| 84 | 23 | ▶ | if ($self->{spawn_interval}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | $timeout = $self->{spawn_interval} * $self->{max_workers} | ||||||||||
| 86 | 2 | } | |||||||||
| 87 | 23 | ▶ | while ($pm->wait_all_children($timeout)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 19 | $pm->signal_all_children('TERM'); | |||||||||
| 89 | } | ||||||||||
| 90 | } else { | ||||||||||
| 91 | # run directly, mainly for debugging | ||||||||||
| 92 | 0 | ▶ | local $SIG{TERM} = sub { exit 0; }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 93 | 2 | while (1) { | |||||||||
| 94 | 2 | $self->accept_loop($app, $self->_calc_reqs_per_child()); | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 10031 | ▶ | sub _calc_reqs_per_child { | ||||||||
|
Subroutine
_calc_reqs_per_child: called CC=2 SCAR=6.9
| |||||||||||
| 100 | 10031 | my $self = shift; | |||||||||
| 101 | 10031 | my $max = $self->{max_reqs_per_child}; | |||||||||
| 102 | 10031 | ▶ | if (my $min = $self->{min_reqs_per_child}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 10000 | return $max - int(($max - $min + 1) * rand); | |||||||||
| 104 | } else { | ||||||||||
| 105 | 31 | return $max; | |||||||||
| 106 | } | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 55 | 1; | |||||||||