line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::SizeLimit; |
2
|
1
|
|
|
1
|
|
757
|
use 5.008; |
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
5
|
1
|
|
|
|
|
8
|
use parent qw( |
6
|
|
|
|
|
|
|
Plack::Middleware |
7
|
|
|
|
|
|
|
Process::SizeLimit::Core |
8
|
1
|
|
|
1
|
|
4
|
); |
|
1
|
|
|
|
|
2
|
|
9
|
1
|
|
|
|
|
10
|
use Plack::Util::Accessor qw( |
10
|
|
|
|
|
|
|
max_unshared_size_in_kb |
11
|
|
|
|
|
|
|
min_shared_size_in_kb |
12
|
|
|
|
|
|
|
max_process_size_in_kb |
13
|
|
|
|
|
|
|
check_every_n_requests |
14
|
|
|
|
|
|
|
log_when_limits_exceeded |
15
|
|
|
|
|
|
|
callback |
16
|
1
|
|
|
1
|
|
27539
|
); |
|
1
|
|
|
|
|
4
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub prepare_app { |
21
|
1
|
|
|
1
|
1
|
105
|
my $self = shift; |
22
|
1
|
|
50
|
|
|
3
|
$self->set_check_interval($self->check_every_n_requests || 1); |
23
|
1
|
|
|
|
|
73
|
$self->set_max_process_size($self->max_process_size_in_kb); |
24
|
1
|
|
|
|
|
16
|
$self->set_min_shared_size($self->min_shared_size_in_kb); |
25
|
1
|
|
|
|
|
11
|
$self->set_max_unshared_size($self->max_unshared_size_in_kb); |
26
|
1
|
|
|
|
|
10
|
$self->set_callback($self->callback); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub call { |
30
|
1
|
|
|
1
|
1
|
22079
|
my ($self, $env) = @_; |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
11
|
my $res = $self->app->($env); |
33
|
|
|
|
|
|
|
|
34
|
1
|
0
|
33
|
|
|
48
|
return $res unless $env->{'psgix.harakiri'} or $env->{'psgix.harakiri.supported'}; |
35
|
|
|
|
|
|
|
|
36
|
1
|
50
|
|
|
|
5
|
if (my $interval = $self->check_every_n_requests) { |
37
|
0
|
|
|
|
|
0
|
my $pinc = $self->get_and_pinc_request_count; |
38
|
0
|
0
|
|
|
|
0
|
return $res if ($pinc % $interval); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
7
|
my $harakiri; |
42
|
1
|
50
|
|
|
|
5
|
if ($env->{'psgix.harakiri'}) { |
|
|
0
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Canonical implementation (Starman 0.2012+) |
44
|
1
|
|
|
|
|
13
|
$harakiri = $env->{'psgix.harakiri.commit'} = $self->_limits_are_exceeded; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
elsif ($env->{'psgix.harakiri.supported'}) { |
47
|
|
|
|
|
|
|
# Legacy implementation (uWSGI) |
48
|
0
|
|
|
|
|
0
|
$harakiri = $env->{'psgix.harakiri'} = $self->_limits_are_exceeded; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
1
|
50
|
33
|
|
|
19122
|
if ($harakiri && $self->log_when_limits_exceeded) { |
52
|
|
|
|
|
|
|
my $message = sprintf( |
53
|
|
|
|
|
|
|
'pid %d committed harakiri (size: %d, shared: %d, unshared: %d) at %s', |
54
|
|
|
|
|
|
|
$$, $self->_check_size, $env->{REQUEST_URI}, |
55
|
1
|
|
|
|
|
14
|
); |
56
|
1
|
50
|
|
|
|
16984
|
if (my $logger = $env->{'psgix.logger'}) { |
57
|
1
|
|
|
|
|
8
|
$logger->( { message => $message, level => 'warn' } ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
0
|
warn "$message\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
16
|
return $res; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
__END__ |