]> granicus.if.org Git - apache/blob - modules/proxy/examples/mod_lbmethod_rr.c
Arrange the proxy_balancer_method:
[apache] / modules / proxy / examples / mod_lbmethod_rr.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /* Round Robin lbmethod EXAMPLE module for Apache proxy */
18
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 */
23
24 #include "mod_proxy.h"
25 #include "scoreboard.h"
26 #include "ap_mpm.h"
27 #include "apr_version.h"
28 #include "apr_hooks.h"
29
30 #if APR_HAVE_UNISTD_H
31 #include <unistd.h> /* for getpid() */
32 #endif
33
34 module AP_MODULE_DECLARE_DATA proxy_balancer_rr_module;
35
36 typedef struct {
37     int index;
38 } rr_data ;
39
40 /*
41  */
42 static proxy_worker *find_best_roundrobin(proxy_balancer *balancer,
43                                          request_rec *r)
44 {
45     int i;
46     proxy_worker **worker;
47     proxy_worker *mycandidate = NULL;
48     int checking_standby;
49     int checked_standby;
50     rr_data *ctx;
51
52     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
53                  "proxy: Entering roundrobin for BALANCER %s (%d)",
54                  balancer->name, (int)getpid());
55     
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;
66     }
67     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
68                  "proxy: roundrobin index: %d (%d)",
69                  ctx->index, (int)getpid());
70
71     checking_standby = checked_standby = 0;
72     while (!mycandidate && !checked_standby) {
73         worker = (proxy_worker **)balancer->workers->elts;
74
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;
85             }
86         }
87         checked_standby = checking_standby++;
88     }
89
90
91     ctx->index += 1;
92     if (ctx->index >= balancer->workers->nelts) {
93         ctx->index = 0;
94     }
95     return mycandidate;
96 }
97
98 static apr_status_t reset(proxy_balancer *balancer, server_rec *r) {
99         return APR_SUCCESS;
100 }
101
102 static apr_status_t age(proxy_balancer *balancer, server_rec *r) {
103         return APR_SUCCESS;
104 }
105
106 static const proxy_balancer_method roundrobin =
107 {
108     "roundrobin",
109     &find_best_roundrobin,
110     NULL,
111     &reset,
112     &age
113 };
114
115
116 static void ap_proxy_rr_register_hook(apr_pool_t *p)
117 {
118     ap_register_provider(p, PROXY_LBMETHOD, "roundrobin", "0", &roundrobin);
119 }
120
121 module AP_MODULE_DECLARE_DATA proxy_balancer_rr_module = {
122     STANDARD20_MODULE_STUFF,
123     NULL,       /* create per-directory config structure */
124     NULL,       /* merge per-directory config structures */
125     NULL,       /* create per-server config structure */
126     NULL,       /* merge per-server config structures */
127     NULL,       /* command apr_table_t */
128     ap_proxy_rr_register_hook /* register hooks */
129 };