moonbridge
diff moonbridge.c @ 130:ee98e12427a9
Modified termination sequence handling
| author | jbe |
|---|---|
| date | Wed Apr 15 00:54:17 2015 +0200 (2015-04-15) |
| parents | ebaac38b7224 |
| children | 89fc89b22e28 |
line diff
1.1 --- a/moonbridge.c Tue Apr 14 22:25:32 2015 +0200 1.2 +++ b/moonbridge.c Wed Apr 15 00:54:17 2015 +0200 1.3 @@ -1139,10 +1139,6 @@ 1.4 } 1.5 } 1.6 } 1.7 - pool->pre_fork = 0; 1.8 - pool->min_fork = 0; 1.9 - pool->max_fork = 0; 1.10 - timerclear(&pool->exit_delay); 1.11 } 1.12 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */ 1.13 } 1.14 @@ -1596,68 +1592,68 @@ 1.15 break; 1.16 } 1.17 } 1.18 - /* connect listeners with idle workers */ 1.19 if (!moonbr_shutdown_in_progress) { 1.20 + /* connect listeners with idle workers */ 1.21 while (pool->first_connected_listener && pool->first_idle_worker) { 1.22 moonbr_connect(pool); 1.23 } 1.24 - } 1.25 - /* create new worker processes */ 1.26 - while ( 1.27 - pool->total_worker_count < pool->max_fork && ( 1.28 - pool->unassigned_worker_count < pool->pre_fork || 1.29 - pool->total_worker_count < pool->min_fork 1.30 - ) 1.31 - ) { 1.32 - if (pool->use_fork_error_wakeup) { 1.33 - if (timercmp(&pool->fork_error_wakeup, &now, >)) { 1.34 - moonbr_calc_wait(&wait, &pool->fork_error_wakeup); 1.35 - break; 1.36 + /* create new worker processes */ 1.37 + while ( 1.38 + pool->total_worker_count < pool->max_fork && ( 1.39 + pool->unassigned_worker_count < pool->pre_fork || 1.40 + pool->total_worker_count < pool->min_fork 1.41 + ) 1.42 + ) { 1.43 + if (pool->use_fork_error_wakeup) { 1.44 + if (timercmp(&pool->fork_error_wakeup, &now, >)) { 1.45 + moonbr_calc_wait(&wait, &pool->fork_error_wakeup); 1.46 + break; 1.47 + } 1.48 + } else { 1.49 + if (terminated_worker_count) { 1.50 + terminated_worker_count--; 1.51 + } else if (timercmp(&pool->fork_wakeup, &now, >)) { 1.52 + moonbr_calc_wait(&wait, &pool->fork_wakeup); 1.53 + break; 1.54 + } 1.55 } 1.56 - } else { 1.57 - if (terminated_worker_count) { 1.58 - terminated_worker_count--; 1.59 - } else if (timercmp(&pool->fork_wakeup, &now, >)) { 1.60 - moonbr_calc_wait(&wait, &pool->fork_wakeup); 1.61 + if (moonbr_create_worker(pool, L)) { 1.62 + /* on error, enforce error delay */ 1.63 + timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); 1.64 + pool->use_fork_error_wakeup = 1; 1.65 + moonbr_calc_wait(&wait, &pool->fork_error_wakeup); 1.66 + break; 1.67 + } else { 1.68 + /* normal fork delay on success */ 1.69 + timeradd(&now, &pool->fork_delay, &pool->fork_wakeup); 1.70 + timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); 1.71 + pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */ 1.72 + } 1.73 + } 1.74 + /* terminate excessive worker processes */ 1.75 + while ( 1.76 + pool->total_worker_count > pool->min_fork && 1.77 + pool->idle_worker_count > pool->pre_fork 1.78 + ) { 1.79 + if (timerisset(&pool->exit_wakeup)) { 1.80 + if (timercmp(&pool->exit_wakeup, &now, >)) { 1.81 + moonbr_calc_wait(&wait, &pool->exit_wakeup); 1.82 + break; 1.83 + } 1.84 + moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool)); 1.85 + timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); 1.86 + } else { 1.87 + timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); 1.88 break; 1.89 } 1.90 } 1.91 - if (moonbr_create_worker(pool, L)) { 1.92 - /* on error, enforce error delay */ 1.93 - timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); 1.94 - pool->use_fork_error_wakeup = 1; 1.95 - moonbr_calc_wait(&wait, &pool->fork_error_wakeup); 1.96 - break; 1.97 - } else { 1.98 - /* normal fork delay on success */ 1.99 - timeradd(&now, &pool->fork_delay, &pool->fork_wakeup); 1.100 - timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup); 1.101 - pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */ 1.102 + if (!( 1.103 + pool->total_worker_count > pool->min_fork && 1.104 + pool->idle_worker_count > pool->pre_fork 1.105 + )) { 1.106 + timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */ 1.107 } 1.108 } 1.109 - /* terminate excessive worker processes */ 1.110 - while ( 1.111 - pool->total_worker_count > pool->min_fork && 1.112 - pool->idle_worker_count > pool->pre_fork 1.113 - ) { 1.114 - if (timerisset(&pool->exit_wakeup)) { 1.115 - if (timercmp(&pool->exit_wakeup, &now, >)) { 1.116 - moonbr_calc_wait(&wait, &pool->exit_wakeup); 1.117 - break; 1.118 - } 1.119 - moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool)); 1.120 - timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); 1.121 - } else { 1.122 - timeradd(&now, &pool->exit_delay, &pool->exit_wakeup); 1.123 - break; 1.124 - } 1.125 - } 1.126 - if (!( 1.127 - pool->total_worker_count > pool->min_fork && 1.128 - pool->idle_worker_count > pool->pre_fork 1.129 - )) { 1.130 - timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */ 1.131 - } 1.132 /* optionally output worker count stats */ 1.133 if (moonbr_stat && pool->worker_count_stat) { 1.134 pool->worker_count_stat = 0; 1.135 @@ -1679,10 +1675,15 @@ 1.136 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration); 1.137 } 1.138 } 1.139 - /* check if shutdown is complete */ 1.140 + /* terminate idle workers in case of shutdown and check if shutdown is complete */ 1.141 if (moonbr_shutdown_in_progress) { 1.142 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) { 1.143 - if (pool->first_worker) break; 1.144 + if (pool->first_worker) { 1.145 + while (pool->idle_worker_count) { 1.146 + moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool)); 1.147 + } 1.148 + break; 1.149 + } 1.150 } 1.151 if (!pool) { 1.152 moonbr_log(LOG_INFO, "All worker threads have terminated");