Moved apache code into a folder to help prepare for packaging where we dont want...
[httpd-crcsyncproxy.git] / apache / modules / proxy / examples / mod_lbmethod_rr.c
blobb7f4f0f0ad2215a9b72d90b5c2e5c673eeaae3b0
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Round Robin lbmethod EXAMPLE module for Apache proxy */
19 /* NOTE: This is designed simply to provide some info on how to create
20 extra lbmethods via sub-modules... This code is ugly
21 and needs work to actually do round-robin "right"
22 but that is left as an exercise for the reader */
24 #include "mod_proxy.h"
25 #include "scoreboard.h"
26 #include "ap_mpm.h"
27 #include "apr_version.h"
28 #include "apr_hooks.h"
30 #if APR_HAVE_UNISTD_H
31 #include <unistd.h> /* for getpid() */
32 #endif
34 module AP_MODULE_DECLARE_DATA proxy_balancer_rr_module;
36 typedef struct {
37 int index;
38 } rr_data ;
42 static proxy_worker *find_best_roundrobin(proxy_balancer *balancer,
43 request_rec *r)
45 int i;
46 proxy_worker *worker;
47 proxy_worker *mycandidate = NULL;
48 int checking_standby;
49 int checked_standby;
50 rr_data *ctx;
52 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
53 "proxy: Entering roundrobin for BALANCER %s (%d)",
54 balancer->name, (int)getpid());
56 /* The index of the candidate last chosen is stored in ctx->index */
57 if (!balancer->context) {
58 /* UGLY */
59 ctx = apr_pcalloc(r->server->process->pconf, sizeof(rr_data));
60 balancer->context = (void *)ctx;
61 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
62 "proxy: Creating roundrobin ctx for BALANCER %s (%d)",
63 balancer->name, (int)getpid());
64 } else {
65 ctx = (rr_data *)balancer->context;
67 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
68 "proxy: roundrobin index: %d (%d)",
69 ctx->index, (int)getpid());
71 checking_standby = checked_standby = 0;
72 while (!mycandidate && !checked_standby) {
73 worker = (proxy_worker *)balancer->workers->elts;
75 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
76 if (i < ctx->index)
77 continue;
78 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
79 continue;
80 if (!PROXY_WORKER_IS_USABLE(worker))
81 ap_proxy_retry_worker("BALANCER", worker, r->server);
82 if (PROXY_WORKER_IS_USABLE(worker)) {
83 mycandidate = worker;
84 break;
87 checked_standby = checking_standby++;
91 ctx->index += 1;
92 if (ctx->index >= balancer->workers->nelts) {
93 ctx->index = 0;
95 return mycandidate;
98 static const proxy_balancer_method roundrobin =
100 "roundrobin",
101 &find_best_roundrobin,
102 NULL
106 static void ap_proxy_rr_register_hook(apr_pool_t *p)
108 ap_register_provider(p, PROXY_LBMETHOD, "roundrobin", "0", &roundrobin);
111 module AP_MODULE_DECLARE_DATA proxy_balancer_rr_module = {
112 STANDARD20_MODULE_STUFF,
113 NULL, /* create per-directory config structure */
114 NULL, /* merge per-directory config structures */
115 NULL, /* create per-server config structure */
116 NULL, /* merge per-server config structures */
117 NULL, /* command apr_table_t */
118 ap_proxy_rr_register_hook /* register hooks */