+static int init_runtime_score(apr_pool_t *pool, proxy_balancer *balancer)
+{
+ int i;
+ double median, ffactor = 0.0;
+ proxy_runtime_worker *workers;
+#if PROXY_HAS_SCOREBOARD
+ lb_score *score;
+ int mpm_daemons;
+#else
+ void *score;
+#endif
+
+ workers = (proxy_runtime_worker *)balancer->workers->elts;
+
+ for (i = 0; i < balancer->workers->nelts; i++) {
+#if PROXY_HAS_SCOREBOARD
+ ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &mpm_daemons);
+ /* Check if we are prefork or single child */
+ if (workers[i].w->hmax && mpm_daemons > 1) {
+ score = ap_get_scoreboard_lb(getpid(), workers[i].id);
+ }
+ else
+#endif
+ {
+ /* Use the plain memory */
+ score = apr_pcalloc(pool, sizeof(proxy_runtime_stat));
+ }
+ workers[i].s = (proxy_runtime_stat *)score;
+ }
+
+ /* Recalculate lbfactors */
+ for (i = 0; i < balancer->workers->nelts; i++) {
+ /* Set to the original configuration */
+ workers[i].s->lbfactor = workers[i].w->lbfactor;
+ ffactor += workers[i].s->lbfactor;
+ }
+ if (ffactor < 100.0) {
+ int z = 0;
+ for (i = 0; i < balancer->workers->nelts; i++) {
+ if (workers[i].s->lbfactor == 0.0)
+ ++z;
+ }
+ if (z) {
+ median = (100.0 - ffactor) / z;
+ for (i = 0; i < balancer->workers->nelts; i++) {
+ if (workers[i].s->lbfactor == 0.0)
+ workers[i].s->lbfactor = median;
+ }
+ }
+ else {
+ median = (100.0 - ffactor) / balancer->workers->nelts;
+ for (i = 0; i < balancer->workers->nelts; i++)
+ workers[i].s->lbfactor += median;
+ }
+ }
+ else if (ffactor > 100.0) {
+ median = (ffactor - 100.0) / balancer->workers->nelts;
+ for (i = 0; i < balancer->workers->nelts; i++) {
+ if (workers[i].s->lbfactor > median)
+ workers[i].s->lbfactor -= median;
+ }
+ }
+ for (i = 0; i < balancer->workers->nelts; i++) {
+ /* Update the status entires */
+ workers[i].s->lbstatus = workers[i].s->lbfactor;
+ }
+ balancer->status = 1;
+ return 0;
+}