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