From f76e94d231449d9da6d7f5b5df18d9bfb9975180 Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Wed, 25 Oct 2023 10:10:14 +0200 Subject: [PATCH] MINOR: backend: refactor insertion in avail conns tree Define a new function srv_add_to_avail_list(). This function is used to centralize connection insertion in available tree. It reuses a BUG_ON() statement to ensure the connection is not present in the idle list. --- include/haproxy/server.h | 1 + src/backend.c | 5 ++--- src/connection.c | 6 ++++-- src/mux_fcgi.c | 4 ++-- src/mux_h2.c | 4 ++-- src/server.c | 10 ++++++++++ 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/haproxy/server.h b/include/haproxy/server.h index b5923af69..8b523bf06 100644 --- a/include/haproxy/server.h +++ b/include/haproxy/server.h @@ -87,6 +87,7 @@ struct connection *srv_lookup_conn_next(struct connection *conn); void _srv_add_idle(struct server *srv, struct connection *conn, int is_safe); int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe); +void srv_add_to_avail_list(struct server *srv, struct connection *conn); struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state); int srv_apply_track(struct server *srv, struct proxy *curproxy); diff --git a/src/backend.c b/src/backend.c index 359bd499f..a90dd905f 100644 --- a/src/backend.c +++ b/src/backend.c @@ -1276,8 +1276,7 @@ static struct connection *conn_backend_get(struct stream *s, struct server *srv, session_add_conn(s->sess, conn, conn->target); } else { - eb64_insert(&srv->per_thr[tid].avail_conns, - &conn->hash_node->node); + srv_add_to_avail_list(srv, conn); } } return conn; @@ -1781,7 +1780,7 @@ static int connect_server(struct stream *s) if (srv && reuse_mode == PR_O_REUSE_ALWS && !(srv_conn->flags & CO_FL_PRIVATE) && srv_conn->mux->avail_streams(srv_conn) > 0) { - eb64_insert(&srv->per_thr[tid].avail_conns, &srv_conn->hash_node->node); + srv_add_to_avail_list(srv, srv_conn); } else if (srv_conn->flags & CO_FL_PRIVATE || (reuse_mode == PR_O_REUSE_SAFE && diff --git a/src/connection.c b/src/connection.c index a7ab6f608..59893fbb5 100644 --- a/src/connection.c +++ b/src/connection.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -107,8 +108,9 @@ int conn_create_mux(struct connection *conn) * server list. */ if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) && - !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0) - eb64_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node); + !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0) { + srv_add_to_avail_list(srv, conn); + } else if (conn->flags & CO_FL_PRIVATE) { /* If it fail now, the same will be done in mux->detach() callback */ session_add_conn(sess, conn, conn->target); diff --git a/src/mux_fcgi.c b/src/mux_fcgi.c index ac2f9cc38..f14de4975 100644 --- a/src/mux_fcgi.c +++ b/src/mux_fcgi.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -3603,8 +3604,7 @@ static void fcgi_detach(struct sedesc *sd) else if (!fconn->conn->hash_node->node.node.leaf_p && fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) && !LIST_INLIST(&fconn->conn->session_list)) { - eb64_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns, - &fconn->conn->hash_node->node); + srv_add_to_avail_list(__objt_server(fconn->conn->target), fconn->conn); } } } diff --git a/src/mux_h2.c b/src/mux_h2.c index e6e1b6da8..785fbd9cd 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -4651,8 +4652,7 @@ static void h2_detach(struct sedesc *sd) else if (!h2c->conn->hash_node->node.node.leaf_p && h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) && !LIST_INLIST(&h2c->conn->session_list)) { - eb64_insert(&__objt_server(h2c->conn->target)->per_thr[tid].avail_conns, - &h2c->conn->hash_node->node); + srv_add_to_avail_list(__objt_server(h2c->conn->target), h2c->conn); } } } diff --git a/src/server.c b/src/server.c index 37e8beba0..4873c1438 100644 --- a/src/server.c +++ b/src/server.c @@ -6136,6 +6136,16 @@ int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_saf return 0; } +/* Insert connection in server available list. This is reserved + * for backend connection currently in used with usable streams left. + */ +void srv_add_to_avail_list(struct server *srv, struct connection *conn) +{ + /* connection cannot be in idle list if used as an avail idle conn. */ + BUG_ON(LIST_INLIST(&conn->idle_list)); + eb64_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node); +} + struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state) { struct server *srv;