blib/lib/Apache/LoadAvgLimit.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 82 CC 11 SCAR 45.1
CC
11
Cov
0%
handler
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 91.4
1 package Apache::LoadAvgLimit;
2  
3 use strict;
4 use vars qw($VERSION);
5 use Apache;
6 use Apache::Constants qw(:common HTTP_SERVICE_UNAVAILABLE);
7 use Apache::LoadAvgLimit::GetAvg;
8  
9 $VERSION = '0.04';
10  
11 sub handler {
12 my $r = shift;
13 return DECLINED unless $r->is_initial_req;
14  
15 # get
16 my @avg = Apache::LoadAvgLimit::GetAvg::get_loadavg()
17 or do {
18 $r->log_error("Cannot get load avg !");
19 return SERVER_ERROR;
20 };
21  
22 my $over = 0;
23 if( (my $limit = $r->dir_config('LoadAvgLimit')) =~ /^[\d\.]{1,}$/ ){
24  
25 # at least one avg needs to be over the specified limit.
26 for my $avg(@avg){
27 next if $avg <= $limit;
28 $over++;
29 last;
30 }
31  
32 }else{
33  
34 my @limit;
35 if( $r->dir_config('LoadAvgLimit_1') =~ /^[\d\.]{1,}$/ ){
36 $limit[0] = $r->dir_config('LoadAvgLimit_1')
37 }
38 if( $r->dir_config('LoadAvgLimit_5') =~ /^[\d\.]{1,}$/ ){
39 $limit[1] = $r->dir_config('LoadAvgLimit_5')
40 }
41 if( $r->dir_config('LoadAvgLimit_15') =~ /^[\d\.]{1,}$/ ){
42 $limit[2] = $r->dir_config('LoadAvgLimit_15')
43 }
44  
45 # check
46 for my $i(0..2){
47 next if not defined $limit[$i];
48 next if $avg[$i] <= $limit[$i];
49 $over++;
50 last;
51 }
52  
53 }
54  
55 if( $over ){
56 # set Retry-After field
57 if( (my $retry_after = $r->dir_config('LoadAvgRetryAfter') ) =~ /^\d+$/ ){
58 $r->err_header_out('Retry-After' => int $retry_after);
59 }
60  
61 $r->log_reason("System load average reaches limit.", $r->filename);
62 return HTTP_SERVICE_UNAVAILABLE;
63 }
64  
65 return OK;
66 }
67  
68 1;
69 __END__