]> granicus.if.org Git - apache/blobdiff - modules/proxy/mod_proxy_balancer.c
Again, abstract out the worker status magic to it's handled
[apache] / modules / proxy / mod_proxy_balancer.c
index da0e4fe5146e46f15e3d90d99ad4f0a141b3e1d2..8ea81c93789bc0219c8ed609eb80afd39fe0b5b5 100644 (file)
 #include "ap_mpm.h"
 #include "apr_version.h"
 #include "apr_hooks.h"
+#include "apr_date.h"
+
+static const char *balancer_mutex_type = "proxy-balancer-shm";
+ap_slotmem_provider_t *storage = NULL;
 
 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
 
+/*
+ * Register our mutex type before the config is read so we
+ * can adjust the mutex settings using the Mutex directive.
+ */
+static int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
+                               apr_pool_t *ptemp)
+{
+
+    apr_status_t rv;
+
+    rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
+                               APR_LOCK_DEFAULT, 0);
+    if (rv != APR_SUCCESS) {
+        return rv;
+    }
+
+    return OK;
+}
+
+#if 0
+extern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
+                                 proxy_server_conf *conf);
+#endif
+
 static int proxy_balancer_canon(request_rec *r, char *url)
 {
-    char *host, *path, *search;
+    char *host, *path;
+    char *search = NULL;
     const char *err;
     apr_port_t port = 0;
 
+    /* TODO: offset of BALANCER_PREFIX ?? */
     if (strncasecmp(url, "balancer:", 9) == 0) {
         url += 9;
     }
@@ -37,7 +67,7 @@ static int proxy_balancer_canon(request_rec *r, char *url)
         return DECLINED;
     }
 
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+    ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
              "proxy: BALANCER: canonicalising URL %s", url);
 
     /* do syntatic check.
@@ -50,25 +80,23 @@ static int proxy_balancer_canon(request_rec *r, char *url)
                       url, err);
         return HTTP_BAD_REQUEST;
     }
-    /* now parse path/search args, according to rfc1738 */
-    /* N.B. if this isn't a true proxy request, then the URL _path_
-     * has already been decoded.  True proxy requests have r->uri
-     * == r->unparsed_uri, and no others have that property.
+    /*
+     * now parse path/search args, according to rfc1738:
+     * process the path. With proxy-noncanon set (by
+     * mod_proxy) we use the raw, unparsed uri
      */
-    if (r->uri == r->unparsed_uri) {
-        search = strchr(url, '?');
-        if (search != NULL)
-            *(search++) = '\0';
+    if (apr_table_get(r->notes, "proxy-nocanon")) {
+        path = url;   /* this is the raw path */
     }
-    else
+    else {
+        path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
+                                 r->proxyreq);
         search = r->args;
-
-    /* process path */
-    path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0, r->proxyreq);
+    }
     if (path == NULL)
         return HTTP_BAD_REQUEST;
 
-    r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
+    r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
 
     r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
@@ -76,38 +104,22 @@ static int proxy_balancer_canon(request_rec *r, char *url)
     return OK;
 }
 
-static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
+static void init_balancer_members(proxy_server_conf *conf, server_rec *s,
                                  proxy_balancer *balancer)
 {
     int i;
-    proxy_worker *workers;
+    proxy_worker **workers;
 
-    workers = (proxy_worker *)balancer->workers->elts;
+    workers = (proxy_worker **)balancer->workers->elts;
 
     for (i = 0; i < balancer->workers->nelts; i++) {
         int worker_is_initialized;
-        worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(workers);
-        if (!worker_is_initialized) {
-            proxy_worker_stat *slot;
-            /*
-             * If the worker is not initialized check whether its scoreboard
-             * slot is already initialized.
-             */
-            slot = (proxy_worker_stat *) ap_get_scoreboard_lb(workers->id);
-            if (slot) {
-                worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
-            }
-            else {
-                worker_is_initialized = 0;
-            }
-        }
-        ap_proxy_initialize_worker_share(conf, workers, s);
-        ap_proxy_initialize_worker(workers, s);
+        proxy_worker *worker = *workers;
+        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                     "Looking at %s -> %s initialized?", balancer->name, worker->s->name);
+        worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
         if (!worker_is_initialized) {
-            /* Set to the original configuration */
-            workers->s->lbstatus = workers->s->lbfactor =
-            (workers->lbfactor ? workers->lbfactor : 1);
-            workers->s->lbset = workers->lbset;
+            ap_proxy_initialize_worker(worker, s, conf->pool);
         }
         ++workers;
     }
@@ -115,21 +127,24 @@ static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
     /* Set default number of attempts to the number of
      * workers.
      */
-    if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
-        balancer->max_attempts = balancer->workers->nelts - 1;
-        balancer->max_attempts_set = 1;
+    if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
+        balancer->s->max_attempts = balancer->workers->nelts - 1;
+        balancer->s->max_attempts_set = 1;
     }
-    return 0;
 }
 
 /* Retrieve the parameter with the given name
  * Something like 'JSESSIONID=12345...N'
  */
 static char *get_path_param(apr_pool_t *pool, char *url,
-                            const char *name)
+                            const char *name, int scolon_sep)
 {
     char *path = NULL;
+    char *pathdelims = "?&";
 
+    if (scolon_sep) {
+        pathdelims = ";?&";
+    }
     for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
         path += strlen(name);
         if (*path == '=') {
@@ -137,9 +152,9 @@ static char *get_path_param(apr_pool_t *pool, char *url,
              * Session path was found, get it's value
              */
             ++path;
-            if (strlen(path)) {
+            if (*path) {
                 char *q;
-                path = apr_strtok(apr_pstrdup(pool, path), "?&", &q);
+                path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
                 return path;
             }
         }
@@ -163,12 +178,11 @@ static char *get_cookie_param(request_rec *r, const char *name)
                 start_cookie += strlen(name);
                 while(*start_cookie && isspace(*start_cookie))
                     ++start_cookie;
-                if (*start_cookie == '=' && start_cookie[1]) {
+                if (*start_cookie++ == '=' && *start_cookie) {
                     /*
                      * Session cookie was found, get it's value
                      */
                     char *end_cookie, *cookie;
-                    ++start_cookie;
                     cookie = apr_pstrdup(r->pool, start_cookie);
                     if ((end_cookie = strchr(cookie, ';')) != NULL)
                         *end_cookie = '\0';
@@ -190,13 +204,14 @@ static proxy_worker *find_route_worker(proxy_balancer *balancer,
     int i;
     int checking_standby;
     int checked_standby;
-    
-    proxy_worker *worker;
+
+    proxy_worker **workers;
 
     checking_standby = checked_standby = 0;
     while (!checked_standby) {
-        worker = (proxy_worker *)balancer->workers->elts;
-        for (i = 0; i < balancer->workers->nelts; i++, worker++) {
+        workers = (proxy_worker **)balancer->workers->elts;
+        for (i = 0; i < balancer->workers->nelts; i++, workers++) {
+            proxy_worker *worker = *workers;
             if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
                 continue;
             if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
@@ -256,23 +271,23 @@ static proxy_worker *find_session_route(proxy_balancer *balancer,
 {
     proxy_worker *worker = NULL;
 
-    if (!balancer->sticky)
+    if (!*balancer->s->sticky)
         return NULL;
     /* Try to find the sticky route inside url */
-    *route = get_path_param(r->pool, *url, balancer->sticky_path);
+    *route = get_path_param(r->pool, *url, balancer->s->sticky_path, balancer->s->scolonsep);
     if (*route) {
         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
                      "proxy: BALANCER: Found value %s for "
-                     "stickysession %s", *route, balancer->sticky_path);
-        *sticky_used =  balancer->sticky_path;
+                     "stickysession %s", *route, balancer->s->sticky_path);
+        *sticky_used =  balancer->s->sticky_path;
     }
     else {
-        *route = get_cookie_param(r, balancer->sticky);
+        *route = get_cookie_param(r, balancer->s->sticky);
         if (*route) {
-            *sticky_used =  balancer->sticky;
+            *sticky_used =  balancer->s->sticky;
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
                          "proxy: BALANCER: Found value %s for "
-                         "stickysession %s", *route, balancer->sticky);
+                         "stickysession %s", *route, balancer->s->sticky);
         }
     }
     /*
@@ -310,23 +325,23 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
     proxy_worker *candidate = NULL;
     apr_status_t rv;
 
-    if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
+    if ((rv = PROXY_GLOBAL_LOCK(balancer)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
         "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
         return NULL;
     }
 
-    candidate = (*balancer->lbmethod->finder)(balancer, r);
+    candidate = (*balancer->s->lbmethod->finder)(balancer, r);
 
     if (candidate)
         candidate->s->elected++;
 
 /*
-        PROXY_THREAD_UNLOCK(balancer);
+        PROXY_GLOBAL_UNLOCK(conf);
         return NULL;
 */
 
-    if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
+    if ((rv = PROXY_GLOBAL_UNLOCK(balancer)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
         "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
     }
@@ -339,19 +354,18 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
          * By default the timeout is not set, and the server
          * returns SERVER_BUSY.
          */
-#if APR_HAS_THREADS
-        if (balancer->timeout) {
+        if (balancer->s->timeout) {
             /* XXX: This can perhaps be build using some
              * smarter mechanism, like tread_cond.
              * But since the statuses can came from
              * different childs, use the provided algo.
              */
-            apr_interval_time_t timeout = balancer->timeout;
+            apr_interval_time_t timeout = balancer->s->timeout;
             apr_interval_time_t step, tval = 0;
             /* Set the timeout to 0 so that we don't
              * end in infinite loop
              */
-            balancer->timeout = 0;
+            balancer->s->timeout = 0;
             step = timeout / 100;
             while (tval < timeout) {
                 apr_sleep(step);
@@ -361,11 +375,12 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
                 tval += step;
             }
             /* restore the timeout */
-            balancer->timeout = timeout;
+            balancer->s->timeout = timeout;
         }
-#endif
     }
+
     return candidate;
+
 }
 
 static int rewrite_url(request_rec *r, proxy_worker *worker,
@@ -384,7 +399,7 @@ static int rewrite_url(request_rec *r, proxy_worker *worker,
                              NULL));
     }
 
-    *url = apr_pstrcat(r->pool, worker->name, path, NULL);
+    *url = apr_pstrcat(r->pool, worker->s->name, path, NULL);
 
     return OK;
 }
@@ -393,25 +408,33 @@ static void force_recovery(proxy_balancer *balancer, server_rec *s)
 {
     int i;
     int ok = 0;
-    proxy_worker *worker;
+    proxy_worker **worker;
 
-    worker = (proxy_worker *)balancer->workers->elts;
+    worker = (proxy_worker **)balancer->workers->elts;
     for (i = 0; i < balancer->workers->nelts; i++, worker++) {
-        if (!(worker->s->status & PROXY_WORKER_IN_ERROR)) {
+        if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
             ok = 1;
-            break;    
+            break;
+        }
+        else {
+            /* Try if we can recover */
+            ap_proxy_retry_worker("BALANCER", *worker, s);
+            if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
+                ok = 1;
+                break;
+            }
         }
     }
     if (!ok) {
         /* If all workers are in error state force the recovery.
          */
-        worker = (proxy_worker *)balancer->workers->elts;
+        worker = (proxy_worker **)balancer->workers->elts;
         for (i = 0; i < balancer->workers->nelts; i++, worker++) {
-            ++worker->s->retries;
-            worker->s->status &= ~PROXY_WORKER_IN_ERROR;
+            ++(*worker)->s->retries;
+            (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                          "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
-                         balancer->name, worker->hostname);
+                         balancer->name, (*worker)->s->hostname);
         }
     }
 }
@@ -440,7 +463,7 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
     /* Step 2: Lock the LoadBalancer
      * XXX: perhaps we need the process lock here
      */
-    if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
+    if ((rv = PROXY_GLOBAL_LOCK(*balancer)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
                      "proxy: BALANCER: (%s). Lock failed for pre_request",
                      (*balancer)->name);
@@ -450,46 +473,53 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
     /* Step 3: force recovery */
     force_recovery(*balancer, r->server);
 
+    /* Step 3.5: Update member list for the balancer */
+    /* TODO: Implement as provider! */
+    /* proxy_update_members(balancer, r, conf); */
+
     /* Step 4: find the session route */
     runtime = find_session_route(*balancer, r, &route, &sticky, url);
     if (runtime) {
-        int i, total_factor = 0;
-        proxy_worker *workers;
-        /* We have a sticky load balancer
-         * Update the workers status
-         * so that even session routes get
-         * into account.
-         */
-        workers = (proxy_worker *)(*balancer)->workers->elts;
-        for (i = 0; i < (*balancer)->workers->nelts; i++) {
-            /* Take into calculation only the workers that are
-             * not in error state or not disabled.
-             *
-             * TODO: Abstract the below, since this is dependent
-             *       on the LB implementation
+        if ((*balancer)->s->lbmethod && (*balancer)->s->lbmethod->updatelbstatus) {
+            /* Call the LB implementation */
+            (*balancer)->s->lbmethod->updatelbstatus(*balancer, runtime, r->server);
+        }
+        else { /* Use the default one */
+            int i, total_factor = 0;
+            proxy_worker **workers;
+            /* We have a sticky load balancer
+             * Update the workers status
+             * so that even session routes get
+             * into account.
              */
-            if (PROXY_WORKER_IS_USABLE(workers)) {
-                workers->s->lbstatus += workers->s->lbfactor;
-                total_factor += workers->s->lbfactor;
+            workers = (proxy_worker **)(*balancer)->workers->elts;
+            for (i = 0; i < (*balancer)->workers->nelts; i++) {
+                /* Take into calculation only the workers that are
+                 * not in error state or not disabled.
+                 */
+                if (PROXY_WORKER_IS_USABLE(*workers)) {
+                    (*workers)->s->lbstatus += (*workers)->s->lbfactor;
+                    total_factor += (*workers)->s->lbfactor;
+                }
+                workers++;
             }
-            workers++;
+            runtime->s->lbstatus -= total_factor;
         }
-        runtime->s->lbstatus -= total_factor;
         runtime->s->elected++;
 
         *worker = runtime;
     }
-    else if (route && (*balancer)->sticky_force) {
+    else if (route && (*balancer)->s->sticky_force) {
         int i, member_of = 0;
-        proxy_worker *workers;
+        proxy_worker **workers;
         /*
          * We have a route provided that doesn't match the
          * balancer name. See if the provider route is the
          * member of the same balancer in which case return 503
          */
-        workers = (proxy_worker *)(*balancer)->workers->elts;
+        workers = (proxy_worker **)(*balancer)->workers->elts;
         for (i = 0; i < (*balancer)->workers->nelts; i++) {
-            if (*(workers->s->route) && strcmp(workers->s->route, route) == 0) {
+            if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
                 member_of = 1;
                 break;
             }
@@ -499,7 +529,7 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
                          "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
                          (*balancer)->name, route);
-            if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
+            if ((rv = PROXY_GLOBAL_UNLOCK(*balancer)) != APR_SUCCESS) {
                 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
                              "proxy: BALANCER: (%s). Unlock failed for pre_request",
                              (*balancer)->name);
@@ -508,7 +538,7 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
         }
     }
 
-    if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
+    if ((rv = PROXY_GLOBAL_UNLOCK(*balancer)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
                      "proxy: BALANCER: (%s). Unlock failed for pre_request",
                      (*balancer)->name);
@@ -516,13 +546,19 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
     if (!*worker) {
         runtime = find_best_worker(*balancer, r);
         if (!runtime) {
-            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
-                         "proxy: BALANCER: (%s). All workers are in error state",
-                         (*balancer)->name);
+            if ((*balancer)->workers->nelts) {
+                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+                            "proxy: BALANCER: (%s). All workers are in error state",
+                            (*balancer)->name);
+            } else {
+                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+                            "proxy: BALANCER: (%s). No workers in balancer",
+                            (*balancer)->name);
+            }
 
             return HTTP_SERVICE_UNAVAILABLE;
         }
-        if ((*balancer)->sticky && runtime) {
+        if (*(*balancer)->s->sticky && runtime) {
             /*
              * This balancer has sticky sessions and the client either has not
              * supplied any routing information or all workers for this route
@@ -536,11 +572,13 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
         *worker = runtime;
     }
 
+    (*worker)->s->busy++;
+
     /* Add balancer/worker info to env. */
     apr_table_setn(r->subprocess_env,
                    "BALANCER_NAME", (*balancer)->name);
     apr_table_setn(r->subprocess_env,
-                   "BALANCER_WORKER_NAME", (*worker)->name);
+                   "BALANCER_WORKER_NAME", (*worker)->s->name);
     apr_table_setn(r->subprocess_env,
                    "BALANCER_WORKER_ROUTE", (*worker)->s->route);
 
@@ -563,7 +601,7 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
     }
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
                  "proxy: BALANCER (%s) worker (%s) rewritten to %s",
-                 (*balancer)->name, (*worker)->name, *url);
+                 (*balancer)->name, (*worker)->s->name, *url);
 
     return access_status;
 }
@@ -574,19 +612,31 @@ static int proxy_balancer_post_request(proxy_worker *worker,
                                        proxy_server_conf *conf)
 {
 
-#if 0
     apr_status_t rv;
 
-    if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
+    if ((rv = PROXY_GLOBAL_LOCK(balancer)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
             "proxy: BALANCER: (%s). Lock failed for post_request",
             balancer->name);
         return HTTP_INTERNAL_SERVER_ERROR;
     }
-    /* TODO: placeholder for post_request actions
-     */
 
-    if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
+    if (!apr_is_empty_array(balancer->errstatuses)) {
+        int i;
+        for (i = 0; i < balancer->errstatuses->nelts; i++) {
+            int val = ((int *)balancer->errstatuses->elts)[i];
+            if (r->status == val) {
+                ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
+                             "proxy: BALANCER: (%s).  Forcing recovery for worker (%s), failonstatus %d",
+                             balancer->name, worker->s->name, val);
+                worker->s->status |= PROXY_WORKER_IN_ERROR;
+                worker->s->error_time = apr_time_now();
+                break;
+            }
+        }
+    }
+
+    if ((rv = PROXY_GLOBAL_UNLOCK(balancer)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
             "proxy: BALANCER: (%s). Unlock failed for post_request",
             balancer->name);
@@ -594,52 +644,220 @@ static int proxy_balancer_post_request(proxy_worker *worker,
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
                  "proxy_balancer_post_request for (%s)", balancer->name);
 
-#endif
+    if (worker && worker->s->busy)
+        worker->s->busy--;
 
     return OK;
+
 }
 
 static void recalc_factors(proxy_balancer *balancer)
 {
     int i;
-    proxy_worker *workers;
+    proxy_worker **workers;
 
 
     /* Recalculate lbfactors */
-    workers = (proxy_worker *)balancer->workers->elts;
+    workers = (proxy_worker **)balancer->workers->elts;
     /* Special case if there is only one worker it's
      * load factor will always be 1
      */
     if (balancer->workers->nelts == 1) {
-        workers->s->lbstatus = workers->s->lbfactor = 1;
+        (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
         return;
     }
     for (i = 0; i < balancer->workers->nelts; i++) {
         /* Update the status entries */
-        workers[i].s->lbstatus = workers[i].s->lbfactor;
+        workers[i]->s->lbstatus = workers[i]->s->lbfactor;
     }
 }
 
+static apr_status_t lock_remove(void *data)
+{
+    int i;
+    proxy_balancer *balancer;
+    server_rec *s = data;
+    void *sconf = s->module_config;
+    proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
+
+    balancer = (proxy_balancer *)conf->balancers->elts;
+    for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
+        if (balancer->mutex) {
+            apr_global_mutex_destroy(balancer->mutex);
+            balancer->mutex = NULL;
+        }
+    }
+    return(0);
+}
+
+/* post_config hook: */
+static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
+                         apr_pool_t *ptemp, server_rec *s)
+{
+    apr_status_t rv;
+    void *data;
+    void *sconf = s->module_config;
+    proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
+    const char *userdata_key = "mod_proxy_balancer_init";
+    ap_slotmem_instance_t *new = NULL;
+
+    /* balancer_post_config() will be called twice during startup.  So, only
+     * set up the static data the 1st time through. */
+    apr_pool_userdata_get(&data, userdata_key, s->process->pool);
+    if (!data) {
+        apr_pool_userdata_set((const void *)1, userdata_key,
+                               apr_pool_cleanup_null, s->process->pool);
+        return OK;
+    }
+
+    /*
+     * Get slotmem setups
+     */
+    storage = ap_lookup_provider(AP_SLOTMEM_PROVIDER_GROUP, "shared", "0");
+    if (!storage) {
+        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s,
+                     "ap_lookup_provider %s failed", AP_SLOTMEM_PROVIDER_GROUP);
+        return !OK;
+    }
+
+    
+    /*
+     * Go thru each Vhost and create the shared mem slotmem for
+     * each balancer's workers
+     */
+    while (s) {
+        int i,j;
+        proxy_balancer *balancer;
+        sconf = s->module_config;
+        conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
+
+        if (conf->balancers->nelts) {
+            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Doing balancers create: %d, %d",
+                         (int)ALIGNED_PROXY_BALANCER_SHARED_SIZE,
+                         (int)conf->balancers->nelts);
+            
+            rv = storage->create(&new, conf->id,
+                                 ALIGNED_PROXY_BALANCER_SHARED_SIZE,
+                                 conf->balancers->nelts, AP_SLOTMEM_TYPE_PREGRAB, pconf);
+            if (rv != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "balancer slotmem_create failed");
+                return !OK;
+            }
+            conf->slot = new;
+        }
+
+        /* Initialize shared scoreboard data */
+        balancer = (proxy_balancer *)conf->balancers->elts;
+        for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
+            proxy_worker **workers;
+            proxy_worker *worker;
+            proxy_balancer_shared *bshm;
+            unsigned int index;
+
+            balancer->max_workers = balancer->workers->nelts + balancer->growth;
+            /* no need for the 'balancer://' prefix */
+            ap_pstr2_alnum(pconf, balancer->name + sizeof(BALANCER_PREFIX) - 1, 
+                           &balancer->sname);
+            balancer->sname = apr_pstrcat(pconf, conf->id, "_", balancer->sname, NULL);
+
+            /* Create global mutex */
+            rv = ap_global_mutex_create(&(balancer->mutex), NULL, balancer_mutex_type,
+                                        balancer->sname, s, pconf, 0);
+            if (rv != APR_SUCCESS || !balancer->mutex) {
+                ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
+                             "mutex creation of %s : %s failed", balancer_mutex_type,
+                             balancer->sname);
+                return HTTP_INTERNAL_SERVER_ERROR;
+            }
+
+            apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
+                                      apr_pool_cleanup_null);
+
+            /* setup shm for balancers */
+            if ((rv = storage->grab(conf->slot, &index)) != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "balancer slotmem_grab failed");
+                return !OK;
+                
+            }
+            if ((rv = storage->dptr(conf->slot, index, (void *)&bshm)) != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "balancer slotmem_dptr failed");
+                return !OK;
+            }
+            if ((rv = ap_proxy_share_balancer(balancer, bshm, index)) != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "Cannot share balancer");
+                return !OK;
+            }
+            
+            /* create slotmem slots for workers */
+            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Doing workers create: %s (%s), %d, %d",
+                         balancer->name, balancer->sname,
+                         (int)ALIGNED_PROXY_WORKER_SHARED_SIZE,
+                         (int)balancer->max_workers);
+
+            rv = storage->create(&new, balancer->sname,
+                                 ALIGNED_PROXY_WORKER_SHARED_SIZE,
+                                 balancer->max_workers, AP_SLOTMEM_TYPE_PREGRAB, pconf);
+            if (rv != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_create failed");
+                return !OK;
+            }
+            balancer->slot = new;
+
+            /* now go thru each worker */
+            workers = (proxy_worker **)balancer->workers->elts;
+            for (j = 0; j < balancer->workers->nelts; j++, workers++) {
+                proxy_worker_shared *shm;
+
+                worker = *workers;
+                if ((rv = storage->grab(balancer->slot, &index)) != APR_SUCCESS) {
+                    ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_grab failed");
+                    return !OK;
+
+                }
+                if ((rv = storage->dptr(balancer->slot, index, (void *)&shm)) != APR_SUCCESS) {
+                    ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_dptr failed");
+                    return !OK;
+                }
+                if ((rv = ap_proxy_share_worker(worker, shm, index)) != APR_SUCCESS) {
+                    ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "Cannot share worker");
+                    return !OK;
+                }
+            }
+        }
+        s = s->next;
+    }
+
+    return OK;
+}
+
 /* Manages the loadfactors and member status
  */
 static int balancer_handler(request_rec *r)
 {
-    void *sconf = r->server->module_config;
-    proxy_server_conf *conf = (proxy_server_conf *)
-        ap_get_module_config(sconf, &proxy_module);
+    void *sconf;
+    proxy_server_conf *conf;
     proxy_balancer *balancer, *bsel = NULL;
     proxy_worker *worker, *wsel = NULL;
-    apr_table_t *params = apr_table_make(r->pool, 10);
+    proxy_worker **workers = NULL;
+    apr_table_t *params;
     int access_status;
     int i, n;
+    int ok2change = 1;
     const char *name;
 
     /* is this for us? */
-    if (strcmp(r->handler, "balancer-manager"))
+    if (strcmp(r->handler, "balancer-manager")) {
         return DECLINED;
+    }
+
     r->allowed = (AP_METHOD_BIT << M_GET);
-    if (r->method_number != M_GET)
+    if (r->method_number != M_GET) {
         return DECLINED;
+    }
+
+    sconf = r->server->module_config;
+    conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
+    params = apr_table_make(r->pool, 10);
 
     if (r->args) {
         char *args = apr_pstrdup(r->pool, r->args);
@@ -662,30 +880,32 @@ static int balancer_handler(request_rec *r)
                 return HTTP_BAD_REQUEST;
         }
     }
+
     if ((name = apr_table_get(params, "b")))
         bsel = ap_proxy_get_balancer(r->pool, conf,
-            apr_pstrcat(r->pool, "balancer://", name, NULL));
+            apr_pstrcat(r->pool, BALANCER_PREFIX, name, NULL));
+
     if ((name = apr_table_get(params, "w"))) {
-        proxy_worker *ws;
-
-        ws = ap_proxy_get_worker(r->pool, conf, name);
-        if (bsel && ws) {
-            worker = (proxy_worker *)bsel->workers->elts;
-            for (n = 0; n < bsel->workers->nelts; n++) {
-                if (strcasecmp(worker->name, ws->name) == 0) {
-                    wsel = worker;
-                    break;
-                }
-                ++worker;
-            }
-        }
+        wsel = ap_proxy_get_worker(r->pool, bsel, conf, name);
     }
+
+
+    /* Check that the supplied nonce matches this server's nonce;
+     * otherwise ignore all parameters, to prevent a CSRF attack. */
+    if (!bsel ||
+        (*bsel->s->nonce &&
+         (
+          (name = apr_table_get(params, "nonce")) == NULL ||
+          strcmp(bsel->s->nonce, name) != 0
+         )
+        )
+       ) {
+        apr_table_clear(params);
+        ok2change = 0;
+    }
+
     /* First set the params */
-    /*
-     * Note that it is not possible set the proxy_balancer because it is not
-     * in shared memory.
-     */
-    if (wsel) {
+    if (wsel && ok2change) {
         const char *val;
         if ((val = apr_table_get(params, "lf"))) {
             int ival = atoi(val);
@@ -696,13 +916,13 @@ static int balancer_handler(request_rec *r)
             }
         }
         if ((val = apr_table_get(params, "wr"))) {
-            if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
+            if (strlen(val) && strlen(val) < sizeof(wsel->s->route))
                 strcpy(wsel->s->route, val);
             else
                 *wsel->s->route = '\0';
         }
         if ((val = apr_table_get(params, "rr"))) {
-            if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
+            if (strlen(val) && strlen(val) < sizeof(wsel->s->redirect))
                 strcpy(wsel->s->redirect, val);
             else
                 *wsel->s->redirect = '\0';
@@ -721,6 +941,17 @@ static int balancer_handler(request_rec *r)
         }
 
     }
+
+    if (bsel && ok2change) {
+        const char *val;
+        if ((val = apr_table_get(params, "lbm"))) {
+            proxy_balancer_method *lbmethod;
+            lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
+            if (lbmethod)
+                bsel->s->lbmethod = lbmethod;
+        }
+    }
+
     if (apr_table_get(params, "xml")) {
         ap_set_content_type(r, "text/xml");
         ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
@@ -731,17 +962,18 @@ static int balancer_handler(request_rec *r)
             ap_rputs("    <httpd:balancer>\n", r);
             ap_rvputs(r, "      <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
             ap_rputs("      <httpd:workers>\n", r);
-            worker = (proxy_worker *)balancer->workers->elts;
+            workers = (proxy_worker **)balancer->workers->elts;
             for (n = 0; n < balancer->workers->nelts; n++) {
+                worker = *workers;
                 ap_rputs("        <httpd:worker>\n", r);
-                ap_rvputs(r, "          <httpd:scheme>", worker->scheme,
+                ap_rvputs(r, "          <httpd:scheme>", worker->s->scheme,
                           "</httpd:scheme>\n", NULL);
-                ap_rvputs(r, "          <httpd:hostname>", worker->hostname,
+                ap_rvputs(r, "          <httpd:hostname>", worker->s->hostname,
                           "</httpd:hostname>\n", NULL);
-               ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
+                ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
                           worker->s->lbfactor);
                 ap_rputs("        </httpd:worker>\n", r);
-                ++worker;
+                ++workers;
             }
             ap_rputs("      </httpd:workers>\n", r);
             ap_rputs("    </httpd:balancer>\n", r);
@@ -764,27 +996,32 @@ static int balancer_handler(request_rec *r)
         for (i = 0; i < conf->balancers->nelts; i++) {
 
             ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
-            ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
+            ap_rvputs(r, "<a href=\"", r->uri, "?b=",
+                      balancer->name + sizeof(BALANCER_PREFIX) - 1,
+                      "&nonce=", balancer->s->nonce,
+                      "\">", NULL);
+            ap_rvputs(r, balancer->name, "</a></h3>\n\n", NULL);
             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
-                "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
+                "<th>MaxMembers</th><th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
                 "</tr>\n<tr>", r);
-            if (balancer->sticky) {
-                if (strcmp(balancer->sticky, balancer->sticky_path)) {
-                    ap_rvputs(r, "<td>", balancer->sticky, " | ",
-                              balancer->sticky_path, NULL);
+            ap_rprintf(r, "<td align=\"center\">%d</td>\n", balancer->max_workers);
+            if (*balancer->s->sticky) {
+                if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
+                    ap_rvputs(r, "<td align=\"center\">", balancer->s->sticky, " | ",
+                              balancer->s->sticky_path, NULL);
                 }
                 else {
-                    ap_rvputs(r, "<td>", balancer->sticky, NULL);
+                    ap_rvputs(r, "<td align=\"center\">", balancer->s->sticky, NULL);
                 }
             }
             else {
-                ap_rputs("<td> - ", r);
+                ap_rputs("<td align=\"center\"> - ", r);
             }
-            ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
-                apr_time_sec(balancer->timeout));
-            ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
-            ap_rprintf(r, "<td>%s</td>\n",
-                       balancer->lbmethod->name);
+            ap_rprintf(r, "</td><td align=\"center\">%" APR_TIME_T_FMT "</td>",
+                apr_time_sec(balancer->s->timeout));
+            ap_rprintf(r, "<td align=\"center\">%d</td>\n", balancer->s->max_attempts);
+            ap_rprintf(r, "<td align=\"center\">%s</td>\n",
+                       balancer->s->lbmethod->name);
             ap_rputs("</table>\n<br />", r);
             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
                 "<th>Worker URL</th>"
@@ -793,40 +1030,31 @@ static int balancer_handler(request_rec *r)
                 "<th>Elected</th><th>To</th><th>From</th>"
                 "</tr>\n", r);
 
-            worker = (proxy_worker *)balancer->workers->elts;
+            workers = (proxy_worker **)balancer->workers->elts;
             for (n = 0; n < balancer->workers->nelts; n++) {
                 char fbuf[50];
+                worker = *workers;
                 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
-                          balancer->name + sizeof("balancer://") - 1, "&w=",
-                          ap_escape_uri(r->pool, worker->name),
+                          balancer->name + sizeof(BALANCER_PREFIX) - 1, "&w=",
+                          ap_escape_uri(r->pool, worker->s->name),
+                          "&nonce=", balancer->s->nonce,
                           "\">", NULL);
-                ap_rvputs(r, worker->name, "</a></td>", NULL);
-                ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
+                ap_rvputs(r, worker->s->name, "</a></td>", NULL);
+                ap_rvputs(r, "<td align=\"center\">", ap_escape_html(r->pool, worker->s->route),
                           NULL);
-                ap_rvputs(r, "</td><td>",
+                ap_rvputs(r, "</td><td align=\"center\">",
                           ap_escape_html(r->pool, worker->s->redirect), NULL);
-                ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
-                ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
-                if (worker->s->status & PROXY_WORKER_DISABLED)
-                   ap_rputs("Dis ", r);
-                if (worker->s->status & PROXY_WORKER_IN_ERROR)
-                   ap_rputs("Err ", r);
-                if (worker->s->status & PROXY_WORKER_STOPPED)
-                   ap_rputs("Stop ", r);
-                if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
-                   ap_rputs("Stby ", r);
-                if (PROXY_WORKER_IS_USABLE(worker))
-                    ap_rputs("Ok", r);
-                if (!PROXY_WORKER_IS_INITIALIZED(worker))
-                    ap_rputs("-", r);
+                ap_rprintf(r, "</td><td align=\"center\">%d</td>", worker->s->lbfactor);
+                ap_rprintf(r, "<td align=\"center\">%d</td><td align=\"center\">", worker->s->lbset);
+                ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, worker), NULL);
                 ap_rputs("</td>", r);
-                ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
+                ap_rprintf(r, "<td align=\"center\">%" APR_SIZE_T_FMT "</td><td align=\"center\">", worker->s->elected);
                 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
-                ap_rputs("</td><td>", r);
+                ap_rputs("</td><td align=\"center\">", r);
                 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
                 ap_rputs("</td></tr>\n", r);
 
-                ++worker;
+                ++workers;
             }
             ap_rputs("</table>\n", r);
             ++balancer;
@@ -834,7 +1062,7 @@ static int balancer_handler(request_rec *r)
         ap_rputs("<hr />\n", r);
         if (wsel && bsel) {
             ap_rputs("<h3>Edit worker settings for ", r);
-            ap_rvputs(r, wsel->name, "</h3>\n", NULL);
+            ap_rvputs(r, wsel->s->name, "</h3>\n", NULL);
             ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
             ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
             ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
@@ -858,258 +1086,129 @@ static int balancer_handler(request_rec *r)
             ap_rputs("></td></tr>\n", r);
             ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
             ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ",  NULL);
-            ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
+            ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->s->name), "\">\n", NULL);
             ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
-            ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
-                      "\">\n</form>\n", NULL);
+            ap_rvputs(r, "value=\"", bsel->name + sizeof(BALANCER_PREFIX) - 1,
+                      "\">\n", NULL);
+            ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"",
+                      bsel->s->nonce, "\">\n", NULL);
+            ap_rvputs(r, "</form>\n", NULL);
+            ap_rputs("<hr />\n", r);
+        } else if (bsel) {
+            const apr_array_header_t *provs;
+            const ap_list_provider_names_t *pname;
+            int i;
+            ap_rputs("<h3>Edit balancer settings for ", r);
+            ap_rvputs(r, bsel->name, "</h3>\n", NULL);
+            ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
+            ap_rvputs(r, r->uri, "\">\n<dl>\n<table>\n", NULL);
+            provs = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
+            if (provs) {
+                ap_rputs("<tr><td>LBmethod:</td>", r);
+                ap_rputs("<td>\n<select name=\"lbm\" id=\"lbm\">", r);
+                pname = (ap_list_provider_names_t *)provs->elts;
+                for (i = 0; i < provs->nelts; i++, pname++) {
+                    ap_rvputs(r,"<option value=\"", pname->provider_name, "\"", NULL);
+                    if (strcmp(pname->provider_name, bsel->s->lbmethod->name) == 0)
+                        ap_rputs(" selected ", r);
+                    ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
+                }
+                ap_rputs("</select>\n</td></tr>\n", r);
+            }
+            ap_rputs("</td></tr>\n", r);
+            ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
+            ap_rvputs(r, "</table>\n<input type=hidden name=\"b\" ", NULL);
+            ap_rvputs(r, "value=\"", bsel->name + sizeof(BALANCER_PREFIX) - 1,
+                      "\">\n", NULL);
+            ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"",
+                      bsel->s->nonce, "\">\n", NULL);
+            ap_rvputs(r, "</form>\n", NULL);
             ap_rputs("<hr />\n", r);
         }
         ap_rputs(ap_psignature("",r), r);
         ap_rputs("</body></html>\n", r);
-    }
+}
     return OK;
 }
 
-static void child_init(apr_pool_t *p, server_rec *s)
+static void balancer_child_init(apr_pool_t *p, server_rec *s)
 {
     while (s) {
-        void *sconf = s->module_config;
-        proxy_server_conf *conf;
         proxy_balancer *balancer;
         int i;
-        conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
-
-        /* Initialize shared scoreboard data */
+        void *sconf = s->module_config;
+        proxy_server_conf *conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
+        apr_size_t size;
+        unsigned int num;
+        apr_status_t rv;
+
+        if (conf->balancers->nelts) {
+            storage->attach(&(conf->slot), conf->id, &size, &num, p);
+            if (!conf->slot) {
+                ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s, "slotmem_attach failed");
+                exit(1); /* Ugly, but what else? */
+            }
+        }
+        
         balancer = (proxy_balancer *)conf->balancers->elts;
         for (i = 0; i < conf->balancers->nelts; i++) {
-            init_balancer_members(conf, s, balancer);
-            balancer++;
-        }
-        s = s->next;
-    }
-
-}
-
-/*
- * The idea behind the find_best_byrequests scheduler is the following:
- *
- * lbfactor is "how much we expect this worker to work", or "the worker's
- * normalized work quota".
- *
- * lbstatus is "how urgent this worker has to work to fulfill its quota
- * of work".
- *
- * We distribute each worker's work quota to the worker, and then look
- * which of them needs to work most urgently (biggest lbstatus).  This
- * worker is then selected for work, and its lbstatus reduced by the
- * total work quota we distributed to all workers.  Thus the sum of all
- * lbstatus does not change.(*)
- *
- * If some workers are disabled, the others will
- * still be scheduled correctly.
- *
- * If a balancer is configured as follows:
- *
- * worker     a    b    c    d
- * lbfactor  25   25   25   25
- *
- * And b gets disabled, the following schedule is produced:
- *
- *    a c d a c d a c d ...
- *
- * Note that the above lbfactor setting is the *exact* same as:
- *
- * worker     a    b    c    d
- * lbfactor   1    1    1    1
- *
- * Asymmetric configurations work as one would expect. For
- * example:
- *
- * worker     a    b    c    d
- * lbfactor   1    1    1    2
- *
- * would have a, b and c all handling about the same
- * amount of load with d handling twice what a or b
- * or c handles individually. So we could see:
- *
- *   b a d c d a c d b d ...
- *
- */
 
-static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
-                                request_rec *r)
-{
-    int i;
-    int total_factor = 0;
-    proxy_worker *worker;
-    proxy_worker *mycandidate = NULL;
-    int cur_lbset = 0;
-    int max_lbset = 0;
-    int checking_standby;
-    int checked_standby;
-    
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
-                 "proxy: Entering byrequests for BALANCER (%s)",
-                 balancer->name);
-
-    /* First try to see if we have available candidate */
-    do {
-        checking_standby = checked_standby = 0;
-        while (!mycandidate && !checked_standby) {
-            worker = (proxy_worker *)balancer->workers->elts;
-            for (i = 0; i < balancer->workers->nelts; i++, worker++) {
-                if (!checking_standby) {    /* first time through */
-                    if (worker->s->lbset > max_lbset)
-                        max_lbset = worker->s->lbset;
-                }
-                if (worker->s->lbset > cur_lbset)
-                    continue;
-                if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
-                    continue;
-                /* If the worker is in error state run
-                 * retry on that worker. It will be marked as
-                 * operational if the retry timeout is elapsed.
-                 * The worker might still be unusable, but we try
-                 * anyway.
-                 */
-                if (!PROXY_WORKER_IS_USABLE(worker))
-                    ap_proxy_retry_worker("BALANCER", worker, r->server);
-                /* Take into calculation only the workers that are
-                 * not in error state or not disabled.
-                 */
-                if (PROXY_WORKER_IS_USABLE(worker)) {
-                    worker->s->lbstatus += worker->s->lbfactor;
-                    total_factor += worker->s->lbfactor;
-                    if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
-                        mycandidate = worker;
-                }
+            /*
+             * for each balancer we need to init the global
+             * mutex and then attach to the shared worker shm
+             */
+            if (!balancer->mutex) {
+                ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                             "no mutex %s: %s", balancer->name,
+                             balancer_mutex_type);
+                return;
             }
-            checked_standby = checking_standby++;
-        }
-        cur_lbset++;
-    } while (cur_lbset <= max_lbset && !mycandidate);
-
-    if (mycandidate) {
-        mycandidate->s->lbstatus -= total_factor;
-    }
-
-    return mycandidate;
-}
 
-/*
- * The idea behind the find_best_bytraffic scheduler is the following:
- *
- * We know the amount of traffic (bytes in and out) handled by each
- * worker. We normalize that traffic by each workers' weight. So assuming
- * a setup as below:
- *
- * worker     a    b    c
- * lbfactor   1    1    3
- *
- * the scheduler will allow worker c to handle 3 times the
- * traffic of a and b. If each request/response results in the
- * same amount of traffic, then c would be accessed 3 times as
- * often as a or b. If, for example, a handled a request that
- * resulted in a large i/o bytecount, then b and c would be
- * chosen more often, to even things out.
- */
-static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
-                                         request_rec *r)
-{
-    int i;
-    apr_off_t mytraffic = 0;
-    apr_off_t curmin = 0;
-    proxy_worker *worker;
-    proxy_worker *mycandidate = NULL;
-    int cur_lbset = 0;
-    int max_lbset = 0;
-    int checking_standby;
-    int checked_standby;
+            /* Re-open the mutex for the child. */
+            rv = apr_global_mutex_child_init(&(balancer->mutex),
+                                             apr_global_mutex_lockfile(balancer->mutex),
+                                             p);
+            if (rv != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
+                             "Failed to reopen mutex %s: %s in child",
+                             balancer->name, balancer_mutex_type);
+                exit(1); /* Ugly, but what else? */
+            }
 
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
-                 "proxy: Entering bytraffic for BALANCER (%s)",
-                 balancer->name);
-
-    /* First try to see if we have available candidate */
-    do {
-        checking_standby = checked_standby = 0;
-        while (!mycandidate && !checked_standby) {
-            worker = (proxy_worker *)balancer->workers->elts;
-            for (i = 0; i < balancer->workers->nelts; i++, worker++) {
-                if (!checking_standby) {    /* first time through */
-                    if (worker->s->lbset > max_lbset)
-                        max_lbset = worker->s->lbset;
-                }
-                if (worker->s->lbset > cur_lbset)
-                    continue;
-                if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
-                    continue;
-                /* If the worker is in error state run
-                 * retry on that worker. It will be marked as
-                 * operational if the retry timeout is elapsed.
-                 * The worker might still be unusable, but we try
-                 * anyway.
-                 */
-                if (!PROXY_WORKER_IS_USABLE(worker))
-                    ap_proxy_retry_worker("BALANCER", worker, r->server);
-                /* Take into calculation only the workers that are
-                 * not in error state or not disabled.
-                 */
-                if (PROXY_WORKER_IS_USABLE(worker)) {
-                    mytraffic = (worker->s->transferred/worker->s->lbfactor) +
-                                (worker->s->read/worker->s->lbfactor);
-                    if (!mycandidate || mytraffic < curmin) {
-                        mycandidate = worker;
-                        curmin = mytraffic;
-                    }
-                }
+            /* now attach */
+            storage->attach(&(balancer->slot), balancer->sname, &size, &num, p);
+            if (!balancer->slot) {
+                ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s, "slotmem_attach failed");
+                exit(1); /* Ugly, but what else? */
             }
-            checked_standby = checking_standby++;
+            if (balancer->s->lbmethod && balancer->s->lbmethod->reset)
+               balancer->s->lbmethod->reset(balancer, s);
+            init_balancer_members(conf, s, balancer);
+            balancer++;
         }
-        cur_lbset++;
-    } while (cur_lbset <= max_lbset && !mycandidate);
+        s = s->next;
+    }
 
-    return mycandidate;
 }
 
-/*
- * How to add additional lbmethods:
- *   1. Create func which determines "best" candidate worker
- *      (eg: find_best_bytraffic, above)
- *   2. Register it as a provider.
- */
-static const proxy_balancer_method byrequests =
-{
-    "byrequests",
-    &find_best_byrequests,
-    NULL
-};
-
-static const proxy_balancer_method bytraffic =
-{
-    "bytraffic",
-    &find_best_bytraffic,
-    NULL
-};
-
 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
 {
     /* Only the mpm_winnt has child init hook handler.
      * make sure that we are called after the mpm
-     * initializes and after the mod_proxy
+     * initializes
      */
-    static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
+    static const char *const aszPred[] = { "mpm_winnt.c", "mod_slotmem_shm.c", NULL};
      /* manager handler */
+    ap_hook_post_config(balancer_post_config, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_pre_config(balancer_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
     ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
-    ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
+    ap_hook_child_init(balancer_child_init, aszPred, NULL, APR_HOOK_MIDDLE);
     proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
     proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
     proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
-    ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
-    ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
 }
 
-module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
+AP_DECLARE_MODULE(proxy_balancer) = {
     STANDARD20_MODULE_STUFF,
     NULL,       /* create per-directory config structure */
     NULL,       /* merge per-directory config structures */