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"
27 #include "apr_version.h"
28 #include "apr_hooks.h"
31 #include <unistd.h> /* for getpid() */
34 module AP_MODULE_DECLARE_DATA proxy_balancer_rr_module
;
42 static proxy_worker
*find_best_roundrobin(proxy_balancer
*balancer
,
47 proxy_worker
*mycandidate
= NULL
;
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
) {
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());
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
++) {
78 if ( (checking_standby
? !PROXY_WORKER_IS_STANDBY(worker
) : PROXY_WORKER_IS_STANDBY(worker
)) )
80 if (!PROXY_WORKER_IS_USABLE(worker
))
81 ap_proxy_retry_worker("BALANCER", worker
, r
->server
);
82 if (PROXY_WORKER_IS_USABLE(worker
)) {
87 checked_standby
= checking_standby
++;
92 if (ctx
->index
>= balancer
->workers
->nelts
) {
98 static const proxy_balancer_method roundrobin
=
101 &find_best_roundrobin
,
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 */