REORG: backend: simplify conn_backend_get

Reorganize the conditions for the reuse of idle/safe connections :
- reduce code by using variable to store reuse mode and idle/safe conns
  counts
- consider that idle/safe/avail lists are properly allocated if
  max_idle_conns not null. An allocation failure prevents haproxy
  startup.
This commit is contained in:
Amaury Denoyelle 2021-01-26 14:35:26 +01:00 committed by Willy Tarreau
parent 37e25bcd1e
commit 7f68d815af

View File

@ -1237,6 +1237,7 @@ int connect_server(struct stream *s)
struct connection *srv_conn = NULL; struct connection *srv_conn = NULL;
struct conn_stream *srv_cs = NULL; struct conn_stream *srv_cs = NULL;
struct server *srv; struct server *srv;
const int reuse_mode = s->be->options & PR_O_REUSE_MASK;
int reuse = 0; int reuse = 0;
int init_mux = 0; int init_mux = 0;
int err; int err;
@ -1254,9 +1255,7 @@ int connect_server(struct stream *s)
srv = objt_server(s->target); srv = objt_server(s->target);
if (srv && !reuse) { if (srv && !reuse && reuse_mode != PR_O_REUSE_NEVR) {
srv_conn = NULL;
/* Below we pick connections from the safe, idle or /* Below we pick connections from the safe, idle or
* available (which are safe too) lists based * available (which are safe too) lists based
* on the strategy, the fact that this is a first or second * on the strategy, the fact that this is a first or second
@ -1275,30 +1274,28 @@ int connect_server(struct stream *s)
* Idle conns are necessarily looked up on the same thread so * Idle conns are necessarily looked up on the same thread so
* that there is no concurrency issues. * that there is no concurrency issues.
*/ */
if (srv->available_conns && !LIST_ISEMPTY(&srv->available_conns[tid]) && if (!LIST_ISEMPTY(&srv->available_conns[tid])) {
((s->be->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR)) {
srv_conn = LIST_ELEM(srv->available_conns[tid].n, struct connection *, list); srv_conn = LIST_ELEM(srv->available_conns[tid].n, struct connection *, list);
reuse = 1; reuse = 1;
} }
else if (!srv_conn && srv->curr_idle_conns > 0) { /* if no available connections found, search for an idle/safe */
if (srv->idle_conns && srv->safe_conns && else if (srv->max_idle_conns && srv->curr_idle_conns > 0) {
((s->be->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR && const int not_first_req = s->txn && s->txn->flags & TX_NOT_FIRST;
s->txn && (s->txn->flags & TX_NOT_FIRST)) && const int idle = srv->curr_idle_nb > 0;
srv->curr_idle_nb + srv->curr_safe_nb > 0) { const int safe = srv->curr_safe_nb > 0;
/* we're on the second column of the tables above, let's
* try idle then safe. /* second column of the tables above,
*/ * search for an idle then safe conn */
if (not_first_req) {
if (idle || safe)
srv_conn = conn_backend_get(s, srv, 0); srv_conn = conn_backend_get(s, srv, 0);
} }
else if (srv->safe_conns && /* first column of the tables above */
((s->txn && (s->txn->flags & TX_NOT_FIRST)) || else if (reuse_mode >= PR_O_REUSE_AGGR) {
(s->be->options & PR_O_REUSE_MASK) >= PR_O_REUSE_AGGR) && /* search for a safe conn */
srv->curr_safe_nb > 0) { if (safe)
srv_conn = conn_backend_get(s, srv, 1); srv_conn = conn_backend_get(s, srv, 1);
} else if (reuse_mode == PR_O_REUSE_ALWS && idle)
else if (srv->idle_conns &&
((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) &&
srv->curr_idle_nb > 0) {
srv_conn = conn_backend_get(s, srv, 0); srv_conn = conn_backend_get(s, srv, 0);
} }
@ -1401,7 +1398,7 @@ int connect_server(struct stream *s)
if (srv_conn) { if (srv_conn) {
srv_conn->owner = s->sess; srv_conn->owner = s->sess;
if ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) if (reuse_mode == PR_O_REUSE_NEVR)
conn_set_private(srv_conn); conn_set_private(srv_conn);
} }
} }
@ -1551,11 +1548,11 @@ int connect_server(struct stream *s)
* safe and the mux protocol supports multiplexing, add it in * safe and the mux protocol supports multiplexing, add it in
* the session server list. * the session server list.
*/ */
if (srv && ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) && if (srv && reuse_mode == PR_O_REUSE_ALWS &&
!(srv_conn->flags & CO_FL_PRIVATE) && srv_conn->mux->avail_streams(srv_conn) > 0) !(srv_conn->flags & CO_FL_PRIVATE) && srv_conn->mux->avail_streams(srv_conn) > 0)
LIST_ADDQ(&srv->available_conns[tid], mt_list_to_list(&srv_conn->list)); LIST_ADDQ(&srv->available_conns[tid], mt_list_to_list(&srv_conn->list));
else if (srv_conn->flags & CO_FL_PRIVATE || else if (srv_conn->flags & CO_FL_PRIVATE ||
((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_SAFE && (reuse_mode == PR_O_REUSE_SAFE &&
srv_conn->mux->flags & MX_FL_HOL_RISK)) { srv_conn->mux->flags & MX_FL_HOL_RISK)) {
/* If it fail now, the same will be done in mux->detach() callback */ /* If it fail now, the same will be done in mux->detach() callback */
session_add_conn(s->sess, srv_conn, srv_conn->target); session_add_conn(s->sess, srv_conn, srv_conn->target);