Proxy/Engine/ScoreBoard.pm

filter: Statementstmt 20.0% 24 / 120 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 15.9% 33 / 207 CC 32 SCAR 53.2
CC
32
Cov
13%
run
380.0
_run_child
110.0
stop
20.0
CRAP 205.3
↑ Summary Proxy.pm »
1 package HTTP::Proxy::Engine::ScoreBoard;
2 1 $HTTP::Proxy::Engine::ScoreBoard::VERSION = '0.304';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POSIX ":sys_wait_h"; # WNOHANG
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Fcntl qw(LOCK_UN LOCK_EX);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @ISA = qw( HTTP::Proxy::Engine );
11 1 our %defaults = (
12 start_servers => 4, # start this many, and don't go below
13 max_clients => 12, # don't go above
14 max_requests_per_child => 250, # just in case there's a leak
15 min_spare_servers => 1, # minimum idle (if 0, never start new)
16 max_spare_servers => 12, # maximum idle (should be "single browser max")
17 verify_delay => 60, # minimum time between kids verification
18 );
19  
20 1 __PACKAGE__->make_accessors(
21 qw(
22 kids select status_read status_write scoreboard tempfile
23 verify_live_kids_time last_active_time last_fork_time
24 ),
25 keys %defaults
26 );
27  
28 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
29 0 my $self = shift;
30 0 $self->kids( {} );
31  
32 # set up the communication pipe
33 0 $self->status_read( IO::Handle->new() );
34 0 $self->status_write( IO::Handle->new() );
35 0 ▶ pipe( $self->status_read(), $self->status_write() )
Branch: 1 branch
BranchTrueFalseTotal
pipe $self->status_read, $self->status_write or die "Can't create pipe: $!" 0 0 0
36 or die "Can't create pipe: $!";
37 0 $self->status_write()->autoflush(1);
38 0 $self->select( IO::Select->new( $self->status_read() ) );
39 0 setpgrp; # set as group leader
40  
41 # scoreboard information
42 0 $self->verify_live_kids_time( time );
43 0 $self->last_active_time( time );
44 0 $self->last_fork_time( time );
45 0 $self->scoreboard( '' );
46  
47 # lockfile information
48 0 $self->tempfile(
49 File::Temp->new( UNLINK => 0, TEMPLATE => 'http-proxy-XXXX' ) );
50 0 $self->proxy()->log( HTTP::Proxy::ENGINE, "ENGINE",
51 "Using " . $self->tempfile()->filename() . " as lockfile" );
52 }
53  
54 1 my %status = ( A => 'Acccept', B => 'Busy', I => 'Idle' );
55 0 ▶sub run {
Subroutine run: not called CC=19 SCAR=59.4
Pod documented
56 0 my $self = shift;
57 0 my $proxy = $self->proxy();
58 0 my $kids = $self->kids();
59  
60 ## first phase: update scoreboard
61 0 ▶ if ( $self->select()->can_read(1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->select->can_read(1)) 0 0 0
62 0 ▶ $self->status_read()->sysread( my $buf, 50 ) > 0 # read first 10 changes
Branch: 1 branch
BranchTrueFalseTotal
$self->status_read->sysread(my $buf, 50) > 0 or die "bad read" 0 0 0
63 or die "bad read"; # FIXME
64 0 ▶ while ( length $buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 0 0 0
65 0 my ( $pid, $status ) = unpack "NA", substr( $buf, 0, 5, "" );
66 0 $proxy->log( HTTP::Proxy::ENGINE, 'ENGINE',
67 "Child process $pid updated to $status ($status{$status})" );
68 0 $kids->{$pid} = $status;
69 }
70 0 $self->last_active_time(time);
71 }
72  
73 {
74 0 my $new = join "", values %$kids;
75 0 ▶ if ( $new ne $self->scoreboard() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new ne $self->scoreboard) 0 0 0
76 0 $proxy->log( HTTP::Proxy::ENGINE, 'ENGINE', "ScoreBoard = $new" );
77 0 $self->scoreboard($new);
78 }
79 }
80  
81 ## second phase: delete dead kids
82 0 ▶ while ( ( my $kid = waitpid( -1, WNOHANG ) ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $kid = waitpid(-1, 1)) > 0) 0 0 0
83 0 $proxy->{conn}++; # Cannot use the interface for RO attributes
84 0 $proxy->log( HTTP::Proxy::PROCESS, 'PROCESS',
85 "Reaped child process $kid" );
86 0 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS",
87 0 keys(%$kids) . " remaining kids: @{[ keys %$kids ]}" );
88 0 delete $kids->{$kid};
89 }
90  
91 ## third phase: verify live kids
92 0 ▶ if ( time > $self->verify_live_kids_time() + $self->verify_delay() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time > $self->verify_live_kids_time + $self->verify_delay) 0 0 0
93 0 for my $kid ( keys %$kids ) {
94 0 ▶ next if kill 0, $kid;
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $kid 0 0 0
95  
96 # shouldn't happen normally
97 0 $proxy->log( HTTP::Proxy::ERROR, "ENGINE",
98 "Child process $kid found missing" );
99 0 delete $kids->{$kid};
100 }
101 0 $self->verify_live_kids_time(time);
102 }
103  
104 ## fourth phase: launch kids
105 0 my @idlers = grep $kids->{$_} eq "I", keys %$kids;
106 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (@idlers < $self->min_spare_servers || keys %$kids < $self->start_servers and keys %$kids < $self->max_clients and time > $self->last_fork_time) { } 0 0 0
elsif (@idlers > $self->max_spare_servers || @idlers > $self->min_spare_servers && time > $self->last_active_time + $self->verify_delay and keys %$kids > $self->start_servers) { } 0 0 0
Condition0%0/9
@idlers < $self->min_spare_servers || keys %$kids < $self->start_servers and keys %$kids < $self->max_clients and time > $self->last_fork_time
!ll&&!rl&&r
000
@idlers < $self->min_spare_servers || keys %$kids < $self->start_servers and keys %$kids < $self->max_clients
!ll&&!rl&&r
000
@idlers < $self->min_spare_servers or keys %$kids < $self->start_servers
l!l&&r!l&&!r
000
MC/DC0%0/4
@idlers < $self->min_spare_servers || keys %$kids < $self->start_servers and keys %$kids < $self->max_clients and time > $self->last_fork_time
@idlers < $self->min_spare_servers keys %$kids < $self->start_servers keys %$kids < $self->max_clients time > $self->last_fork_time
Truth table0%0/7
A or B and C and D
A= @idlers < $self->min_spare_serversB= keys %$kids < $self->start_serversC= keys %$kids < $self->max_clientsD= time > $self->last_fork_time
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
107 (
108 @idlers < $self->min_spare_servers() # not enough idlers
109 or keys %$kids < $self->start_servers() # not enough overall
110 )
111 and keys %$kids < $self->max_clients() # not too many please
112 and time > $self->last_fork_time() # not too fast please
113 )
114 {
115 0 my $child = fork();
116 0 ▶ if ( !defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $child) { } 0 0 0
117 0 $proxy->log( HTTP::Proxy::ERROR, "PROCESS", "Cannot fork" );
118 }
119 else {
120 0 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) { } 0 0 0
121 0 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS",
122 "Forked child process $child" );
123 0 $kids->{$child} = "I";
124 0 $self->last_fork_time(time);
125 }
126 else { # child process
127 0 $self->_run_child();
128 0 exit; # we're done
129 }
130 }
131 }
132 ▶ elsif (
Condition0%0/9
@idlers > $self->max_spare_servers || @idlers > $self->min_spare_servers && time > $self->last_active_time + $self->verify_delay and keys %$kids > $self->start_servers
!ll&&!rl&&r
000
@idlers > $self->max_spare_servers or @idlers > $self->min_spare_servers and time > $self->last_active_time + $self->verify_delay
l!l&&r!l&&!r
000
@idlers > $self->min_spare_servers and time > $self->last_active_time + $self->verify_delay
!ll&&!rl&&r
000
MC/DC0%0/4
@idlers > $self->max_spare_servers || @idlers > $self->min_spare_servers && time > $self->last_active_time + $self->verify_delay and keys %$kids > $self->start_servers
@idlers > $self->max_spare_servers @idlers > $self->min_spare_servers time > $self->last_active_time + $self->verify_delay keys %$kids > $self->start_servers
Truth table0%0/6
A or B and C and D
A= @idlers > $self->max_spare_serversB= @idlers > $self->min_spare_serversC= time > $self->last_active_time + $self->verify_delayD= keys %$kids > $self->start_servers
ABCDresult
00XX0
010X0
1XX00
01100
1XX11
01111
133 (
134 @idlers > $self->max_spare_servers() # too many idlers
135 or @idlers > $self->min_spare_servers() # too many lazy idlers
136 and time > $self->last_active_time + $self->verify_delay()
137 )
138 and keys %$kids > $self->start_servers() # not too few please
139 )
140 {
141 0 my $victim = $idlers[ rand @idlers ];
142 0 $proxy->log( HTTP::Proxy::ENGINE, "ENGINE",
143 "Killing idle child process $victim" );
144 0 kill INT => $victim; # pick one at random
145 0 $self->last_active_time(time);
146 }
147  
148 }
149  
150 0 ▶sub stop {
Subroutine stop: not called CC=4 SCAR=30.0
Pod documented
151 0 my $self = shift;
152 0 my $kids = $self->kids();
153 0 my $proxy = $self->proxy();
154  
155 0 kill 'INT' => keys %$kids;
156  
157 # wait for remaining children
158 0 ▶ while (%$kids) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$kids) 0 0 0
159 0 my $pid = waitpid( -1, WNOHANG );
160 0 ▶ next unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
161  
162 0 $proxy->{conn}++; # WRONG for this engine!
163  
164 0 delete $kids->{$pid};
165 0 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS",
166 "Reaped child process $pid" );
167 0 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS",
168 0 keys(%$kids) . " remaining kids: @{[ keys %$kids ]}" );
169 }
170  
171 # remove the temporary file
172 0 ▶ unlink $self->tempfile()->filename() or do {
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->tempfile->filename or do { $proxy->log(-1, "ERROR", "Can't unlink @{[$self->tempfile->filename];}: $!") } 0 0 0
173 0 $proxy->log( HTTP::Proxy::ERROR, "ERROR",
174 0 "Can't unlink @{[ $self->tempfile()->filename() ]}: $!" );
175 };
176 }
177  
178 0 ▶sub _run_child {
Subroutine _run_child: not called CC=10 SCAR=47.0
179 0 my $self = shift;
180 0 my $proxy = $self->proxy();
181  
182 0 my $daemon = $proxy->daemon();
183 0 my $status_write = $self->status_write();
184  
185 0 ▶ open my $lockfh, $self->tempfile()->filename() or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $lockfh, $self->tempfile->filename or do { $proxy->log(-1, "ERROR", "Cannot open lock file: $!"); exit } 0 0 0
186 0 $proxy->log( HTTP::Proxy::ERROR, "ERROR", "Cannot open lock file: $!" );
187 0 exit;
188 };
189  
190 0 my $did = 0; # processed count
191  
192 0 ▶ while ( ++$did <= $self->max_requests_per_child() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$did <= $self->max_requests_per_child) 0 0 0
193  
194 0 ▶ flock $lockfh, LOCK_EX or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $lockfh, 2 or do { $proxy->log(-1, "ERROR", "Cannot get flock: $!"); exit } 0 0 0
195 0 $proxy->log( HTTP::Proxy::ERROR, "ERROR", "Cannot get flock: $!" );
196 0 exit;
197 };
198  
199 0 ▶ last unless $proxy->loop();
Branch: 1 branch
BranchTrueFalseTotal
unless $proxy->loop 0 0 0
200  
201 0 ▶ 5 == syswrite $status_write, pack "NA", $$, "A" # go accept
Branch: 1 branch
BranchTrueFalseTotal
5 == syswrite($status_write, pack("NA", $$, "A")) or $proxy->log(-1, "ERROR", "status A: short write") 0 0 0
202 or $proxy->log( HTTP::Proxy::ERROR, "ERROR", "status A: short write");
203  
204 0 ▶ my $slave = $daemon->accept() or do {
Branch: 1 branch
BranchTrueFalseTotal
my $slave = $daemon->accept or do { $proxy->log(-1, "ERROR", "Cannot accept: $!"); exit } 0 0 0
205 0 $proxy->log( HTTP::Proxy::ERROR, "ERROR", "Cannot accept: $!");
206 0 exit;
207 };
208  
209 0 ▶ flock $lockfh, LOCK_UN or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $lockfh, 8 or do { $proxy->log(-1, "ERROR", "Cannot unflock: $!"); exit } 0 0 0
210 0 $proxy->log( HTTP::Proxy::ERROR, "ERROR", "Cannot unflock: $!" );
211 0 exit;
212 };
213  
214 0 ▶ 5 == syswrite $status_write, pack "NA", $$, "B" # go busy
Branch: 1 branch
BranchTrueFalseTotal
5 == syswrite($status_write, pack("NA", $$, "B")) or $proxy->log(-1, "ERROR", "status B: short write") 0 0 0
215 or $proxy->log( HTTP::Proxy::ERROR, "ERROR", "status B: short write");
216 0 $slave->autoflush(1);
217
218 0 $proxy->serve_connections($slave); # the real work is done here
219  
220 0 close $slave;
221 0 ▶ 5 == syswrite $status_write, pack "NA", $$, "I" # go idle
Branch: 1 branch
BranchTrueFalseTotal
5 == syswrite($status_write, pack("NA", $$, "I")) or $proxy->log(-1, "ERROR", "status I: short write") 0 0 0
222 or $proxy->log( HTTP::Proxy::ERROR, "ERROR", "status I: short write");
223 }
224 }
225  
226 1 1;
227  
228 __END__
↑ Summary Proxy.pm »