# HG changeset patch # User jbe # Date 1429052057 -7200 # Node ID ee98e12427a9c18494d58183cdd82ed741dbdf82 # Parent df08e63dc44be4a2839e90965c4cfc163fc01cb3 Modified termination sequence handling diff -r df08e63dc44b -r ee98e12427a9 helloworld.lua --- a/helloworld.lua Tue Apr 14 22:25:32 2015 +0200 +++ b/helloworld.lua Wed Apr 15 00:54:17 2015 +0200 @@ -6,13 +6,6 @@ local http = require "moonbridge_http" listen{ - { proto = "main" }, - connect = function() - print("Main function executed") - end -} - -listen{ { proto = "tcp", host = "127.0.0.1", port = 8080 }, -- IPv4 { proto = "tcp", host = "::1", port = 8080 }, -- IPv6 connect = http.generate_handler( diff -r df08e63dc44b -r ee98e12427a9 moonbridge.c --- a/moonbridge.c Tue Apr 14 22:25:32 2015 +0200 +++ b/moonbridge.c Wed Apr 15 00:54:17 2015 +0200 @@ -1139,10 +1139,6 @@ } } } - pool->pre_fork = 0; - pool->min_fork = 0; - pool->max_fork = 0; - timerclear(&pool->exit_delay); } moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */ } @@ -1596,68 +1592,68 @@ break; } } - /* connect listeners with idle workers */ if (!moonbr_shutdown_in_progress) { + /* connect listeners with idle workers */ while (pool->first_connected_listener && pool->first_idle_worker) { moonbr_connect(pool); } - } - /* create new worker processes */ - while ( - pool->total_worker_count < pool->max_fork && ( - pool->unassigned_worker_count < pool->pre_fork || - pool->total_worker_count < pool->min_fork - ) - ) { - if (pool->use_fork_error_wakeup) { - if (timercmp(&pool->fork_error_wakeup, &now, >)) { - moonbr_calc_wait(&wait, &pool->fork_error_wakeup); - break; + /* create new worker processes */ + while ( + pool->total_worker_count < pool->max_fork && ( + pool->unassigned_worker_count < pool->pre_fork || + pool->total_worker_count < pool->min_fork + ) + ) { + if (pool->use_fork_error_wakeup) { + if (timercmp(&pool->fork_error_wakeup, &now, >)) { + moonbr_calc_wait(&wait, &pool->fork_error_wakeup); + break; + } + } else { + if (terminated_worker_count) { + terminated_worker_count--; + } else if (timercmp(&pool->fork_wakeup, &now, >)) { + moonbr_calc_wait(&wait, &pool->fork_wakeup); + break; + } } - } else { - if (terminated_worker_count) { - terminated_worker_count--; - } else if (timercmp(&pool->fork_wakeup, &now, >)) { - moonbr_calc_wait(&wait, &pool->fork_wakeup); + if (moonbr_create_worker(pool, L)) { + /* on error, enforce error delay */ + timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); + pool->use_fork_error_wakeup = 1; + moonbr_calc_wait(&wait, &pool->fork_error_wakeup); + break; + } else { + /* normal fork delay on success */ + timeradd(&now, &pool->fork_delay, &pool->fork_wakeup); + timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); + pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */ + } + } + /* terminate excessive worker processes */ + while ( + pool->total_worker_count > pool->min_fork && + pool->idle_worker_count > pool->pre_fork + ) { + if (timerisset(&pool->exit_wakeup)) { + if (timercmp(&pool->exit_wakeup, &now, >)) { + moonbr_calc_wait(&wait, &pool->exit_wakeup); + break; + } + moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool)); + timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); + } else { + timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); break; } } - if (moonbr_create_worker(pool, L)) { - /* on error, enforce error delay */ - timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); - pool->use_fork_error_wakeup = 1; - moonbr_calc_wait(&wait, &pool->fork_error_wakeup); - break; - } else { - /* normal fork delay on success */ - timeradd(&now, &pool->fork_delay, &pool->fork_wakeup); - timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); - pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */ + if (!( + pool->total_worker_count > pool->min_fork && + pool->idle_worker_count > pool->pre_fork + )) { + timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */ } } - /* terminate excessive worker processes */ - while ( - pool->total_worker_count > pool->min_fork && - pool->idle_worker_count > pool->pre_fork - ) { - if (timerisset(&pool->exit_wakeup)) { - if (timercmp(&pool->exit_wakeup, &now, >)) { - moonbr_calc_wait(&wait, &pool->exit_wakeup); - break; - } - moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool)); - timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); - } else { - timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); - break; - } - } - if (!( - pool->total_worker_count > pool->min_fork && - pool->idle_worker_count > pool->pre_fork - )) { - timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */ - } /* optionally output worker count stats */ if (moonbr_stat && pool->worker_count_stat) { pool->worker_count_stat = 0; @@ -1679,10 +1675,15 @@ moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration); } } - /* check if shutdown is complete */ + /* terminate idle workers in case of shutdown and check if shutdown is complete */ if (moonbr_shutdown_in_progress) { for (pool=moonbr_first_pool; pool; pool=pool->next_pool) { - if (pool->first_worker) break; + if (pool->first_worker) { + while (pool->idle_worker_count) { + moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool)); + } + break; + } } if (!pool) { moonbr_log(LOG_INFO, "All worker threads have terminated");