| 1 | package Plack::Loader::Shotgun; | ||||||||||||||||||||||||||||
| 2 | 38 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 38 | ▶ | use parent qw(Plack::Loader); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use Storable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 38 | ▶ | use Plack::Middleware::BufferedStreaming; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 38 | ▶ | die <<DIE if $^O eq 'MSWin32' && !$ENV{PLACK_SHOTGUN_MEMORY_LEAK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O eq "MSWin32" and not $ENV{"PLACK_SHOTGUN_MEMORY_LEAK"}
MC/DC0%0/2
$^O eq "MSWin32" and not $ENV{"PLACK_SHOTGUN_MEMORY_LEAK"}
$^O eq "MSWin32"
not $ENV{"PLACK_SHOTGUN_MEMORY_LEAK"}
Truth table33%1/3
A and not(B)
A= $^O eq "MSWin32"B= not $ENV{"PLACK_SHOTGUN_MEMORY_LEAK"}
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Shotgun loader uses fork(2) system call to create a fresh Perl interpreter, that is known to not work | ||||||||||||||||||||||||||||
| 11 | properly in a fork-emulation layer on Windows and cause huge memory leaks. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | If you're aware of this and still want to run the loader, run it with the environment variable | ||||||||||||||||||||||||||||
| 14 | PLACK_SHOTGUN_MEMORY_LEAK on. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | DIE | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 37 | ▶ | sub preload_app { | ||||||||||||||||||||||||||
|
Subroutine
preload_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 37 | my($self, $builder) = @_; | |||||||||||||||||||||||||||
| 20 | 37 | ▶ | $self->{builder} = sub { Plack::Middleware::BufferedStreaming->wrap($builder->()) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 37 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 37 | my($self, $server) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 703 | ▶ | my $app = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 703 | my $env = shift; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 703 | pipe my $read, my $write; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 703 | my $pid = fork; | |||||||||||||||||||||||||||
| 32 | 703 | ▶ | if ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | # parent | ||||||||||||||||||||||||||||
| 34 | 666 | close $write; | |||||||||||||||||||||||||||
| 35 | 666 | my $res = Storable::thaw(join '', <$read>); | |||||||||||||||||||||||||||
| 36 | 666 | close $read; | |||||||||||||||||||||||||||
| 37 | 666 | waitpid($pid, 0); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 666 | return $res; | |||||||||||||||||||||||||||
| 40 | } else { | ||||||||||||||||||||||||||||
| 41 | # child | ||||||||||||||||||||||||||||
| 42 | 37 | close $read; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 37 | my $res; | |||||||||||||||||||||||||||
| 45 | 37 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 37 | $env->{'psgi.streaming'} = 0; | |||||||||||||||||||||||||||
| 47 | 37 | $res = $self->{builder}->()->($env); | |||||||||||||||||||||||||||
| 48 | 36 | my @body; | |||||||||||||||||||||||||||
| 49 | 36 | ▶ | Plack::Util::foreach($res->[2], sub { push @body, $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 36 | $res->[2] = \@body; | |||||||||||||||||||||||||||
| 51 | 1 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | $env->{'psgi.errors'}->print($_); | |||||||||||||||||||||||||||
| 53 | 1 | $res = [ 500, [ "Content-Type", "text/plain" ], [ "Internal Server Error" ] ]; | |||||||||||||||||||||||||||
| 54 | 37 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 37 | print {$write} Storable::freeze($res); | |||||||||||||||||||||||||||
| 57 | 37 | close $write; | |||||||||||||||||||||||||||
| 58 | 37 | exit; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 37 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 37 | $server->run($app); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 38 | 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||