moonbridge

changeset 130:ee98e12427a9

Modified termination sequence handling
author jbe
date Wed Apr 15 00:54:17 2015 +0200 (2015-04-15)
parents df08e63dc44b
children 89fc89b22e28
files helloworld.lua moonbridge.c
line diff
     1.1 --- a/helloworld.lua	Tue Apr 14 22:25:32 2015 +0200
     1.2 +++ b/helloworld.lua	Wed Apr 15 00:54:17 2015 +0200
     1.3 @@ -6,13 +6,6 @@
     1.4  local http = require "moonbridge_http"
     1.5  
     1.6  listen{
     1.7 -  { proto = "main" },
     1.8 -  connect = function()
     1.9 -    print("Main function executed")
    1.10 -  end
    1.11 -}
    1.12 -
    1.13 -listen{
    1.14    { proto = "tcp", host = "127.0.0.1", port = 8080 },  -- IPv4
    1.15    { proto = "tcp", host = "::1",       port = 8080 },  -- IPv6
    1.16    connect = http.generate_handler(
     2.1 --- a/moonbridge.c	Tue Apr 14 22:25:32 2015 +0200
     2.2 +++ b/moonbridge.c	Wed Apr 15 00:54:17 2015 +0200
     2.3 @@ -1139,10 +1139,6 @@
     2.4          }
     2.5        }
     2.6      }
     2.7 -    pool->pre_fork = 0;
     2.8 -    pool->min_fork = 0;
     2.9 -    pool->max_fork = 0;
    2.10 -    timerclear(&pool->exit_delay);
    2.11    }
    2.12    moonbr_poll_shutdown();  /* avoids loops due to error condition when polling closed listeners */
    2.13  }
    2.14 @@ -1596,68 +1592,68 @@
    2.15            break;
    2.16          }
    2.17        }
    2.18 -      /* connect listeners with idle workers */
    2.19        if (!moonbr_shutdown_in_progress) {
    2.20 +        /* connect listeners with idle workers */
    2.21          while (pool->first_connected_listener && pool->first_idle_worker) {
    2.22            moonbr_connect(pool);
    2.23          }
    2.24 -      }
    2.25 -      /* create new worker processes */
    2.26 -      while (
    2.27 -        pool->total_worker_count < pool->max_fork && (
    2.28 -          pool->unassigned_worker_count < pool->pre_fork ||
    2.29 -          pool->total_worker_count < pool->min_fork
    2.30 -        )
    2.31 -      ) {
    2.32 -        if (pool->use_fork_error_wakeup) {
    2.33 -          if (timercmp(&pool->fork_error_wakeup, &now, >)) {
    2.34 -          moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
    2.35 -          break;
    2.36 +        /* create new worker processes */
    2.37 +        while (
    2.38 +          pool->total_worker_count < pool->max_fork && (
    2.39 +            pool->unassigned_worker_count < pool->pre_fork ||
    2.40 +            pool->total_worker_count < pool->min_fork
    2.41 +          )
    2.42 +        ) {
    2.43 +          if (pool->use_fork_error_wakeup) {
    2.44 +            if (timercmp(&pool->fork_error_wakeup, &now, >)) {
    2.45 +            moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
    2.46 +            break;
    2.47 +            }
    2.48 +          } else {
    2.49 +            if (terminated_worker_count) {
    2.50 +              terminated_worker_count--;
    2.51 +            } else if (timercmp(&pool->fork_wakeup, &now, >)) {
    2.52 +              moonbr_calc_wait(&wait, &pool->fork_wakeup);
    2.53 +              break;
    2.54 +            }
    2.55            }
    2.56 -        } else {
    2.57 -          if (terminated_worker_count) {
    2.58 -            terminated_worker_count--;
    2.59 -          } else if (timercmp(&pool->fork_wakeup, &now, >)) {
    2.60 -            moonbr_calc_wait(&wait, &pool->fork_wakeup);
    2.61 +          if (moonbr_create_worker(pool, L)) {
    2.62 +            /* on error, enforce error delay */
    2.63 +            timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
    2.64 +            pool->use_fork_error_wakeup = 1;
    2.65 +            moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
    2.66 +            break;
    2.67 +          } else {
    2.68 +            /* normal fork delay on success */
    2.69 +            timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
    2.70 +            timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
    2.71 +            pool->use_fork_error_wakeup = 0;  /* gets set later if error occures during preparation */
    2.72 +          }
    2.73 +        }
    2.74 +        /* terminate excessive worker processes */
    2.75 +        while (
    2.76 +          pool->total_worker_count > pool->min_fork &&
    2.77 +          pool->idle_worker_count > pool->pre_fork
    2.78 +        ) {
    2.79 +          if (timerisset(&pool->exit_wakeup)) {
    2.80 +            if (timercmp(&pool->exit_wakeup, &now, >)) {
    2.81 +              moonbr_calc_wait(&wait, &pool->exit_wakeup);
    2.82 +              break;
    2.83 +            }
    2.84 +            moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
    2.85 +            timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
    2.86 +          } else {
    2.87 +            timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
    2.88              break;
    2.89            }
    2.90          }
    2.91 -        if (moonbr_create_worker(pool, L)) {
    2.92 -          /* on error, enforce error delay */
    2.93 -          timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
    2.94 -          pool->use_fork_error_wakeup = 1;
    2.95 -          moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
    2.96 -          break;
    2.97 -        } else {
    2.98 -          /* normal fork delay on success */
    2.99 -          timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
   2.100 -          timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
   2.101 -          pool->use_fork_error_wakeup = 0;  /* gets set later if error occures during preparation */
   2.102 +        if (!(
   2.103 +          pool->total_worker_count > pool->min_fork &&
   2.104 +          pool->idle_worker_count > pool->pre_fork
   2.105 +        )) {
   2.106 +          timerclear(&pool->exit_wakeup);  /* timer gets restarted later when there are excessive workers */
   2.107          }
   2.108        }
   2.109 -      /* terminate excessive worker processes */
   2.110 -      while (
   2.111 -        pool->total_worker_count > pool->min_fork &&
   2.112 -        pool->idle_worker_count > pool->pre_fork
   2.113 -      ) {
   2.114 -        if (timerisset(&pool->exit_wakeup)) {
   2.115 -          if (timercmp(&pool->exit_wakeup, &now, >)) {
   2.116 -            moonbr_calc_wait(&wait, &pool->exit_wakeup);
   2.117 -            break;
   2.118 -          }
   2.119 -          moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
   2.120 -          timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
   2.121 -        } else {
   2.122 -          timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
   2.123 -          break;
   2.124 -        }
   2.125 -      }
   2.126 -      if (!(
   2.127 -        pool->total_worker_count > pool->min_fork &&
   2.128 -        pool->idle_worker_count > pool->pre_fork
   2.129 -      )) {
   2.130 -        timerclear(&pool->exit_wakeup);  /* timer gets restarted later when there are excessive workers */
   2.131 -      }
   2.132        /* optionally output worker count stats */
   2.133        if (moonbr_stat && pool->worker_count_stat) {
   2.134          pool->worker_count_stat = 0;
   2.135 @@ -1679,10 +1675,15 @@
   2.136          moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
   2.137        }
   2.138      }
   2.139 -    /* check if shutdown is complete */
   2.140 +    /* terminate idle workers in case of shutdown and check if shutdown is complete */
   2.141      if (moonbr_shutdown_in_progress) {
   2.142        for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
   2.143 -        if (pool->first_worker) break;
   2.144 +        if (pool->first_worker) {
   2.145 +          while (pool->idle_worker_count) {
   2.146 +            moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
   2.147 +          }
   2.148 +          break;
   2.149 +        }
   2.150        }
   2.151        if (!pool) {
   2.152          moonbr_log(LOG_INFO, "All worker threads have terminated");

Impressum / About Us