blib/lib/POE/Component/Server/SimpleHTTP/PreFork.pm

filter: Statementstmt 19.7% 74 / 375 Branchbran 2.0% 3 / 148 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 26.6% 12 / 45 Podpod 0.0% 0 / 1 totaltotal 13.9% 89 / 639 CC 98 SCAR 43.3
CC
98
Cov
14%
__ANON__
182.0
start_listener
156.0
__ANON__
156.0
CRAP 76.1
↑ Summary
1 package POE::Component::Server::SimpleHTTP::PreFork;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '2.10';
7  
8 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Date qw( time2str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IPC::Shareable qw( :lock );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POE::Component::Server::SimpleHTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Set some constants
16 1 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=14.5
17  
18 # Interval at which to check spares
19 1 ▶ if ( !defined &CHECKSPARES_INTERVAL ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Component::Server::SimpleHTTP::PreFork::CHECKSPARES_INTERVAL) 1 0 1
20 1 eval "sub CHECKSPARES_INTERVAL () { 1 }";
21 }
22  
23 # Interval at which to retry preforking
24 1 ▶ if ( !defined &PREFORK_INTERVAL ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Component::Server::SimpleHTTP::PreFork::PREFORK_INTERVAL) 1 0 1
25 1 eval "sub PREFORK_INTERVAL () { 5 }";
26 }
27  
28 # If true, show the scoreboard every second
29 1 ▶ if ( !defined &DEBUGSB ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &POE::Component::Server::SimpleHTTP::PreFork::DEBUGSB) 1 0 1
30 1 eval "sub DEBUGSB () { 0 }";
31 }
32 }
33  
34 1 ▶use MooseX::POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 extends 'POE::Component::Server::SimpleHTTP';
38  
39 has 'forkhandlers' => (
40 is => 'ro',
41 isa => 'HashRef',
42 0 ▶ default => sub {{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 1 writer => 'set_forkhandlers',
44 );
45  
46 has 'minspareservers' => (
47 is => 'ro',
48 0 ▶ default => sub { 5 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 ▶ isa => subtype 'Int' => where { $_ > 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 1 );
51  
52 # maxspareservers must be greater than minspareservers
53 has 'maxspareservers' => (
54 is => 'ro',
55 isa => 'Int',
56 0 ▶ default => sub { 10 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 1 );
58  
59 has 'maxclients' => (
60 is => 'ro',
61 isa => 'Int',
62 0 ▶ default => sub { 256 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 1 );
64  
65 has 'maxrequestperchild' => (
66 is => 'ro',
67 0 ▶ isa => subtype 'Int' => where { $_ > 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 1 );
69  
70 # startservers must be greater than minspareservers
71 has 'startservers' => (
72 is => 'ro',
73 0 ▶ default => sub { 10 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ isa => subtype 'Int' => where { $_ > 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 1 );
76  
77 1 has 'scoreboard' => (
78 is => 'ro',
79 isa => 'HashRef',
80 clearer => 'wipe_scoreboard',
81 writer => 'set_scoreboard',
82 init_arg => undef,
83 );
84  
85 has 'is_child' => (
86 is => 'ro',
87 isa => 'Bool',
88 writer => 'am_child',
89 clearer => 'not_child',
90 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 1 init_arg => undef,
92 );
93  
94 has 'reqcount' => (
95 traits => ['Counter'],
96 is => 'ro',
97 isa => 'Num',
98 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 1 handles => {
100 'inc_reqcount', 'inc',
101 'dec_reqcount', 'dec',
102 'reset_reqcount', 'reset',
103 },
104 );
105  
106 has 'ipc_glue' => (
107 is => 'ro',
108 default => 'scbd',
109 0 ▶ isa => subtype 'Str' => where { $_ =~ /^\d+$/s || length $_ == 4 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^\d+$/s or length $_ == 4 0 0 0
110 1 );
111  
112 0 ▶sub START {
Subroutine START: not called CC=1 SCAR=6.9
Pod undocumented
113 0 $poe_kernel->sig( TERM => '_sig_term' );
114 #$poe_kernel->sig( CHLD => '_sig_chld' );
115 0 return;
116 }
117  
118 # 'SHUTDOWN'
119 # Stops the server!
120 0 ▶event 'SHUTDOWN' => sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
121 0 my ($kernel,$self,$session,$graceful) = @_[KERNEL,OBJECT,SESSION,ARG0];
122 # Shutdown the SocketFactory wheel
123 0 ▶ $self->_clear_factory if $self->_factory;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_factory 0 0 0
124  
125 # Debug stuff
126 0 warn 'Stopped listening for new connections!'
127 if POE::Component::Server::SimpleHTTP::DEBUG;
128  
129 0 my $children;
130  
131 0 ▶ if ( $graceful ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($graceful) 0 0 0
132 # Attempt to gracefully kill the children.
133 0 $children = $kernel->call( $session, 'kill_children', 'TERM' );
134  
135 # Check for number of requests, and children.
136 0 ▶ if ( keys( %{ $self->_requests } ) == 0 and $children == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->_requests;} == 0 and $children == 0) 0 0 0
Condition0%0/3
keys %{$self->_requests;} == 0 and $children == 0
!ll&&!rl&&r
000
MC/DC0%0/2
keys %{$self->_requests;} == 0 and $children == 0
keys %{$self->_requests;} == 0 $children == 0
Truth table0%0/3
A and B
A= keys %{$self->_requests;} == 0B= $children == 0
ABresult
0X0
100
111
137  
138 # Alright, shutdown anyway
139  
140 # Delete our alias
141 0 $kernel->alias_remove( $_ ) for $kernel->alias_list();
142 0 ▶ $kernel->refcount_decrement( $self->get_session_id, __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->alias 0 0 0
143 unless $self->alias;
144  
145 # Destroy all memory segments created by this process.
146 0 IPC::Shareable->clean_up;
147 0 $self->wipe_scoreboard;
148  
149 # Debug stuff
150 0 warn 'Stopped SimpleHTTP gracefully, no requests left'
151 if POE::Component::Server::SimpleHTTP::DEBUG;
152  
153 }
154  
155 # All done!
156 0 return 1;
157 }
158  
159 # CheckSpares need to know that we're shutting down
160 0 $self->scoreboard->{'shutdown'} = 1;
161  
162 # Forcefully kill all the children.
163 0 $kernel->call( $session, 'kill_children', 'KILL' );
164  
165 # Forcibly close all sockets that are open
166 0 foreach my $S ( $self->_requests, $self->_connections ) {
167 0 foreach my $conn ( keys %$S ) {
168  
169 # Can't call method "shutdown_input" on an undefined value at
170 # /usr/lib/perl5/site_perl/5.8.2/POE/Component/Server/SimpleHTTP.pm line 323.
171 0 ▶ if ( defined $S->{$conn}->wheel
Branch: 1 branch
BranchTrueFalseTotal
if (defined $S->{$conn}->wheel and defined $S->{$conn}->wheel->get_input_handle) 0 0 0
Condition0%0/3
defined $S->{$conn}->wheel and defined $S->{$conn}->wheel->get_input_handle
!ll&&!rl&&r
000
MC/DC0%0/2
defined $S->{$conn}->wheel and defined $S->{$conn}->wheel->get_input_handle
defined $S->{$conn}->wheel defined $S->{$conn}->wheel->get_input_handle
Truth table0%0/3
A and B
A= defined $S->{$conn}->wheelB= defined $S->{$conn}->wheel->get_input_handle
ABresult
0X0
100
111
172 and defined $S->{$conn}->wheel->get_input_handle() )
173 {
174 0 $S->{$conn}->close_wheel;
175 }
176  
177 # Delete this request
178 0 delete $S->{$conn};
179 }
180 }
181  
182 # Remove any shared memory segments.
183 0 IPC::Shareable->clean_up;
184 0 $self->wipe_scoreboard;
185  
186 # Delete our alias
187 0 $kernel->alias_remove( $_ ) for $kernel->alias_list();
188 0 ▶ $kernel->refcount_decrement( $self->get_session_id, __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->alias 0 0 0
189 unless $self->alias;
190  
191  
192 0 warn 'Successfully stopped SimpleHTTP'
193 if POE::Component::Server::SimpleHTTP::DEBUG;
194  
195 # Return success
196 0 return 1;
197 1 };
198  
199 # Sets up the SocketFactory wheel :)
200 0 ▶event 'start_listener' => sub {
Subroutine start_listener: not called CC=12 SCAR=50.5
201 0 my ($kernel,$self,$noinc) = @_[KERNEL,OBJECT,ARG0];
202  
203 0 warn 'Creating SocketFactory wheel now'
204 if POE::Component::Server::SimpleHTTP::DEBUG;
205  
206 # Only try to re-establish the listener if we are the parent
207 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
208 0 warn 'Inside the child. Aborting attempt to reestablish the listener.';
209 0 return 0;
210 }
211  
212 # Check if we should set up the wheel
213 0 ▶ if ( $self->retries == POE::Component::Server::SimpleHTTP::MAX_RETRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->retries == 5) { } 0 0 0
214 0 die 'POE::Component::Server::SimpleHTTP tried '
215 . POE::Component::Server::SimpleHTTP::MAX_RETRIES
216 . ' times to create a Wheel and is giving up...';
217 }
218 else {
219  
220 0 ▶ $self->inc_retry unless $noinc;
Branch: 1 branch
BranchTrueFalseTotal
unless $noinc 0 0 0
221  
222 # Create our own SocketFactory Wheel :)
223 0 ▶ my $factory = POE::Wheel::SocketFactory->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->address ? : 0 0 0
224 BindPort => $self->port,
225 ( $self->address ? ( BindAddress => $self->address ) : () ),
226 Reuse => 'yes',
227 SuccessEvent => 'got_connection',
228 FailureEvent => 'listener_error',
229 );
230  
231 0 my ( $port, $address ) =
232 sockaddr_in( $factory->getsockname );
233 0 ▶ $self->_set_port( $port ) if $self->port == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->port == 0 0 0 0
234  
235 0 $self->_set_factory( $factory );
236  
237 0 ▶ if ( $self->setuphandler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->setuphandler) 0 0 0
238 0 my $setuphandler = $self->setuphandler;
239 0 ▶ if ( $setuphandler->{POSTBACK} and
Branch: 1 branch
BranchTrueFalseTotal
if ($setuphandler->{'POSTBACK'} and ref $setuphandler->{'POSTBACK'} eq 'POE::Session::AnonEvent') { } 0 0 0
Condition0%0/3
$setuphandler->{"POSTBACK"} and ref $setuphandler->{"POSTBACK"} eq "POE::Session::AnonEvent"
!ll&&!rl&&r
000
MC/DC0%0/2
$setuphandler->{"POSTBACK"} and ref $setuphandler->{"POSTBACK"} eq "POE::Session::AnonEvent"
$setuphandler->{"POSTBACK"} ref $setuphandler->{"POSTBACK"} eq "POE::Session::AnonEvent"
Truth table0%0/3
A and B
A= $setuphandler->{"POSTBACK"}B= ref $setuphandler->{"POSTBACK"} eq "POE::Session::AnonEvent"
ABresult
0X0
100
111
240 ref $setuphandler->{POSTBACK} eq 'POE::Session::AnonEvent' ) {
241 0 $setuphandler->{POSTBACK}->( $port, $address );
242 }
243 else {
244 $kernel->post(
245 $setuphandler->{'SESSION'},
246 $setuphandler->{'EVENT'},
247 $port, $address,
248 0 ▶ ) if $setuphandler->{'SESSION'} and $setuphandler->{'EVENT'};
Branch: 1 branch
BranchTrueFalseTotal
if $setuphandler->{"SESSION"} and $setuphandler->{"EVENT"} 0 0 0
Condition0%0/3
$setuphandler->{"SESSION"} and $setuphandler->{"EVENT"}
!ll&&!rl&&r
000
MC/DC0%0/2
$setuphandler->{"SESSION"} and $setuphandler->{"EVENT"}
$setuphandler->{"SESSION"} $setuphandler->{"EVENT"}
Truth table0%0/3
A and B
A= $setuphandler->{"SESSION"}B= $setuphandler->{"EVENT"}
ABresult
0X0
100
111
249 }
250 }
251  
252 # Pre-fork if that is what was requested
253 0 ▶ if ( $self->startservers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->startservers) 0 0 0
254  
255 # We don't want to accept socket connections in the parent process
256 0 $self->_factory->pause_accept();
257  
258 # Wait a bit and then do the actual forking
259 0 $kernel->yield( 'prefork', $self->_factory );
260 }
261 }
262  
263 0 return 1;
264 1 };
265  
266 # Stops listening on the socket
267 0 ▶event 'STOPLISTEN' => sub {
Subroutine STOPLISTEN: not called CC=3 SCAR=24.8
268 0 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
269  
270 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) { } 0 0 0
271  
272 # If we are the child then we shouldn't really stop listening.
273 # Instead, pause accepting on our SocketFactory.
274 0 ▶ unless ( $self->_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_factory) { } 0 0 0
275 0 warn "Cannot StopListen on a non-existant SOCKETFACTORY in child $$";
276 0 return 0;
277 }
278 else {
279  
280 # Pause accepting.
281 0 $self->_factory->pause_accept();
282 0 return 1;
283 }
284 }
285 else {
286  
287 # We are in the parent, so truly stop listening.
288 # Kill the children because they are still listenning.
289 0 $kernel->call( $session, 'kill_children', 'TERM' );
290  
291 # Call the super class method.
292 0 shift;
293 0 return $self->SUPER::STOPLISTEN(@_);
294 }
295 1 };
296  
297 0 ▶event 'STARTLISTEN' => sub {
Subroutine STARTLISTEN: not called CC=3 SCAR=24.8
298 0 my ($kernel,$self) = @_[KERNEL,OBJECT];
299  
300 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) { } 0 0 0
301  
302 # If we are the child then we can't really create a new SOCKETFACTORY.
303 # Instead, we can resume accepting on our current SOCKETFACTORY.
304 0 ▶ unless ( $self->_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_factory) { } 0 0 0
305 0 warn "Cannot StartListen on a non-existant SOCKETFACTORY in child $$";
306 0 return 0;
307 }
308 else {
309  
310 # Resume accepting.
311 0 $self->_factory->resume_accept();
312 0 return 1;
313 }
314 }
315 else {
316  
317 # We are the parent. Truly start listening again.
318 0 shift;
319 0 return $self->SUPER::STARTLISTEN(@_);
320 }
321 1 };
322  
323 # Sets the HANDLERS
324 0 ▶event 'SETHANDLERS' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
325 0 my $self = $_[OBJECT];
326  
327 # Setting handlers in a child makes little sense, so abort if this is the case
328 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
329 0 warn "Child $$ tried to set the handlers for SimpleHTTP.";
330 0 return 0;
331 }
332  
333 # Call the super class method.
334 0 shift;
335 0 return $self->SUPER::SETHANDLERS(@_);
336 1 };
337  
338 # The actual manager of connections
339 0 ▶event 'got_connection' => sub {
Subroutine got_connection: not called CC=1 SCAR=6.9
340 0 my ($kernel,$self) = @_[KERNEL,OBJECT];
341 0 shift;
342 0 $self->SUPER::got_connection(@_);
343 # Update the scoreboard.
344 0 $kernel->call( $_[SESSION], 'UpdateScoreboard' );
345 0 return 1;
346 1 };
347  
348 # Finally got input, set some stuff and send away!
349 0 ▶event 'got_input' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
350 0 my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG1];
351 # Call the super class method.
352 0 shift;
353 0 my $rv = $self->SUPER::got_input(@_);
354 # If the connection died/failed for some reason then the request is deleted.
355 # In this case, we have to update the scoreboard.
356 $kernel->call( $_[SESSION], 'UpdateScoreboard' )
357 0 ▶ unless exists $self->_requests->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_requests->{$id} 0 0 0
358 0 return $rv;
359 1 };
360  
361 # Finished with a request!
362 0 ▶event 'got_flush' => sub {
Subroutine got_flush: not called CC=5 SCAR=34.0
363 0 my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0];
364  
365 # Call the super class method.
366 0 shift;
367 0 my $rv = $self->SUPER::got_flush(@_);
368  
369 # Deal with maxrequestperchild
370 0 ▶ if ( $self->is_child and defined $self->maxrequestperchild ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child and defined $self->maxrequestperchild) 0 0 0
Condition0%0/3
$self->is_child and defined $self->maxrequestperchild
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_child and defined $self->maxrequestperchild
$self->is_child defined $self->maxrequestperchild
Truth table0%0/3
A and B
A= $self->is_childB= defined $self->maxrequestperchild
ABresult
0X0
100
111
371 0 $self->inc_reqcount;
372  
373 0 ▶ if ( $self->reqcount >= $self->maxrequestperchild) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->reqcount >= $self->maxrequestperchild) 0 0 0
374 0 warn "Shutting down $$ because it reached MAXREQUESTPERCHILD."
375 if POE::Component::Server::SimpleHTTP::DEBUG;
376  
377 0 $kernel->yield( 'SHUTDOWN', 'GRACEFUL' );
378 0 return 1;
379 }
380 }
381  
382 # If the connection died/failed for some reason then the request is deleted.
383 # In this case, we have to update the scoreboard.
384 $kernel->call( $_[SESSION], 'update_scoreboard' )
385 0 ▶ unless exists $self->_requests->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_requests->{$id} 0 0 0
386 0 return $rv;
387 1 };
388  
389 # Got some sort of error from ReadWrite
390 0 ▶event 'got_error' => sub {
Subroutine got_error: not called CC=1 SCAR=6.9
391 0 my ($kernel,$self) = @_[KERNEL,OBJECT];
392 # Call the super class method.
393 # but first shift @_ otherwise $self->SUPER::got_error(@_) results in
394 # $self/OBJECT being in the first two values in @_ param array to
395 # SimpleHTTP got_error method instead of just the first value
396 0 shift @_;
397 0 my $rv = $self->SUPER::got_error(@_);
398 # The connection was probably cleared, so update the scoreboard.
399 0 $kernel->call( $_[SESSION], 'update_scoreboard' );
400 0 return $rv;
401 1 };
402  
403 # Closes the connection
404 0 ▶event 'CLOSE' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
405 0 my ($kernel,$self) = @_[KERNEL,OBJECT];
406 # Call the super class method.
407 0 my $rv = $self->SUPER::CLOSE(@_);
408 # The connection was probably cleared, so update the scoreboard.
409 0 $kernel->call( $_[SESSION], 'update_scoreboard' );
410 0 return $rv;
411 1 };
412  
413 # PreFork the initial instances.
414 0 ▶event 'prefork' => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
415 # ARG0 = SocketFactory
416 0 my ($kernel,$self,$session,$sf) = @_[KERNEL,OBJECT,SESSION,ARG0];
417 0 my ($scoreboard,$mem);
418  
419 0 warn 'Trying to prefork.'
420 if POE::Component::Server::SimpleHTTP::DEBUG;
421  
422 # Only the parent is allowed to fork
423 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
424 0 warn "Cannot pre-fork from child $$.";
425 0 return 0;
426 }
427  
428 # Make that the current SF is the same as the one we were called for.
429 # If not, then that means and error occured sometime inbetween.
430 0 ▶ unless ( defined $self->_factory and $sf == $self->_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_factory and $sf == $self->_factory) 0 0 0
Condition0%0/3
defined $self->_factory and $sf == $self->_factory
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->_factory and $sf == $self->_factory
defined $self->_factory $sf == $self->_factory
Truth table0%0/3
A and B
A= defined $self->_factoryB= $sf == $self->_factory
ABresult
0X0
100
111
431 0 warn 'Aborting pre-fork because the SocketFactory is not the same.';
432 0 return 0;
433 }
434  
435 # Initialize the scoreboard the first time around.
436 0 ▶ unless ( defined $self->scoreboard ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->scoreboard) { } 0 0 0
437 0 my %temp;
438  
439 # In order to keep a pool of spare children we need to know how many spares there are.
440 0 $mem = tie %temp, 'IPC::Shareable', $self->ipc_glue,
441 { 'create' => 1, 'mode' => 0600 };
442 0 $scoreboard = \%temp;
443 }
444 else {
445  
446 # We already have a scoreboard from a previous listen attempt.
447 0 $scoreboard = $self->scoreboard;
448 0 $mem = tied %$scoreboard;
449 }
450  
451 0 ▶ unless ( defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mem) { } 0 0 0
452 0 warn
453 'Cannot tie to the shared memory segment. Will try again in 5 seconds.';
454 0 $kernel->delay_set( 'prefork', PREFORK_INTERVAL, $sf );
455 0 return 0;
456 }
457 else {
458  
459 # Clear the variable and store it for later use.
460 0 %{ $scoreboard } = ( 'spares' => 0, 'actives' => 0 );
461 0 $self->set_scoreboard( $scoreboard );
462 }
463  
464 0 for ( 1 .. $self->startservers ) {
465 0 my $pid = fork();
466  
467 0 ▶ if ( not defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif ($pid) { } 0 0 0
468  
469 # Make sure this fork succeeded.
470 0 warn "Server $$ fork failed: $!";
471 0 next;
472 }
473 elsif ($pid) {
474  
475 # We are the parent.
476 0 $kernel->sig_child( $pid, '_sig_chld' );
477 0 next;
478 }
479 else {
480  
481 0 warn "Forked child $$."
482 if POE::Component::Server::SimpleHTTP::DEBUG;
483  
484 # We are the child. Do something "childish".
485 0 $self->am_child( 1 );
486 0 $kernel->call( $session, 'add_scoreboard' );
487  
488 # Notify the other forked sessions that we have forked.
489 0 foreach my $sess ( keys( %{ $self->forkhandlers } ) ) {
490 0 $kernel->call( $sess, $self->forkhandlers->{$sess} );
491 }
492  
493 # Get to work!
494 0 $sf->resume_accept();
495 0 return 1;
496 }
497 }
498  
499 # Pre-forking is done and our children are happily away!
500 # We are the parent, so start monitoring the spare pool.
501 0 $kernel->delay_set( 'check_spares', CHECKSPARES_INTERVAL );
502  
503 # Let the developer see the scoreboard if they want.
504 0 if (DEBUGSB) {
505 $kernel->delay_set( 'show_scoreboard', 1 );
506 }
507 1 };
508  
509 # True if this is a child.
510 0 ▶event 'ISCHILD' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
511 0 return $_[OBJECT]->is_child;
512 1 };
513  
514 # Kill all our children, and return the number we sent signals too.
515 0 ▶event 'kill_children' => sub {
Subroutine kill_children: not called CC=8 SCAR=42.8
516 0 my ($kernel,$self,$sig) = @_[KERNEL,OBJECT,ARG0];
517 0 my ($children,$scoreboard,$mem) = 0;
518  
519 # By default, kill them nicely.
520 0 ▶ $sig = 'TERM' unless defined $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig 0 0 0
521  
522 # Make sure we are the parent AND preforked.
523 0 ▶ unless ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_child) 0 0 0
524 0 warn "Killing children from $$ with signal $sig."
525 if POE::Component::Server::SimpleHTTP::DEBUG;
526  
527 0 $scoreboard = $self->scoreboard;
528 0 $mem = tied %$scoreboard;
529 0 ▶ if ( not defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $mem) { } 0 0 0
530  
531 # There was an error, but there's nothing we can do,
532 # so just exit.
533 0 warn "Parent's SCOREBOARD is not tied!";
534 0 $children = 0;
535 }
536 else {
537  
538 # Get a count of the number of children, and start killing them
539 0 $mem->shlock(LOCK_SH);
540  
541 # The children haven't already received a signal, so send them one.
542 0 foreach my $pid ( keys %$scoreboard ) {
543 0 ▶ if ( ( $pid ne 'actives' ) && ( $pid ne 'spares' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid ne "actives" and $pid ne "spares") 0 0 0
Condition0%0/3
$pid ne "actives" and $pid ne "spares"
!ll&&!rl&&r
000
MC/DC0%0/2
$pid ne "actives" and $pid ne "spares"
$pid ne "actives" $pid ne "spares"
Truth table0%0/3
A and B
A= $pid ne "actives"B= $pid ne "spares"
ABresult
0X0
100
111
544 0 ++$children;
545 0 kill $sig, $pid;
546 }
547 }
548 0 $mem->shlock(LOCK_UN);
549  
550 # Check to make sure it is sane.
551 0 ▶ if ( $children < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children < 0) 0 0 0
552 0 warn "The child count is negative: $children.";
553 0 $children = 0;
554 }
555 }
556 }
557  
558 #
559 0 $kernel->delay_set('check_spares');
560  
561 0 return $children;
562 1 };
563  
564 # Sets the FORKHANDLERS
565 0 ▶event 'SETFORKHANDLERS' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
566 # ARG0 = ref to handlers hash
567 0 my ($self,$handlers) = @_[OBJECT,ARG0];
568  
569 # Setting handlers in a child makes little sense, so abort if this is the case.
570 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
571 0 warn "Child $$ tried to set the handlers for SimpleHTTP.";
572 0 return 0;
573 }
574  
575 # Validate it...
576 0 ▶ unless ( defined $handlers and ref $handlers eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $handlers and ref $handlers eq "HASH") 0 0 0
Condition0%0/3
defined $handlers and ref $handlers eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $handlers and ref $handlers eq "HASH"
defined $handlers ref $handlers eq "HASH"
Truth table0%0/3
A and B
A= defined $handlersB= ref $handlers eq "HASH"
ABresult
0X0
100
111
577 0 warn "FORKHANDLERS is not in the proper format.";
578 0 return 0;
579 }
580  
581 # If we got here, passed tests!
582 0 $self->set_forkhandlers( $handlers );
583  
584 # All done!
585 0 return 1;
586 1 };
587  
588 # Gets the FORKHANDLERS
589 0 ▶event 'GETFORKHANDLERS' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
590 # ARG0 = session, ARG1 = event
591 0 my ($kernel,$self,$session,$event) = @_[KERNEL,OBJECT,ARG0,ARG1];
592  
593 # Validation
594 0 ▶ return undef unless defined $session and defined $event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session and defined $event 0 0 0
Condition0%0/3
defined $session and defined $event
!ll&&!rl&&r
000
MC/DC0%0/2
defined $session and defined $event
defined $session defined $event
Truth table0%0/3
A and B
A= defined $sessionB= defined $event
ABresult
0X0
100
111
595  
596 # Make a deep copy of the handlers
597 0 require Storable;
598  
599 0 my $handlers = Storable::dclone( $self->forkhandlers );
600  
601 # All done!
602 0 $kernel->post( $session, $event, $handlers );
603  
604 # All done!
605 0 return 1;
606 1 };
607  
608 # Check to see if we need a new spare.
609 0 ▶event 'check_spares' => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
610 0 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
611 0 my ($scoreboard,$mem);
612  
613 # Make sure that we are not a child.
614 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
615 0 warn "Child $$ trying to check the spares!";
616 0 return 0;
617 }
618  
619 # Make sure there is still a socket factory. If not, then this server
620 # is shutting down.
621 0 ▶ unless ( defined $self->_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_factory) 0 0 0
622 0 warn 'Ending CheckSpares on the parent.'
623 if POE::Component::Server::SimpleHTTP::DEBUG;
624 0 return 1;
625 }
626  
627 # Retrieve the shared memory variable.
628 0 $scoreboard = $self->scoreboard;
629  
630 # in the test for maxrequestperchild the only
631 # way to have CheckSpare aware that we're shutting down
632 # is using this shared variable
633 0 ▶ if ( defined $scoreboard->{'shutdown'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scoreboard->{"shutdown"}) 0 0 0
634 0 warn 'Shutdown in progress, checkspare is useless'
635 if POE::Component::Server::SimpleHTTP::DEBUG;
636 0 return 1;
637 }
638  
639 0 ▶ $mem = tied %$scoreboard if defined $scoreboard;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scoreboard 0 0 0
640 0 ▶ unless ( defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mem) 0 0 0
641 0 warn 'SCOREBOARD is not tied! Aborting.';
642 0 return 0;
643 }
644  
645 # Check to see if we need another spare, and if so make sure we don't
646 # already have more than enough clients.
647 0 $mem->shlock(LOCK_SH);
648 0 ▶ if ( ( $scoreboard->{'spares'} < $self->minspareservers )
Branch: 1 branch
BranchTrueFalseTotal
if ($scoreboard->{'spares'} < $self->minspareservers and keys(%$scoreboard) - 2 < $self->maxclients) { } 0 0 0
Condition0%0/3
$scoreboard->{"spares"} < $self->minspareservers and keys(%$scoreboard) - 2 < $self->maxclients
!ll&&!rl&&r
000
MC/DC0%0/2
$scoreboard->{"spares"} < $self->minspareservers and keys(%$scoreboard) - 2 < $self->maxclients
$scoreboard->{"spares"} < $self->minspareservers keys(%$scoreboard) - 2 < $self->maxclients
Truth table0%0/3
A and B
A= $scoreboard->{"spares"} < $self->minspareserversB= keys(%$scoreboard) - 2 < $self->maxclients
ABresult
0X0
100
111
649 && ( ( keys(%$scoreboard) - 2 ) < $self->maxclients ) )
650 {
651 0 $mem->shlock(LOCK_UN);
652 0 my $pid = fork();
653  
654 0 ▶ if ( not defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif ($pid) { } 0 0 0
655 0 warn 'fork failed while creating a new spare.';
656 }
657 elsif ($pid) {
658  
659 # We are the parent.
660 }
661 else {
662 0 warn "Created spare child $$."
663 if POE::Component::Server::SimpleHTTP::DEBUG;
664  
665 # We are the child. Do something "childish".
666 0 $self->am_child( 1 );
667 0 $kernel->call( $session, 'add_scoreboard' );
668  
669 # Notify the other forked sessions that we have forked.
670 0 foreach my $sess ( keys( %{ $self->forkhandlers } ) ) {
671 0 $kernel->call( $sess, $self->forkhandlers->{$sess} );
672 }
673  
674 # Get to work!
675 0 $self->_factory->resume_accept();
676 }
677 }
678 else {
679  
680 # No new spares were needed.
681 0 $mem->shlock(LOCK_UN);
682 }
683  
684 # If we are the parent, then reschedule another spare check.
685 0 ▶ $kernel->delay_set( 'check_spares', CHECKSPARES_INTERVAL )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_child 0 0 0
686 unless $self->is_child;
687 1 };
688  
689 # Debug routine so that we can watch what is happening on the scoreboard.
690 0 ▶event 'show_scoreboard' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
691 0 my ($kernel,$self) = @_[KERNEL,OBJECT];
692 0 my ($scoreboard,$mem,$hcount,$pid);
693  
694 # Check to make sure we are not a child.
695 0 ▶ return 0 if $self->is_child;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_child 0 0 0
696  
697 # Check to make sure the scoreboard is still up.
698 0 $scoreboard = $self->scoreboard;
699 0 ▶ return 0 unless defined $scoreboard;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scoreboard 0 0 0
700  
701 # Retrieve the underlying class.
702 0 $mem = tied %$scoreboard;
703 0 ▶ if ( not defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mem) 0 0 0
704 0 warn 'SCOREBOARD is not tied! Aborting.';
705 0 return 0;
706 }
707  
708 # Lock the scoreboard and print out the entries.
709 0 $mem->shlock(LOCK_SH);
710 0 $hcount = 0;
711 print STDERR "[$$] actives = ", $scoreboard->{'actives'}, "\tspares = ",
712 0 $scoreboard->{'spares'}, "\n";
713 0 foreach $pid ( keys %$scoreboard ) {
714 0 ▶ next if $pid eq 'actives' or $pid eq 'spares';
Branch: 1 branch
BranchTrueFalseTotal
if $pid eq "actives" or $pid eq "spares" 0 0 0
Condition0%0/3
$pid eq "actives" or $pid eq "spares"
l!l&&r!l&&!r
000
MC/DC0%0/2
$pid eq "actives" or $pid eq "spares"
$pid eq "actives" $pid eq "spares"
Truth table0%0/3
A or B
A= $pid eq "actives"B= $pid eq "spares"
ABresult
1X1
011
000
715  
716 0 print STDERR $pid, " = ", $scoreboard->{$pid};
717 0 ▶ if ( ++$hcount % 5 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$hcount % 5 == 0) { } 0 0 0
718 0 print STDERR "\n";
719 }
720 else {
721 0 print STDERR "\t";
722 }
723 }
724 0 print STDERR "\n\n";
725 0 $mem->shlock(LOCK_UN);
726  
727 # If the socketfactory still exists then we should continue looping.
728 0 ▶ $kernel->delay_set( 'show_scoreboard', 1 ) if $self->_factory;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_factory 0 0 0
729 1 };
730  
731 # A child died :(
732 0 ▶event '_sig_chld' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
733 0 my ($kernel,$self,$pid) = @_[KERNEL,OBJECT,ARG1];
734 0 my ($scoreboard,$mem,$children);
735  
736 # Check to see if we are in preforked mode and the parent.
737 0 ▶ unless ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_child) 0 0 0
738  
739 # Retrieve our scoreboard.
740 0 $scoreboard = $self->scoreboard;
741 0 ▶ $mem = tied %$scoreboard if defined $scoreboard;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scoreboard 0 0 0
742 0 ▶ if ( not defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mem) 0 0 0
743 0 warn 'Cannot get the IPC::Shareable object for the SCOREBOARD!';
744 0 return;
745 }
746  
747 0 $mem->shlock(LOCK_EX);
748  
749 # Cleanup children here (they should never do it themselves).
750 0 ▶ if ( exists $scoreboard->{$pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $scoreboard->{$pid}) 0 0 0
751 0 ▶ if ( $scoreboard->{$pid} eq 'S' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scoreboard->{$pid} eq 'S') { } 0 0 0
elsif ($scoreboard->{$pid} eq 'A') { } 0 0 0
752 0 --$scoreboard->{'spares'};
753 }
754 elsif ( $scoreboard->{$pid} eq 'A' ) {
755 0 --$scoreboard->{'actives'};
756 }
757 0 delete $scoreboard->{$pid};
758 }
759  
760 # Get the number of children.
761 0 $children = keys(%$scoreboard) - 2;
762  
763 0 $mem->shlock(LOCK_UN);
764  
765 # If the children are dying and the SOCKETFACTORY no longer exists, then
766 # we are probably in a graceful shutdown.
767 0 ▶ $kernel->yield( 'SHUTDOWN', 'GRACEFUL' ) if $children <= 0 and !$self->_factory;
Branch: 1 branch
BranchTrueFalseTotal
if $children <= 0 and not $self->_factory 0 0 0
Condition0%0/3
$children <= 0 and not $self->_factory
!ll&&!rl&&r
000
MC/DC0%0/2
$children <= 0 and not $self->_factory
$children <= 0 not $self->_factory
Truth table0%0/3
A and not(B)
A= $children <= 0B= not $self->_factory
ABresult
0X0
100
111
768 }
769 0 $kernel->sig_handled();
770 1 };
771  
772 # Someone is asking us to quit...
773 0 ▶event '_sig_term' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
774 0 my ($kernel,$sig) = @_[KERNEL,ARG0];
775  
776 0 warn "Caught signal ", $sig, " inside $$. Initiating graceful shutdown."
777 if POE::Component::Server::SimpleHTTP::DEBUG;
778  
779 # Shutdown gracefully, and tell POE we handled the signal.
780 0 $kernel->yield( 'SHUTDOWN', 'GRACEFUL' );
781 0 $kernel->sig_handled();
782 1 };
783  
784 # Add the scoreboard entry for this child.
785 0 ▶event 'add_scoreboard' => sub {
Subroutine add_scoreboard: not called CC=5 SCAR=34.0
786 0 my $self = $_[OBJECT];
787 0 my ($scoreboard,$mem);
788  
789 # Check to see if we are preforked.
790 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
791 0 $scoreboard = $self->scoreboard;
792 0 ▶ $mem = tied %$scoreboard if defined $scoreboard;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scoreboard 0 0 0
793 0 ▶ if ( not defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $mem) { } 0 0 0
794  
795 # Don't do anyting if we can't lock stuff.
796 0 warn "SCOREBOARD is not tied to IPC::Shareable in child $$!";
797 }
798 else {
799  
800 # Lock the scoreboard and record ourself properly.
801 0 $mem->shlock(LOCK_EX);
802 0 ▶ if ( not exists $scoreboard->{$$} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $scoreboard->{$$}) 0 0 0
803 0 $scoreboard->{$$} = 'S';
804 0 ++$scoreboard->{'spares'};
805 }
806 0 $mem->shlock(LOCK_UN);
807 }
808 }
809  
810 0 return 1;
811 1 };
812  
813 # Set the scoreboard entry for this child.
814 0 ▶event 'update_scoreboard' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
815 0 my ($kernel,$self) = @_[KERNEL,OBJECT];
816 0 my ($scoreboard,$mem);
817  
818 # Check to see if we are preforked.
819 0 ▶ if ( $self->is_child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_child) 0 0 0
820 0 $scoreboard = $self->scoreboard;
821 0 ▶ $mem = tied %$scoreboard if defined $scoreboard;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scoreboard 0 0 0
822 0 ▶ if ( not defined $mem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $mem) { } 0 0 0
823  
824 # Don't do anyting if we can't lock stuff.
825 0 warn "SCOREBOARD is not tied to IPC::Shareable in child $$!";
826 }
827 else {
828  
829 # Lock the scoreboard and record ourself properly.
830 0 $mem->shlock(LOCK_EX);
831 0 ▶ if ( keys( %{ $self->_requests } ) == 0
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$self->_requests;} == 0 and $scoreboard->{$$} eq 'A') { } 0 0 0
elsif (keys %{$self->_requests;} != 0 and $scoreboard->{$$} eq 'S') { } 0 0 0
Condition0%0/3
keys %{$self->_requests;} == 0 and $scoreboard->{$$} eq "A"
!ll&&!rl&&r
000
MC/DC0%0/2
keys %{$self->_requests;} == 0 and $scoreboard->{$$} eq "A"
keys %{$self->_requests;} == 0 $scoreboard->{$$} eq "A"
Truth table0%0/3
A and B
A= keys %{$self->_requests;} == 0B= $scoreboard->{$$} eq "A"
ABresult
0X0
100
111
832 and $scoreboard->{$$} eq 'A' )
833 {
834 0 $scoreboard->{$$} = 'S';
835 0 ++$scoreboard->{'spares'};
836 0 --$scoreboard->{'actives'};
837  
838 # If we have too many spares then ask this one to shutdown.
839 0 ▶ if ( $scoreboard->{'spares'} > $self->maxspareservers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scoreboard->{"spares"} > $self->maxspareservers) 0 0 0
840 0 warn "Shutting down $$ because of too many spares."
841 if POE::Component::Server::SimpleHTTP::DEBUG;
842  
843 0 $kernel->yield( 'SHUTDOWN', 'GRACEFUL' );
844 }
845 }
846 0 ▶ elsif ( keys( %{ $self->_requests } ) != 0
Condition0%0/3
keys %{$self->_requests;} != 0 and $scoreboard->{$$} eq "S"
!ll&&!rl&&r
000
MC/DC0%0/2
keys %{$self->_requests;} != 0 and $scoreboard->{$$} eq "S"
keys %{$self->_requests;} != 0 $scoreboard->{$$} eq "S"
Truth table0%0/3
A and B
A= keys %{$self->_requests;} != 0B= $scoreboard->{$$} eq "S"
ABresult
0X0
100
111
847 and $scoreboard->{$$} eq 'S' )
848 {
849 0 $scoreboard->{$$} = 'A';
850 0 --$scoreboard->{'spares'};
851 0 ++$scoreboard->{'actives'};
852 }
853 0 $mem->shlock(LOCK_UN);
854 }
855 }
856  
857 0 return 1;
858 1 };
859  
860 1 ▶no MooseX::POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
861  
862 1 __PACKAGE__->meta->make_immutable;
863  
864 # End of module
865 1 1;
866  
867 __END__
↑ Summary