File: Shotgun.pm

package info (click to toggle)
libplack-perl 1.0047-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: perl: 5,287; python: 6; makefile: 4
file content (108 lines) | stat: -rw-r--r-- 2,871 bytes parent folder | download | duplicates (7)
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
99
100
101
102
103
104
105
106
107
108
package Plack::Loader::Shotgun;
use strict;
use parent qw(Plack::Loader);
use Storable;
use Try::Tiny;
use Plack::Middleware::BufferedStreaming;

die <<DIE if $^O eq 'MSWin32' && !$ENV{PLACK_SHOTGUN_MEMORY_LEAK};

Shotgun loader uses fork(2) system call to create a fresh Perl interpreter, that is known to not work
properly in a fork-emulation layer on Windows and cause huge memory leaks.

If you're aware of this and still want to run the loader, run it with the environment variable
PLACK_SHOTGUN_MEMORY_LEAK on.

DIE

sub preload_app {
    my($self, $builder) = @_;
    $self->{builder} = sub { Plack::Middleware::BufferedStreaming->wrap($builder->()) };
}

sub run {
    my($self, $server) = @_;

    my $app = sub {
        my $env = shift;

        pipe my $read, my $write;

        my $pid = fork;
        if ($pid) {
            # parent
            close $write;
            my $res = Storable::thaw(join '', <$read>);
            close $read;
            waitpid($pid, 0);

            return $res;
        } else {
            # child
            close $read;

            my $res;
            try {
                $env->{'psgi.streaming'} = 0;
                $res = $self->{builder}->()->($env);
                my @body;
                Plack::Util::foreach($res->[2], sub { push @body, $_[0] });
                $res->[2] = \@body;
            } catch {
                $env->{'psgi.errors'}->print($_);
                $res = [ 500, [ "Content-Type", "text/plain" ], [ "Internal Server Error" ] ];
            };

            print {$write} Storable::freeze($res);
            close $write;
            exit;
        }
    };

    $server->run($app);
}

1;

__END__

=head1 NAME

Plack::Loader::Shotgun - forking implementation of plackup

=head1 SYNOPSIS

  plackup -L Shotgun

=head1 DESCRIPTION

Shotgun loader delays the compilation and execution of your
application until the runtime. When a new request comes in, this forks
a new child, compiles your code and runs the application.

This should be an ultimate alternative solution when reloading with
L<Plack::Middleware::Refresh> doesn't work, or plackup's default C<-r>
filesystem watcher causes problems. I can imagine this is useful for
applications which expects their application is only evaluated once
(like in-file templates) or on operating systems with broken fork
implementation, etc.

This is much like good old CGI's fork and run but you don't need a web
server, and there's a benefit of preloading modules that are not
likely to change. For instance if you develop a web application using
Moose and DBIx::Class,

  plackup -MMoose -MDBIx::Class -L Shotgun yourapp.psgi

would preload those modules and only re-evaluates your code in every
request.

=head1 AUTHOR

Tatsuhiko Miyagawa with an inspiration from L<http://github.com/rtomayko/shotgun>

=head1 SEE ALSO

L<plackup>

=cut