/usr/share/perl5/Plack/Handler/Starlet.pm is in starlet 0.24-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package Plack::Handler::Starlet;
use strict;
use warnings;
use Parallel::Prefork;
use Server::Starter ();
use base qw(Starlet::Server);
sub new {
my ($klass, %args) = @_;
# setup before instantiation
if (defined $ENV{SERVER_STARTER_PORT}) {
$args{listens} = [];
my $server_ports = Server::Starter::server_ports();
for my $hostport (keys %$server_ports) {
my $fd = $server_ports->{$hostport};
my $listen = {};
if ($hostport =~ /(.*):(\d+)/) {
$listen->{host} = $1;
$listen->{port} = $2;
} else {
$listen->{port} = $hostport;
}
$listen->{sock} = IO::Socket::INET->new(
Proto => 'tcp',
) or die "failed to create socket:$!";
$listen->{sock}->fdopen($fd, 'w')
or die "failed to bind to listening socket:$!";
unless (@{$args{listens}}) {
$args{host} = $listen->{host};
$args{port} = $listen->{port};
}
$args{listens}[$fd] = $listen;
}
}
my $max_workers = 10;
for (qw(max_workers workers)) {
$max_workers = delete $args{$_}
if defined $args{$_};
}
# instantiate and set the variables
my $self = $klass->SUPER::new(%args);
$self->{is_multiprocess} = 1;
$self->{max_workers} = $max_workers;
$self;
}
sub run {
my($self, $app) = @_;
$self->setup_listener();
if ($self->{max_workers} != 0) {
# use Parallel::Prefork
my %pm_args = (
max_workers => $self->{max_workers},
trap_signals => {
TERM => 'TERM',
HUP => 'TERM',
},
);
if (defined $self->{spawn_interval}) {
$pm_args{trap_signals}{USR1} = [ 'TERM', $self->{spawn_interval} ];
$pm_args{spawn_interval} = $self->{spawn_interval};
}
if (defined $self->{err_respawn_interval}) {
$pm_args{err_respawn_interval} = $self->{err_respawn_interval};
}
my $pm = Parallel::Prefork->new(\%pm_args);
while ($pm->signal_received !~ /^(TERM|USR1)$/) {
$pm->start and next;
srand((rand() * 2 ** 30) ^ $$ ^ time);
$self->accept_loop($app, $self->_calc_reqs_per_child());
$pm->finish;
}
$pm->wait_all_children;
} else {
# run directly, mainly for debugging
local $SIG{TERM} = sub { exit 0; };
while (1) {
$self->accept_loop($app, $self->_calc_reqs_per_child());
}
}
}
sub _calc_reqs_per_child {
my $self = shift;
my $max = $self->{max_reqs_per_child};
if (my $min = $self->{min_reqs_per_child}) {
return $max - int(($max - $min + 1) * rand);
} else {
return $max;
}
}
1;
|