| 1 | package POE::Component::Server::PreforkTCP; | ||
| 2 | |||
| 3 | |||
| 4 | #sub POE::Kernel::TRACE_EVENTS { 1 } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use vars qw($VERSION); | ||
| 9 | $VERSION = 0.11; | ||
| 10 | |||
| 11 | use POE; | ||
| 12 | use POE::Session; | ||
| 13 | use POE::Wheel::Run; | ||
| 14 | use POE::Component::Server::TCP; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | sub DEBUG { 0 } | ||
| 18 | |||
| 19 | ################################################################## | ||
| 20 | # | ||
| 21 | # Master Server Section | ||
| 22 | # | ||
| 23 | ################################################################## | ||
| 24 | |||
| 25 | sub _preforkConfig | ||
| 26 | { | ||
| 27 | my $params = shift; | ||
| 28 | |||
| 29 | $params->{Alias} = 'prefork_server' | ||
| 30 | unless exists $params->{Alias}; | ||
| 31 | |||
| 32 | my $heap = { | ||
| 33 | server_alias => $params->{Alias}, | ||
| 34 | }; | ||
| 35 | |||
| 36 | $heap->{master_alias} = delete $params->{MasterAlias} | ||
| 37 | || 'prefork_master'; | ||
| 38 | |||
| 39 | $heap->{max_server} = delete $params->{MaxServer} | ||
| 40 | || 20; | ||
| 41 | |||
| 42 | $heap->{start_server} = delete $params->{StartServer} | ||
| 43 | || 5; | ||
| 44 | |||
| 45 | $heap->{min_server} = delete $params->{MinServer} | ||
| 46 | || $heap->{start_server} ; | ||
| 47 | |||
| 48 | $heap->{min_spare_server} = delete $params->{MinSpareServer} | ||
| 49 | || $heap->{min_server}; | ||
| 50 | |||
| 51 | $heap->{max_spare_server} = delete $params->{MaxSpareServer} | ||
| 52 | || ($heap->{max_server} - $heap->{min_server}); | ||
| 53 | |||
| 54 | $heap->{master_heartbeat_time} = delete $params->{MasterHeartBeatTime} | ||
| 55 | || 10; | ||
| 56 | |||
| 57 | $heap->{server_heartbeat_time} = delete $params->{ServerHeartBeatTime} | ||
| 58 | || $heap->{master_heartbeat_time} / 2 ; | ||
| 59 | |||
| 60 | $heap->{max_session} = delete $params->{MaxSessionPerServer} | ||
| 61 | || 0; | ||
| 62 | |||
| 63 | $heap->{max_expire_time} = delete $params->{MaxServerExpireTime} | ||
| 64 | || $heap->{master_heartbeat_time} * 5; | ||
| 65 | |||
| 66 | $heap->{grace_expire_time} = delete $params->{GraceExpireTime} | ||
| 67 | || $heap->{max_expire_time} ; | ||
| 68 | |||
| 69 | $heap->{max_spare_time} = delete $params->{MaxServerSpareTime} | ||
| 70 | || 0; | ||
| 71 | |||
| 72 | # $heap->{max_free_time} = delete $params->{MaxServerFreeTime} | ||
| 73 | # || 0; | ||
| 74 | |||
| 75 | $heap->{max_life_time} = delete $params->{MaxServerLifeTime} | ||
| 76 | || 0; | ||
| 77 | |||
| 78 | $heap->{max_life_times} = delete $params->{MaxServerLifeTimes} | ||
| 79 | || 0; | ||
| 80 | |||
| 81 | $heap->{shutdown_children} = delete $params->{ShutdownChildren} | ||
| 82 | || 0; | ||
| 83 | DEBUG || | ||
| 84 | return $heap; | ||
| 85 | |||
| 86 | print "HEAP PARAMS \n"; | ||
| 87 | foreach ( keys %$heap ) { | ||
| 88 | print "\t $_ => $heap->{$_} \n"; | ||
| 89 | } | ||
| 90 | |||
| 91 | $heap; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _preforkHandler | ||
| 95 | { | ||
| 96 | my $params = shift; | ||
| 97 | my $master_alias = shift; | ||
| 98 | |||
| 99 | my $old_error_handler = delete $params->{Error} | ||
| 100 | || sub { | ||
| 101 | delete $_[HEAP]->{listener}; | ||
| 102 | |||
| 103 | # here is the default Compoent::Server::TCP | ||
| 104 | # error handler, it will be call in server | ||
| 105 | # session when error happened to shutdown server | ||
| 106 | }; | ||
| 107 | |||
| 108 | $params->{Error} = sub { | ||
| 109 | my ( $heap, $kernel, $scmd ) = @_[HEAP, KERNEL, ARG3]; | ||
| 110 | |||
| 111 | if ( $scmd eq 'pause' ) { | ||
| 112 | my $accept_socket = $heap->{listener}->[ | ||
| 113 | POE::Wheel::SocketFactory::MY_SOCKET_HANDLE() | ||
| 114 | ] ; | ||
| 115 | $kernel->select_pause_read ( $accept_socket ); | ||
| 116 | #$kernel->select_pause_write ( $accept_socket ); | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | elsif ( $scmd eq 'resume' ) { | ||
| 120 | my $accept_socket = $heap->{listener}->[ | ||
| 121 | POE::Wheel::SocketFactory::MY_SOCKET_HANDLE() | ||
| 122 | ] ; | ||
| 123 | $kernel->select_resume_read ( $accept_socket ); | ||
| 124 | #$kernel->select_resume_write ( $accept_socket ); | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | $old_error_handler->(@_); | ||
| 128 | |||
| 129 | # since POE::Component::Server::TCP just handle few | ||
| 130 | # event for accept SocketFactory Wheel, | ||
| 131 | # (one is error, another is accept) | ||
| 132 | |||
| 133 | # but i need one event handler to setup | ||
| 134 | # the accept socket's option, to pause/resume it. | ||
| 135 | # of course the best way is : change the | ||
| 136 | # source of POE::Coponent::Server::TCP to | ||
| 137 | # add other handlers for "pause" and "resume" | ||
| 138 | # but i don't want to do like this. | ||
| 139 | # since the Server::TCP is not included in the package. | ||
| 140 | # and i think the ::TCP need not "resume"/"pause". | ||
| 141 | |||
| 142 | # another way is rewrite the code about tcp servers | ||
| 143 | # but i don't like to do same work | ||
| 144 | # since the Server::TCP has done it. | ||
| 145 | |||
| 146 | # so i just do a wrapper for error_handler | ||
| 147 | # to do the resume/pause work, before the | ||
| 148 | # Server::TCP can do it. | ||
| 149 | |||
| 150 | # today i read the reply in perl.poe groups, | ||
| 151 | # i found Rocco has add pause_accept and resume_accept | ||
| 152 | # for SocketFacotory. | ||
| 153 | # next revision i will check the SocketFactory's Version | ||
| 154 | # and use the two functions , not access the | ||
| 155 | # SocketFactory's internel data struct, too. | ||
| 156 | |||
| 157 | }; | ||
| 158 | |||
| 159 | my $old_connected_handler = delete $params->{ClientConnected} | ||
| 160 | || sub { }; | ||
| 161 | |||
| 162 | $params->{ClientConnected} = sub { | ||
| 163 | my ( $heap, $kernel, $session) | ||
| 164 | = @_[HEAP,KERNEL,SESSION]; | ||
| 165 | |||
| 166 | $kernel->post( $master_alias, 'client_manage', | ||
| 167 | 'connected', | ||
| 168 | $session->ID(), | ||
| 169 | $heap->{remote_ip}, | ||
| 170 | $heap->{remote_port}, | ||
| 171 | ); | ||
| 172 | $old_connected_handler->(@_); | ||
| 173 | }; | ||
| 174 | |||
| 175 | my $old_disconnected_handler = delete $params->{ClientDisconnected} | ||
| 176 | || sub { }; | ||
| 177 | |||
| 178 | $params->{ClientDisconnected} = sub { | ||
| 179 | my ( $heap, $kernel, $session) | ||
| 180 | = @_[HEAP,KERNEL,SESSION]; | ||
| 181 | |||
| 182 | $kernel->post( $master_alias, 'client_manage', | ||
| 183 | 'disconnected', | ||
| 184 | $session->ID(), | ||
| 185 | $heap->{remote_ip}, | ||
| 186 | $heap->{remote_port}, | ||
| 187 | ); | ||
| 188 | $old_disconnected_handler->(@_); | ||
| 189 | }; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _preforkMaster | ||
| 193 | { | ||
| 194 | my ( $params, $newheap ) = @_; | ||
| 195 | |||
| 196 | create POE::Session ( | ||
| 197 | inline_states => { | ||
| 198 | _start => \&master_start, | ||
| 199 | _stop => \&master_stop, | ||
| 200 | |||
| 201 | born => \&born_server, | ||
| 202 | term => \&term_server, | ||
| 203 | kill => \&kill_server, | ||
| 204 | |||
| 205 | heartbeat => \&master_heartbeat, | ||
| 206 | |||
| 207 | pause => \&accept_pause, | ||
| 208 | resume => \&accept_resume, | ||
| 209 | |||
| 210 | child_stdout => \&master_child_out, | ||
| 211 | signal =>\&master_signal, | ||
| 212 | |||
| 213 | # child_close and child_error to handle the child exit. | ||
| 214 | # the work have done in singal event, too, so the two event | ||
| 215 | # is useless. | ||
| 216 | |||
| 217 | child_close => \&master_child_close, | ||
| 218 | child_error => \&master_child_error, | ||
| 219 | |||
| 220 | }, | ||
| 221 | heap => $newheap, | ||
| 222 | ); | ||
| 223 | } | ||
| 224 | |||
| 225 | sub master_stop | ||
| 226 | { | ||
| 227 | my ($heap, $kernel) = @_[HEAP,KERNEL]; | ||
| 228 | |||
| 229 | # here , cancel heartbeat event, make the session close. | ||
| 230 | $kernel->state( 'heartbeat' ); | ||
| 231 | |||
| 232 | if ( $heap->{shutdown_children} == 0 ) { | ||
| 233 | return ; | ||
| 234 | } | ||
| 235 | |||
| 236 | # maybe the function is useless, | ||
| 237 | # now, the event can be used to kill | ||
| 238 | # all children server and master server, | ||
| 239 | # just post "_stop" to the "prefork_master". | ||
| 240 | |||
| 241 | # since children will be recieved SIGPIPE and exit | ||
| 242 | # ?? i don't get the result i guess | ||
| 243 | # if user INT the master process, | ||
| 244 | # child don't get sigPIPE, why? | ||
| 245 | |||
| 246 | my $children= $heap->{children}; | ||
| 247 | foreach my $pid ( keys %$children ) { | ||
| 248 | # $kernel->yield('kill', $pid ); | ||
| 249 | kill KILL => $pid; | ||
| 250 | delete $heap->{children}->{$pid}; | ||
| 251 | delete $heap->{'child_' . $pid}; | ||
| 252 | } | ||
| 253 | |||
| 254 | delete $heap->{children}; | ||
| 255 | |||
| 256 | $kernel->post( $heap->{server_alias}, 'shutdown' ); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub master_signal | ||
| 260 | { | ||
| 261 | my ( $heap, $kernel) = @_[HEAP, KERNEL] ; | ||
| 262 | my ( $signal, $pid, $status )= @_[ARG0, ARG1, ARG2]; | ||
| 263 | |||
| 264 | if ( $signal eq 'CHLD' ) { | ||
| 265 | DEBUG && print "child $pid singal $signal\n" ; | ||
| 266 | $kernel->yield('kill', $pid ); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | sub master_child_error | ||
| 271 | { | ||
| 272 | my ( $heap, $kernel, $wheel_id ) = @_[HEAP, KERNEL, ARG3 ]; | ||
| 273 | |||
| 274 | return ; | ||
| 275 | |||
| 276 | # the child's exit can be handler better by signal (CHLD), | ||
| 277 | # so the function can be skip | ||
| 278 | |||
| 279 | my $children= $heap->{children}; | ||
| 280 | foreach my $pid ( keys %$children ) { | ||
| 281 | if ( $children->{$pid}->ID() == $wheel_id ) { | ||
| 282 | $kernel->yield( 'kill', $pid ); | ||
| 283 | return ; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | sub master_child_close | ||
| 289 | { | ||
| 290 | my ( $heap, $kernel, $wheel_id ) = @_[HEAP, KERNEL, ARG3 ]; | ||
| 291 | |||
| 292 | return ; | ||
| 293 | |||
| 294 | # the child's exit can be handler better by signal (CHLD), | ||
| 295 | # so the function can be skip | ||
| 296 | |||
| 297 | my $children= $heap->{children}; | ||
| 298 | foreach my $pid ( keys %$children ) { | ||
| 299 | if ( $children->{$pid}->ID() == $wheel_id ) { | ||
| 300 | $kernel->yield( 'kill', $pid ); | ||
| 301 | return ; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | sub accept_pause | ||
| 307 | { | ||
| 308 | $_[KERNEL]->call( | ||
| 309 | $_[HEAP]->{server_alias} , | ||
| 310 | 'tcp_server_got_error', | ||
| 311 | undef, undef, undef, | ||
| 312 | 'pause' ); | ||
| 313 | } | ||
| 314 | |||
| 315 | sub accept_resume | ||
| 316 | { | ||
| 317 | $_[KERNEL]->call( | ||
| 318 | $_[HEAP]->{server_alias} , | ||
| 319 | 'tcp_server_got_error', | ||
| 320 | undef, undef, undef, | ||
| 321 | 'resume' ); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub term_server | ||
| 325 | { | ||
| 326 | my ( $heap, $pid ) = @_[HEAP, ARG0]; | ||
| 327 | return unless ( defined $pid ); | ||
| 328 | |||
| 329 | # here it is a way to tell child by signal | ||
| 330 | # maybe use POE::Wheel::Run's put is better, | ||
| 331 | # but a little complex... | ||
| 332 | |||
| 333 | kill INT => $pid ; | ||
| 334 | |||
| 335 | # the children has handled the INT singal, | ||
| 336 | # so the kill don't term the child , | ||
| 337 | # then the child will exit gracefully. | ||
| 338 | |||
| 339 | DEBUG | ||
| 340 | && print "term: close the server $pid \n"; | ||
| 341 | |||
| 342 | # set the graceful exit timestamp, which is used | ||
| 343 | # to term the child if it can not exit gracefully. | ||
| 344 | |||
| 345 | my $pheap = $heap->{'child_' . $pid}; | ||
| 346 | unless ( defined $pheap->{grace_exit} ) { | ||
| 347 | $pheap->{grace_exit} = time(); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | sub kill_server | ||
| 352 | { | ||
| 353 | my ( $heap, $pid ) = @_[HEAP, ARG0]; | ||
| 354 | return unless ( defined $pid ); | ||
| 355 | |||
| 356 | # here it is a way to tell child by signal | ||
| 357 | # maybe use POE::Wheel::Run's put is better, but a little complex... | ||
| 358 | DEBUG | ||
| 359 | && print "kill: close the server $pid \n"; | ||
| 360 | |||
| 361 | # clean the relative data of the child . | ||
| 362 | |||
| 363 | if ( ! delete $heap->{children}->{$pid} | ||
| 364 | && ! delete $heap->{'child_' . $pid } ) { | ||
| 365 | DEBUG && | ||
| 366 | print "kill, $pid maybe have killed\n"; | ||
| 367 | return ; | ||
| 368 | } | ||
| 369 | |||
| 370 | unless ( kill ( KILL => $pid ) ) { | ||
| 371 | DEBUG && print "server has closed!\n"; | ||
| 372 | } | ||
| 373 | # send the KILL signal. | ||
| 374 | } | ||
| 375 | |||
| 376 | sub born_server | ||
| 377 | { | ||
| 378 | my ( $heap , $kernel ) = @_[HEAP,KERNEL]; | ||
| 379 | |||
| 380 | my $child = new POE::Wheel::Run ( | ||
| 381 | Program => sub { | ||
| 382 | server_main( $heap, $kernel ); | ||
| 383 | }, | ||
| 384 | StdoutEvent => 'child_stdout', | ||
| 385 | CloseEvent => 'child_close', | ||
| 386 | ErrorEvent => 'child_error', | ||
| 387 | ); | ||
| 388 | DEBUG | ||
| 389 | && print "born the server ", | ||
| 390 | $child->PID(), " " , | ||
| 391 | $child->ID(), "\n"; | ||
| 392 | |||
| 393 | # set the relative data of the child. | ||
| 394 | $heap->{children}->{$child->PID()} = $child; | ||
| 395 | $heap->{'child_' . $child->PID() } = {starttime => time()}; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub master_start | ||
| 399 | { | ||
| 400 | my ( $heap, $kernel, $session) = @_[HEAP, KERNEL, SESSION] ; | ||
| 401 | |||
| 402 | $heap->{children} = {}; | ||
| 403 | |||
| 404 | my $master_alias = $heap->{master_alias} | ||
| 405 | || 'prefork_master' ; | ||
| 406 | $kernel->alias_set( $master_alias ); | ||
| 407 | |||
| 408 | DEBUG && | ||
| 409 | print "Try start " , $heap->{start_server} , " child \n"; | ||
| 410 | |||
| 411 | $kernel->call( $master_alias, 'pause' ); | ||
| 412 | |||
| 413 | for( my $i = 0 ; $i < $heap->{start_server} ; $i ++ ) { | ||
| 414 | $kernel->call( $master_alias, 'born' ); | ||
| 415 | } | ||
| 416 | |||
| 417 | $kernel->sig('CHLD', 'signal' ); | ||
| 418 | |||
| 419 | $heap->{heartbeat_count} = 0; | ||
| 420 | $kernel->delay( 'heartbeat', $heap->{master_heartbeat_time} ); | ||
| 421 | } | ||
| 422 | |||
| 423 | sub master_child_out | ||
| 424 | { | ||
| 425 | my ($out, $heap) = @_[ARG0, HEAP]; | ||
| 426 | |||
| 427 | my ( $wheelid, $pid, $type, $param ) = split(/\s+/, $out ); | ||
| 428 | # the format of children's out put. | ||
| 429 | |||
| 430 | DEBUG && | ||
| 431 | print "CHILD : $out\n"; | ||
| 432 | |||
| 433 | my $pheap = $heap->{'child_' . $pid} | ||
| 434 | or return ; | ||
| 435 | |||
| 436 | if ( $type eq 'heartbeat' ) { | ||
| 437 | $pheap->{child_heartbeat} = time; | ||
| 438 | $pheap->{child_heartbeat_tick} = $heap->{heartbeat_count}; | ||
| 439 | } | ||
| 440 | elsif ( $type eq 'connected' ) { | ||
| 441 | $pheap->{connection} ++ ; | ||
| 442 | $pheap->{connection_num} ++ ; | ||
| 443 | $pheap->{connection_active} = time; | ||
| 444 | if ( $heap->{max_session} > 0 | ||
| 445 | && $pheap->{connection} >= $heap->{max_session} | ||
| 446 | && ! exists $pheap->{accept_pause} ) { | ||
| 447 | DEBUG && | ||
| 448 | print "try pause $pid \n"; | ||
| 449 | $pheap->{accept_pause} = 1; | ||
| 450 | kill USR1 => $pid; | ||
| 451 | # send USR1 is means pause, | ||
| 452 | # here is the hardcode, if the user | ||
| 453 | # can configure which signal to use ? | ||
| 454 | |||
| 455 | # and , here pause the child may not | ||
| 456 | # as quickly as the client request, | ||
| 457 | # so maybe there is someone can send | ||
| 458 | # accept request after the child connected | ||
| 459 | # but before the master process the | ||
| 460 | # connection event. | ||
| 461 | } | ||
| 462 | } | ||
| 463 | elsif ( $type eq 'disconnected' ) { | ||
| 464 | $pheap->{connection} -- ; | ||
| 465 | $pheap->{connection_active} = time; | ||
| 466 | if ( $heap->{max_session} > 0 | ||
| 467 | && $pheap->{connection} < $heap->{max_session} | ||
| 468 | && exists $pheap->{accept_pause} ) { | ||
| 469 | DEBUG && | ||
| 470 | print "try resume $pid \n"; | ||
| 471 | delete $pheap->{accept_pause} ; | ||
| 472 | kill USR2 => $pid; | ||
| 473 | # look above about "pause" | ||
| 474 | # to see discusss about signal and connection. | ||
| 475 | } | ||
| 476 | } | ||
| 477 | elsif ( $type eq 'grace_exit' ) { | ||
| 478 | $pheap->{grace_exit} = time; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | sub _check_expire_children | ||
| 483 | { | ||
| 484 | my ($heap, $kernel) = @_; | ||
| 485 | |||
| 486 | my $children = $heap->{children}; | ||
| 487 | my $server_count = 0; | ||
| 488 | my $spare_server_count = 0; | ||
| 489 | |||
| 490 | foreach my $pid ( keys %$children ) { | ||
| 491 | my $pheap = $heap->{'child_' . $pid} ; | ||
| 492 | |||
| 493 | # something is wrong about server pid? | ||
| 494 | unless( defined $pheap ) { | ||
| 495 | $kernel->yield('kill', $pid ); | ||
| 496 | next; | ||
| 497 | |||
| 498 | # if it can kill other process unfortunatly? | ||
| 499 | } | ||
| 500 | |||
| 501 | if ( defined $pheap->{grace_exit} | ||
| 502 | && time() - $pheap->{grace_exit} > $heap->{grace_expire_time} ) { | ||
| 503 | $kernel->yield('kill', $pid ); | ||
| 504 | next; | ||
| 505 | |||
| 506 | # kill the server which exit gracefully. | ||
| 507 | # if the heartbeat cycle is too long and | ||
| 508 | # system is too busy, if the system will | ||
| 509 | # spawn some other process which pid is it? | ||
| 510 | # maybe , maybe few, but not none. | ||
| 511 | } | ||
| 512 | |||
| 513 | $kernel->yield('term', $pid ) | ||
| 514 | if ( | ||
| 515 | # if the server has run too long time? | ||
| 516 | ( | ||
| 517 | $heap->{max_life_time} > 0 | ||
| 518 | && ( time() - $pheap->{starttime} > $heap->{max_life_time} ) | ||
| 519 | # && print "term life \n" | ||
| 520 | ) | ||
| 521 | || | ||
| 522 | # if the server has accept too many connection? | ||
| 523 | ( | ||
| 524 | $heap->{max_life_times} > 0 | ||
| 525 | && ($pheap->{connection_num} > $heap->{max_life_times} ) | ||
| 526 | # && print "term life times \n" | ||
| 527 | ) | ||
| 528 | || | ||
| 529 | # if the server has some time without heartbeat? | ||
| 530 | ( | ||
| 531 | $heap->{max_expire_time} > 0 | ||
| 532 | && ( time() - $pheap->{child_heartbeat} > $heap->{max_expire_time} ) | ||
| 533 | # && print "term expire ", $heap->{max_expire_time} , " ", $pheap->{child_heartbeat}," \n" | ||
| 534 | ) | ||
| 535 | || | ||
| 536 | # if the server has heartbeat but has some time not to do anything? | ||
| 537 | ( | ||
| 538 | $heap->{max_spare_time} > 0 | ||
| 539 | && ( time() - $pheap->{connection_active} > $heap->{max_spare_time} ) | ||
| 540 | # && print "term spare \n" | ||
| 541 | ) | ||
| 542 | ) ; | ||
| 543 | |||
| 544 | $server_count ++ ; | ||
| 545 | $spare_server_count ++ | ||
| 546 | if ( (! exists $pheap->{connection}) || ($pheap->{connection} == 0) ) ; | ||
| 547 | } | ||
| 548 | return ( $server_count, $spare_server_count ); | ||
| 549 | } | ||
| 550 | |||
| 551 | sub master_heartbeat | ||
| 552 | { | ||
| 553 | my ( $session, $heap, $kernel ) = @_[SESSION, HEAP, KERNEL]; | ||
| 554 | |||
| 555 | $heap->{heartbeat_count} ++ ; | ||
| 556 | |||
| 557 | my ( $server_count, $spare_server_count ) = _check_expire_children( $heap, $kernel ); | ||
| 558 | |||
| 559 | my $term_servers = 0; | ||
| 560 | |||
| 561 | # check server numbers for max spare server number .. | ||
| 562 | if ( $heap->{max_spare_server} > 0 | ||
| 563 | && $heap->{max_spare_server} < $spare_server_count ) { | ||
| 564 | $term_servers = $spare_server_count - $heap->{max_spare_server} ; | ||
| 565 | } | ||
| 566 | |||
| 567 | # check server numbers for max server number .. | ||
| 568 | if ( $heap->{max_server} > 0 | ||
| 569 | && $heap->{max_server} < $server_count | ||
| 570 | && $term_servers < $server_count - $heap->{max_server} ) { | ||
| 571 | $term_servers = $server_count - $heap->{max_server} ; | ||
| 572 | } | ||
| 573 | |||
| 574 | if ( $term_servers > 0 ) { | ||
| 575 | my $children = $heap->{children} ; | ||
| 576 | foreach my $pid ( keys %$children ) { | ||
| 577 | $kernel->yield('term', $pid ); | ||
| 578 | last if ( --$term_servers == 0 ); | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | # if there is a server termed, so don't born new one. | ||
| 583 | if ( $term_servers == 0 ) { | ||
| 584 | my $born_servers = 0; | ||
| 585 | |||
| 586 | # check server numbers for min spare server number .. | ||
| 587 | if ( $heap->{min_spare_server} > 0 | ||
| 588 | && $heap->{min_spare_server} > $spare_server_count ) { | ||
| 589 | $born_servers = $heap->{min_spare_server} - $spare_server_count ; | ||
| 590 | } | ||
| 591 | |||
| 592 | # check server numbers for min server number.. | ||
| 593 | if ( $heap->{min_server} > 0 | ||
| 594 | && $heap->{min_server} > $server_count | ||
| 595 | && $heap->{min_server} - $server_count > $born_servers ) { | ||
| 596 | $born_servers = $heap->{min_server} - $server_count ; | ||
| 597 | } | ||
| 598 | |||
| 599 | for( my $i=0; $i<$born_servers; $i++ ) { | ||
| 600 | $kernel->yield('born'); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | # term server is check at first before born servers. | ||
| 604 | # so, the maxserver has more level than other parameter. | ||
| 605 | |||
| 606 | $kernel->delay( 'heartbeat', $heap->{master_heartbeat_time} ); | ||
| 607 | } | ||
| 608 | |||
| 609 | sub new | ||
| 610 | { | ||
| 611 | my ( $type, %params ) = @_; | ||
| 612 | |||
| 613 | my $newheap = _preforkConfig( \%params ); | ||
| 614 | |||
| 615 | _preforkHandler( \%params, $newheap->{master_alias} ); | ||
| 616 | |||
| 617 | new POE::Component::Server::TCP( | ||
| 618 | %params, | ||
| 619 | ); | ||
| 620 | |||
| 621 | POE::Kernel->call( $newheap->{server_alias}, | ||
| 622 | 'tcp_server_got_error', | ||
| 623 | undef, undef, undef, | ||
| 624 | 'pause' | ||
| 625 | ); | ||
| 626 | |||
| 627 | # here to pause the server as early as possible | ||
| 628 | # another way is pause the server when master session start | ||
| 629 | # or after forking, | ||
| 630 | # which is also ok but maybe a little later. | ||
| 631 | # it will be terrible if there are some connection request | ||
| 632 | # when child has not been forked. | ||
| 633 | |||
| 634 | # if pause accept in master session, we can get | ||
| 635 | # better interface to do pause and resume since | ||
| 636 | # we can use the unique interface to post event | ||
| 637 | # to master session, not post event to Compoent::Server::TCP | ||
| 638 | # directly. maybe it is better. | ||
| 639 | |||
| 640 | # maybe everything can be improve to rewrite Accept Handler | ||
| 641 | # for Server::TCP. it is next step... | ||
| 642 | |||
| 643 | my $master = _preforkMaster( \%params, $newheap ); | ||
| 644 | |||
| 645 | undef; | ||
| 646 | } | ||
| 647 | |||
| 648 | |||
| 649 | ###################################################################### | ||
| 650 | # | ||
| 651 | # child server | ||
| 652 | # | ||
| 653 | ###################################################################### | ||
| 654 | |||
| 655 | sub server_clean_wheel | ||
| 656 | { | ||
| 657 | my $heap = shift; | ||
| 658 | |||
| 659 | my $children = $heap->{children}; | ||
| 660 | my $sum = 0; | ||
| 661 | my $count = 0; | ||
| 662 | foreach my $key ( keys %$children ) { | ||
| 663 | $sum ++ ; | ||
| 664 | if ( $key == $$ ) { | ||
| 665 | $heap->{wheel_id} = $children->{$key}->ID(); | ||
| 666 | # next ; | ||
| 667 | } | ||
| 668 | |||
| 669 | # next if ( $key == $$ ); | ||
| 670 | # remove all children and relative wheel | ||
| 671 | |||
| 672 | delete $children->{$key}; | ||
| 673 | delete $heap->{'child_'. $key }; | ||
| 674 | $count ++; | ||
| 675 | |||
| 676 | # here i think it is very important to remove these wheel, | ||
| 677 | # since i use many poe::wheel::run in same program, | ||
| 678 | # it is means the process do many times fork(), | ||
| 679 | # so there are any poe::wheel::run obj in the children, | ||
| 680 | # it is useless , if these wheel exist in children, | ||
| 681 | # a child maybe sent the out to another child, | ||
| 682 | # not to parent process directly. | ||
| 683 | |||
| 684 | } | ||
| 685 | delete $heap->{children}; | ||
| 686 | # in fact , just the one is enough in function, | ||
| 687 | # but it will not clean heap. | ||
| 688 | |||
| 689 | # my $master_alias = $heap->{master_alias} || 'prefork_master' ; | ||
| 690 | # my $session = $kernel->alias_resolv( $master_alias ); | ||
| 691 | # undef $session; | ||
| 692 | |||
| 693 | # in fact, i want to release the master session | ||
| 694 | # and create the new server session | ||
| 695 | # in child server, but failed, so it means | ||
| 696 | # i need study POE more. | ||
| 697 | # so i have to clean the event the master own | ||
| 698 | # and add new event state the server want ... | ||
| 699 | |||
| 700 | DEBUG && | ||
| 701 | print "release $count wheel in $sum \n"; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub server_main | ||
| 705 | { | ||
| 706 | my ( $heap, $kernel ) = @_; | ||
| 707 | |||
| 708 | DEBUG && | ||
| 709 | print "Sub process $$ start ...\n"; | ||
| 710 | |||
| 711 | server_clean_wheel( $heap ); | ||
| 712 | |||
| 713 | # some state of master server is useful, such as resume and pause. | ||
| 714 | $kernel->call( $heap->{master_alias} ,'resume' ); | ||
| 715 | |||
| 716 | # $kernel->delay( 'resume', 10 ); | ||
| 717 | # use delay can test if the resume is useful, when delay resume, | ||
| 718 | # any connected request will wait the server resume after 10 second, | ||
| 719 | # not refused. | ||
| 720 | |||
| 721 | # clean some useless event state | ||
| 722 | foreach my $event ( qw(signal born term kill child_close child_error)){ | ||
| 723 | $kernel->state( $event ); | ||
| 724 | } | ||
| 725 | |||
| 726 | my $states = { | ||
| 727 | heartbeat => \&server_heartbeat, | ||
| 728 | shutdown => \&server_shutdown, | ||
| 729 | server_signal => \&server_signal, | ||
| 730 | client_manage => \&server_manage, | ||
| 731 | }; | ||
| 732 | foreach my $event ( keys %$states ) { | ||
| 733 | $kernel->state( $event, $states->{$event} ); | ||
| 734 | } | ||
| 735 | |||
| 736 | # release CHLD signal handler. | ||
| 737 | $kernel->sig ( 'CHLD' ); | ||
| 738 | |||
| 739 | foreach my $signal ( qw(PIPE USR1 USR2 INT) ) { | ||
| 740 | $kernel->sig( $signal, 'server_signal' ); | ||
| 741 | } | ||
| 742 | |||
| 743 | $kernel->yield( 'heartbeat' ); | ||
| 744 | |||
| 745 | $kernel->run(); | ||
| 746 | |||
| 747 | exit 0; | ||
| 748 | } | ||
| 749 | |||
| 750 | sub server_signal | ||
| 751 | { | ||
| 752 | my ( $heap, $kernel) = @_[HEAP, KERNEL] ; | ||
| 753 | my ( $signal )= @_[ARG0]; | ||
| 754 | |||
| 755 | DEBUG && | ||
| 756 | print "$$ singal $signal recieved\n" ; | ||
| 757 | |||
| 758 | if ( $signal eq 'USR1' ) { | ||
| 759 | $kernel->post( $heap->{master_alias}, 'pause'); | ||
| 760 | } | ||
| 761 | elsif ( $signal eq 'USR2' ) { | ||
| 762 | $kernel->post( $heap->{master_alias}, 'resume'); | ||
| 763 | } | ||
| 764 | elsif ( $signal eq 'INT' ) { | ||
| 765 | $kernel->post( $heap->{master_alias}, 'shutdown'); | ||
| 766 | } | ||
| 767 | elsif ( $signal eq 'PIPE' ) { | ||
| 768 | DEBUG && | ||
| 769 | $heap->{client}->put( "SIGPIPE recievied\n"); | ||
| 770 | # ?? ..... | ||
| 771 | # when user INT the master perl process, | ||
| 772 | # what signal the child server process get? | ||
| 773 | |||
| 774 | $kernel->post( $heap->{master_alias}, 'shutdown'); | ||
| 775 | } | ||
| 776 | } | ||
| 777 | |||
| 778 | sub server_manage | ||
| 779 | { | ||
| 780 | # when client connection and disconnection , it will post the event. | ||
| 781 | # it is the easiest way to manage the connection number | ||
| 782 | # and, here i just print the message to parent process (master), | ||
| 783 | # let master to manage the connection number of the server | ||
| 784 | |||
| 785 | my ( $heap, $type, $sid, $rip, $rport ) = @_[HEAP, ARG0, ARG1 , ARG2, ARG3]; | ||
| 786 | |||
| 787 | print $heap->{wheel_id} , " $$ $type ", time(), " $sid $rip $rport \n"; | ||
| 788 | } | ||
| 789 | |||
| 790 | sub server_shutdown | ||
| 791 | { | ||
| 792 | my ( $heap, $kernel) = @_[HEAP, KERNEL] ; | ||
| 793 | |||
| 794 | DEBUG | ||
| 795 | && print "server $$ shutdown \n"; | ||
| 796 | |||
| 797 | $heap->{shutdown} = 1; | ||
| 798 | $kernel->post( $heap->{server_alias} , 'shutdown' ); | ||
| 799 | |||
| 800 | print $heap->{wheel_id} , " $$ grace_exit ", time(), " \n"; | ||
| 801 | } | ||
| 802 | |||
| 803 | sub server_heartbeat | ||
| 804 | { | ||
| 805 | my ( $session, $heap, $kernel ) = @_[SESSION, HEAP, KERNEL]; | ||
| 806 | |||
| 807 | print $heap->{wheel_id} , " $$ heartbeat ", time(), " \n"; | ||
| 808 | if ( ! exists $heap->{shutdown} or $heap->{shutdown} != 1 ) { | ||
| 809 | $kernel->delay( 'heartbeat', $heap->{server_heartbeat_time} ); | ||
| 810 | } | ||
| 811 | # if shutdown is set, don't heartbeat, so | ||
| 812 | # the server session will be over since no event. | ||
| 813 | } | ||
| 814 | |||
| 815 | 1; | ||
| 816 | __END__ |