POE-Component-Server-PreforkTCP
view release on metacpan or search on metacpan
PreforkTCP.pm view on Meta::CPAN
print "try pause $pid \n";
$pheap->{accept_pause} = 1;
kill USR1 => $pid;
# send USR1 is means pause,
# here is the hardcode, if the user
# can configure which signal to use ?
# and , here pause the child may not
# as quickly as the client request,
# so maybe there is someone can send
# accept request after the child connected
# but before the master process the
# connection event.
}
}
elsif ( $type eq 'disconnected' ) {
$pheap->{connection} -- ;
$pheap->{connection_active} = time;
if ( $heap->{max_session} > 0
&& $pheap->{connection} < $heap->{max_session}
&& exists $pheap->{accept_pause} ) {
DEBUG &&
print "try resume $pid \n";
delete $pheap->{accept_pause} ;
kill USR2 => $pid;
# look above about "pause"
# to see discusss about signal and connection.
}
}
elsif ( $type eq 'grace_exit' ) {
$pheap->{grace_exit} = time;
}
}
sub _check_expire_children
{
my ($heap, $kernel) = @_;
my $children = $heap->{children};
my $server_count = 0;
my $spare_server_count = 0;
foreach my $pid ( keys %$children ) {
my $pheap = $heap->{'child_' . $pid} ;
# something is wrong about server pid?
unless( defined $pheap ) {
$kernel->yield('kill', $pid );
next;
# if it can kill other process unfortunatly?
}
if ( defined $pheap->{grace_exit}
&& time() - $pheap->{grace_exit} > $heap->{grace_expire_time} ) {
$kernel->yield('kill', $pid );
next;
# kill the server which exit gracefully.
# if the heartbeat cycle is too long and
# system is too busy, if the system will
# spawn some other process which pid is it?
# maybe , maybe few, but not none.
}
$kernel->yield('term', $pid )
if (
# if the server has run too long time?
(
$heap->{max_life_time} > 0
&& ( time() - $pheap->{starttime} > $heap->{max_life_time} )
# && print "term life \n"
)
||
# if the server has accept too many connection?
(
$heap->{max_life_times} > 0
&& ($pheap->{connection_num} > $heap->{max_life_times} )
# && print "term life times \n"
)
||
# if the server has some time without heartbeat?
(
$heap->{max_expire_time} > 0
&& ( time() - $pheap->{child_heartbeat} > $heap->{max_expire_time} )
# && print "term expire ", $heap->{max_expire_time} , " ", $pheap->{child_heartbeat}," \n"
)
||
# if the server has heartbeat but has some time not to do anything?
(
$heap->{max_spare_time} > 0
&& ( time() - $pheap->{connection_active} > $heap->{max_spare_time} )
# && print "term spare \n"
)
) ;
$server_count ++ ;
$spare_server_count ++
if ( (! exists $pheap->{connection}) || ($pheap->{connection} == 0) ) ;
}
return ( $server_count, $spare_server_count );
}
sub master_heartbeat
{
my ( $session, $heap, $kernel ) = @_[SESSION, HEAP, KERNEL];
$heap->{heartbeat_count} ++ ;
my ( $server_count, $spare_server_count ) = _check_expire_children( $heap, $kernel );
my $term_servers = 0;
# check server numbers for max spare server number ..
if ( $heap->{max_spare_server} > 0
&& $heap->{max_spare_server} < $spare_server_count ) {
$term_servers = $spare_server_count - $heap->{max_spare_server} ;
}
# check server numbers for max server number ..
if ( $heap->{max_server} > 0
( run in 2.338 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )