Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Fast Weighted Least Connection load balancing algorithm. |
| 3 | * |
| 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/compat.h> |
| 14 | #include <common/config.h> |
| 15 | #include <common/debug.h> |
Willy Tarreau | 45cb4fb | 2009-10-26 21:10:04 +0100 | [diff] [blame] | 16 | #include <eb32tree.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 17 | |
| 18 | #include <types/global.h> |
| 19 | #include <types/server.h> |
| 20 | |
| 21 | #include <proto/backend.h> |
| 22 | #include <proto/queue.h> |
| 23 | |
| 24 | |
| 25 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 26 | * function is meant to be called when a server is going down or has its |
| 27 | * weight disabled. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 28 | * |
| 29 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 30 | */ |
| 31 | static inline void fwlc_remove_from_tree(struct server *s) |
| 32 | { |
| 33 | s->lb_tree = NULL; |
| 34 | } |
| 35 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 36 | /* simply removes a server from a tree. |
| 37 | * |
| 38 | * The server's lock and the lbprm's lock must be held. |
| 39 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 40 | static inline void fwlc_dequeue_srv(struct server *s) |
| 41 | { |
| 42 | eb32_delete(&s->lb_node); |
| 43 | } |
| 44 | |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 45 | /* Queue a server in its associated tree, assuming the <eweight> is >0. |
Willy Tarreau | 1eb6c55 | 2018-12-14 08:33:28 +0100 | [diff] [blame] | 46 | * Servers are sorted by (#conns+1)/weight. To ensure maximum accuracy, |
| 47 | * we use (#conns+1)*SRV_EWGHT_MAX/eweight as the sorting key. The reason |
| 48 | * for using #conns+1 is to sort by weights in case the server is picked |
| 49 | * and not before it is picked. This provides a better load accuracy for |
| 50 | * low connection counts when weights differ and makes sure the round-robin |
Willy Tarreau | 345ddf4 | 2019-09-06 17:04:04 +0200 | [diff] [blame] | 51 | * applies between servers of highest weight first. However servers with no |
| 52 | * connection are always picked first so that under low loads, it's not |
| 53 | * always the single server with the highest weight that gets picked. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 54 | * |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 55 | * NOTE: Depending on the calling context, we use s->next_eweight or |
| 56 | * s->cur_eweight. The next value is used when the server state is updated |
| 57 | * (because the weight changed for instance). During this step, the server |
| 58 | * state is not yet committed. The current value is used to reposition the |
| 59 | * server in the tree. This happens when the server is used. |
| 60 | * |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 61 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 62 | */ |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 63 | static inline void fwlc_queue_srv(struct server *s, unsigned int eweight) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 64 | { |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 65 | s->lb_node.key = s->served ? (s->served + 1) * SRV_EWGHT_MAX / eweight : 0; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 66 | eb32_insert(s->lb_tree, &s->lb_node); |
| 67 | } |
| 68 | |
| 69 | /* Re-position the server in the FWLC tree after it has been assigned one |
| 70 | * connection or after it has released one. Note that it is possible that |
| 71 | * the server has been moved out of the tree due to failed health-checks. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 72 | * |
| 73 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 74 | */ |
| 75 | static void fwlc_srv_reposition(struct server *s) |
| 76 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 77 | HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Christopher Faulet | 3f0b1de | 2019-06-19 10:50:38 +0200 | [diff] [blame] | 78 | if (s->lb_tree) { |
| 79 | fwlc_dequeue_srv(s); |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 80 | fwlc_queue_srv(s, s->cur_eweight); |
Christopher Faulet | 3f0b1de | 2019-06-19 10:50:38 +0200 | [diff] [blame] | 81 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 82 | HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* This function updates the server trees according to server <srv>'s new |
| 86 | * state. It should be called when server <srv>'s status changes to down. |
| 87 | * It is not important whether the server was already down or not. It is not |
| 88 | * important either that the new state is completely down (the caller may not |
| 89 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 90 | * |
| 91 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 92 | */ |
| 93 | static void fwlc_set_server_status_down(struct server *srv) |
| 94 | { |
| 95 | struct proxy *p = srv->proxy; |
| 96 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 97 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 98 | return; |
| 99 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 100 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 101 | goto out_update_state; |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 102 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 103 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 104 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 105 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 106 | /* server was already down */ |
| 107 | goto out_update_backend; |
| 108 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 109 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 110 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 111 | p->srv_bck--; |
| 112 | |
| 113 | if (srv == p->lbprm.fbck) { |
| 114 | /* we lost the first backup server in a single-backup |
| 115 | * configuration, we must search another one. |
| 116 | */ |
| 117 | struct server *srv2 = p->lbprm.fbck; |
| 118 | do { |
| 119 | srv2 = srv2->next; |
| 120 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 121 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 122 | srv_willbe_usable(srv2))); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 123 | p->lbprm.fbck = srv2; |
| 124 | } |
| 125 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 126 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 127 | p->srv_act--; |
| 128 | } |
| 129 | |
| 130 | fwlc_dequeue_srv(srv); |
| 131 | fwlc_remove_from_tree(srv); |
| 132 | |
| 133 | out_update_backend: |
| 134 | /* check/update tot_used, tot_weight */ |
| 135 | update_backend_weight(p); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 136 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 137 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 138 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 139 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* This function updates the server trees according to server <srv>'s new |
| 143 | * state. It should be called when server <srv>'s status changes to up. |
| 144 | * It is not important whether the server was already down or not. It is not |
| 145 | * important either that the new state is completely UP (the caller may not |
| 146 | * know all the variables of a server's state). This function will not change |
| 147 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 148 | * |
| 149 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 150 | */ |
| 151 | static void fwlc_set_server_status_up(struct server *srv) |
| 152 | { |
| 153 | struct proxy *p = srv->proxy; |
| 154 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 155 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 156 | return; |
| 157 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 158 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 159 | goto out_update_state; |
| 160 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 161 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 162 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 163 | if (srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 164 | /* server was already up */ |
| 165 | goto out_update_backend; |
| 166 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 167 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 168 | srv->lb_tree = &p->lbprm.fwlc.bck; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 169 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 170 | p->srv_bck++; |
| 171 | |
| 172 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 173 | if (!p->lbprm.fbck) { |
| 174 | /* there was no backup server anymore */ |
| 175 | p->lbprm.fbck = srv; |
| 176 | } else { |
| 177 | /* we may have restored a backup server prior to fbck, |
| 178 | * in which case it should replace it. |
| 179 | */ |
| 180 | struct server *srv2 = srv; |
| 181 | do { |
| 182 | srv2 = srv2->next; |
| 183 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 184 | if (srv2) |
| 185 | p->lbprm.fbck = srv; |
| 186 | } |
| 187 | } |
| 188 | } else { |
| 189 | srv->lb_tree = &p->lbprm.fwlc.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 190 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 191 | p->srv_act++; |
| 192 | } |
| 193 | |
| 194 | /* note that eweight cannot be 0 here */ |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 195 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 196 | |
| 197 | out_update_backend: |
| 198 | /* check/update tot_used, tot_weight */ |
| 199 | update_backend_weight(p); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 200 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 201 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 202 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 203 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* This function must be called after an update to server <srv>'s effective |
| 207 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 208 | * |
| 209 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 210 | */ |
| 211 | static void fwlc_update_server_weight(struct server *srv) |
| 212 | { |
| 213 | int old_state, new_state; |
| 214 | struct proxy *p = srv->proxy; |
| 215 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 216 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 217 | return; |
| 218 | |
| 219 | /* If changing the server's weight changes its state, we simply apply |
| 220 | * the procedures we already have for status change. If the state |
| 221 | * remains down, the server is not in any tree, so it's as easy as |
| 222 | * updating its values. If the state remains up with different weights, |
| 223 | * there are some computations to perform to find a new place and |
| 224 | * possibly a new tree for this server. |
| 225 | */ |
| 226 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 227 | old_state = srv_currently_usable(srv); |
| 228 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 229 | |
| 230 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 231 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | else if (!old_state && new_state) { |
| 235 | fwlc_set_server_status_up(srv); |
| 236 | return; |
| 237 | } |
| 238 | else if (old_state && !new_state) { |
| 239 | fwlc_set_server_status_down(srv); |
| 240 | return; |
| 241 | } |
| 242 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 243 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 244 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 245 | if (srv->lb_tree) |
| 246 | fwlc_dequeue_srv(srv); |
| 247 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 248 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 249 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 250 | srv->lb_tree = &p->lbprm.fwlc.bck; |
| 251 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 252 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 253 | srv->lb_tree = &p->lbprm.fwlc.act; |
| 254 | } |
| 255 | |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 256 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 257 | |
| 258 | update_backend_weight(p); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 259 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 260 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 261 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* This function is responsible for building the trees in case of fast |
| 265 | * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to |
| 266 | * uweight ratio. Both active and backup groups are initialized. |
| 267 | */ |
| 268 | void fwlc_init_server_tree(struct proxy *p) |
| 269 | { |
| 270 | struct server *srv; |
| 271 | struct eb_root init_head = EB_ROOT; |
| 272 | |
| 273 | p->lbprm.set_server_status_up = fwlc_set_server_status_up; |
| 274 | p->lbprm.set_server_status_down = fwlc_set_server_status_down; |
| 275 | p->lbprm.update_server_eweight = fwlc_update_server_weight; |
| 276 | p->lbprm.server_take_conn = fwlc_srv_reposition; |
| 277 | p->lbprm.server_drop_conn = fwlc_srv_reposition; |
| 278 | |
| 279 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 280 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 281 | srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult; |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 282 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | recount_servers(p); |
| 286 | update_backend_weight(p); |
| 287 | |
| 288 | p->lbprm.fwlc.act = init_head; |
| 289 | p->lbprm.fwlc.bck = init_head; |
| 290 | |
| 291 | /* queue active and backup servers in two distinct groups */ |
| 292 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 293 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 294 | continue; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 295 | srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act; |
Christopher Faulet | 2ecaf84 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 296 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | /* Return next server from the FWLC tree in backend <p>. If the tree is empty, |
| 301 | * return NULL. Saturated servers are skipped. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 302 | * |
| 303 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 304 | */ |
| 305 | struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 306 | { |
| 307 | struct server *srv, *avoided; |
| 308 | struct eb32_node *node; |
| 309 | |
| 310 | srv = avoided = NULL; |
| 311 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 312 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 313 | if (p->srv_act) |
| 314 | node = eb32_first(&p->lbprm.fwlc.act); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 315 | else if (p->lbprm.fbck) { |
| 316 | srv = p->lbprm.fbck; |
| 317 | goto out; |
| 318 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 319 | else if (p->srv_bck) |
| 320 | node = eb32_first(&p->lbprm.fwlc.bck); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 321 | else { |
| 322 | srv = NULL; |
| 323 | goto out; |
| 324 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 325 | |
| 326 | while (node) { |
| 327 | /* OK, we have a server. However, it may be saturated, in which |
| 328 | * case we don't want to reconsider it for now, so we'll simply |
| 329 | * skip it. Same if it's the server we try to avoid, in which |
| 330 | * case we simply remember it for later use if needed. |
| 331 | */ |
| 332 | struct server *s; |
| 333 | |
| 334 | s = eb32_entry(node, struct server, lb_node); |
| 335 | if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) { |
| 336 | if (s != srvtoavoid) { |
| 337 | srv = s; |
| 338 | break; |
| 339 | } |
| 340 | avoided = s; |
| 341 | } |
| 342 | node = eb32_next(node); |
| 343 | } |
| 344 | |
| 345 | if (!srv) |
| 346 | srv = avoided; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 347 | out: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 348 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 349 | return srv; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * Local variables: |
| 355 | * c-indent-level: 8 |
| 356 | * c-basic-offset: 8 |
| 357 | * End: |
| 358 | */ |