mod_proxy_balancer.c revision de446ce1c5cc5df1bb4e530c7e4495b56c7572fc
0734f81384d5f2528bb119f89d135043a280d5b2minfrin/* Licensed to the Apache Software Foundation (ASF) under one or more
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * contributor license agreements. See the NOTICE file distributed with
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * this work for additional information regarding copyright ownership.
d96cf063f04828102edbb0b4987c34b490e2731cnd * The ASF licenses this file to You under the Apache License, Version 2.0
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * (the "License"); you may not use this file except in compliance with
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * the License. You may obtain a copy of the License at
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * Unless required by applicable law or agreed to in writing, software
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * distributed under the License is distributed on an "AS IS" BASIS,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * See the License for the specific language governing permissions and
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * limitations under the License.
0734f81384d5f2528bb119f89d135043a280d5b2minfrin/* Load balancer module for Apache proxy */
0734f81384d5f2528bb119f89d135043a280d5b2minfrinstatic const char *balancer_mutex_type = "proxy-balancer-shm";
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * Register our mutex type before the config is read so we
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * can adjust the mutex settings using the Mutex directive.
0734f81384d5f2528bb119f89d135043a280d5b2minfrinstatic int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
d84dc356814b255affe3abe2ad93ce17f98e632chumbedoohextern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
d84dc356814b255affe3abe2ad93ce17f98e632chumbedoohstatic int proxy_balancer_canon(request_rec *r, char *url)
d84dc356814b255affe3abe2ad93ce17f98e632chumbedooh const char *err;
0734f81384d5f2528bb119f89d135043a280d5b2minfrin /* TODO: offset of BALANCER_PREFIX ?? */
0734f81384d5f2528bb119f89d135043a280d5b2minfrin ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin /* do syntatic check.
04e37a05e8fe2c057cfcc546f382d99fa79894c0gryzor * We break the URL into host, port, path, search
0734f81384d5f2528bb119f89d135043a280d5b2minfrin err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
0734f81384d5f2528bb119f89d135043a280d5b2minfrin "error parsing URL %s: %s",
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * now parse path/search args, according to rfc1738:
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * process the path. With proxy-noncanon set (by
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * mod_proxy) we use the raw, unparsed uri
0734f81384d5f2528bb119f89d135043a280d5b2minfrin path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
0734f81384d5f2528bb119f89d135043a280d5b2minfrin r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
0734f81384d5f2528bb119f89d135043a280d5b2minfrinstatic void init_balancer_members(apr_pool_t *p, server_rec *s,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin "Looking at %s -> %s initialized?", balancer->name, worker->s->name);
f8080c631d40d9f23da18c91116ca59d8d5468f6covener worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
0734f81384d5f2528bb119f89d135043a280d5b2minfrin /* Set default number of attempts to the number of
0734f81384d5f2528bb119f89d135043a280d5b2minfrin if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
0734f81384d5f2528bb119f89d135043a280d5b2minfrin balancer->s->max_attempts = balancer->workers->nelts - 1;
0734f81384d5f2528bb119f89d135043a280d5b2minfrin/* Retrieve the parameter with the given name
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * Something like 'JSESSIONID=12345...N'
0734f81384d5f2528bb119f89d135043a280d5b2minfrinstatic char *get_path_param(apr_pool_t *pool, char *url,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * Session path was found, get it's value
0734f81384d5f2528bb119f89d135043a280d5b2minfrin path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
0734f81384d5f2528bb119f89d135043a280d5b2minfrinstatic char *get_cookie_param(request_rec *r, const char *name)
0734f81384d5f2528bb119f89d135043a280d5b2minfrin const char *cookies;
d84dc356814b255affe3abe2ad93ce17f98e632chumbedooh if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
0734f81384d5f2528bb119f89d135043a280d5b2minfrin for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
d84dc356814b255affe3abe2ad93ce17f98e632chumbedooh start_cookie = ap_strstr_c(start_cookie + 1, name)) {
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * Session cookie was found, get it's value
0734f81384d5f2528bb119f89d135043a280d5b2minfrin/* Find the worker that has the 'route' defined
d84dc356814b255affe3abe2ad93ce17f98e632chumbedoohstatic proxy_worker *find_route_worker(proxy_balancer *balancer,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin for (i = 0; i < balancer->workers->nelts; i++, workers++) {
0734f81384d5f2528bb119f89d135043a280d5b2minfrin if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
0734f81384d5f2528bb119f89d135043a280d5b2minfrin if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * If the worker is in error state run
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * retry on that worker. It will be marked as
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * operational if the retry timeout is elapsed.
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * The worker might still be unusable, but we try
0734f81384d5f2528bb119f89d135043a280d5b2minfrin ap_proxy_retry_worker("BALANCER", worker, r->server);
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * We have a worker that is unusable.
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * It can be in error or disabled, but in case
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * it has a redirection set use that redirection worker.
d84dc356814b255affe3abe2ad93ce17f98e632chumbedooh * This enables to safely remove the member from the
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * balancer. Of course you will need some kind of
d84dc356814b255affe3abe2ad93ce17f98e632chumbedooh * session replication between those two remote.
0734f81384d5f2528bb119f89d135043a280d5b2minfrin rworker = find_route_worker(balancer, worker->s->redirect, r);
return rworker;
return NULL;
request_rec *r,
char **route,
const char **sticky_used,
char **url)
return NULL;
if (*route) {
if (*route) {
(*route)++;
return worker;
return NULL;
request_rec *r)
return NULL;
if (candidate)
return candidate;
char **url)
if (scheme)
if (!worker) {
NULL));
return OK;
int ok = 0;
if (!ok) {
request_rec *r,
int access_status;
if (!*balancer &&
return DECLINED;
return DECLINED;
if (runtime) {
int i, total_factor = 0;
workers++;
int i, member_of = 0;
workers++;
if (member_of) {
return HTTP_SERVICE_UNAVAILABLE;
if (!*worker) {
if (!runtime) {
return HTTP_SERVICE_UNAVAILABLE;
if (route) {
return access_status;
request_rec *r,
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
return OK;
if (!storage) {
return !OK;
return !OK;
unsigned int index;
return HTTP_INTERNAL_SERVER_ERROR;
return !OK;
return !OK;
return !OK;
return !OK;
return !OK;
return !OK;
return !OK;
s = s->next;
return OK;
if (flag)
if (!flag)
char *args;
char *key;
while (key) {
if (val) {
while (ok) {
ok++;
void *sconf;
const char *name;
const char *action;
return DECLINED;
r->allowed = 0
return DECLINED;
return HTTP_INTERNAL_SERVER_ERROR;
if (!bsel ||
ok2change = 0;
const char *val;
if (bsel)
const char *val;
int ival;
if (lbmethod) {
char *path;
char *ret;
if (!ret) {
unsigned int index;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
++workers;
++balancer;
"<th>MaxMembers</th><th>StickySession</th><th>DisableFailover</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
NULL);
ap_rprintf(r, "<td align='center'>%" APR_SIZE_T_FMT "</td><td align='center'>", worker->s->elected);
++workers;
++balancer;
NULL);
NULL);
ap_rputs("<td><table border='1'><tr><th>Ign</th><th>Drn</th><th>Dis</th><th>Stby</th></tr>\n<tr>", r);
} else if (bsel) {
if (provs) {
ap_rflush(r);
return DONE;
unsigned int num;
s = s->next;