From: Amaury Denoyelle Date: Fri, 25 Aug 2023 13:48:39 +0000 (+0200) Subject: MINOR: server: move idle tree insert in a dedicated function X-Git-Tag: v2.9-dev4~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61fc9568fb6546aa256144220db37e6548c1e5d7;p=thirdparty%2Fhaproxy.git MINOR: server: move idle tree insert in a dedicated function Define a new function _srv_add_idle(). This is a simple wrapper to insert a connection in the server idle tree. This is reserved for simple usage and require to idle_conns lock. In most cases, srv_add_to_idle_list() should be used. This patch does not have any functional change. However, it will help with the next patch as idle connection will be always inserted in a list as secondary storage along with idle/safe trees. --- diff --git a/include/haproxy/server.h b/include/haproxy/server.h index 730493362d..b5923af69b 100644 --- a/include/haproxy/server.h +++ b/include/haproxy/server.h @@ -85,6 +85,7 @@ void srv_release_conn(struct server *srv, struct connection *conn); struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash); 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); struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state); diff --git a/src/connection.c b/src/connection.c index df6c345fbe..46d942c451 100644 --- a/src/connection.c +++ b/src/connection.c @@ -177,12 +177,8 @@ int conn_notify_mux(struct connection *conn, int old_flags, int forced_wake) goto done; if (conn_in_list) { - struct eb_root *root = (conn_in_list == CO_FL_SAFE_LIST) ? - &srv->per_thr[tid].safe_conns : - &srv->per_thr[tid].idle_conns; - HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); - eb64_insert(root, &conn->hash_node->node); + _srv_add_idle(srv, conn, conn_in_list == CO_FL_SAFE_LIST); HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); } } diff --git a/src/mux_fcgi.c b/src/mux_fcgi.c index 104668d250..ec2cb60dcf 100644 --- a/src/mux_fcgi.c +++ b/src/mux_fcgi.c @@ -2986,10 +2986,7 @@ struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state) struct server *srv = objt_server(conn->target); HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); - if (conn_in_list == CO_FL_SAFE_LIST) - eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); - else - eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); + _srv_add_idle(srv, conn, conn_in_list == CO_FL_SAFE_LIST); HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); } return t; diff --git a/src/mux_h1.c b/src/mux_h1.c index b051e79e67..9b68e25c24 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -3701,10 +3701,7 @@ struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state) struct server *srv = objt_server(conn->target); HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); - if (conn_in_list == CO_FL_SAFE_LIST) - eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); - else - eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); + _srv_add_idle(srv, conn, conn_in_list == CO_FL_SAFE_LIST); HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); } return t; diff --git a/src/mux_h2.c b/src/mux_h2.c index 7f99eded43..6d5b43467d 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -4123,10 +4123,7 @@ struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state) struct server *srv = objt_server(conn->target); HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); - if (conn_in_list == CO_FL_SAFE_LIST) - eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); - else - eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); + _srv_add_idle(srv, conn, conn_in_list == CO_FL_SAFE_LIST); HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); } diff --git a/src/server.c b/src/server.c index 38b3a28dd2..a51d597d88 100644 --- a/src/server.c +++ b/src/server.c @@ -5986,6 +5986,23 @@ struct connection *srv_lookup_conn_next(struct connection *conn) return next_conn; } +/* Add in idle trees. Set if connection is deemed safe + * for reuse. + * + * This function is a simple wrapper for tree insert. It should only be used + * for internal usage or when removing briefly the connection to avoid takeover + * on it before reinserting it with this function. In other context, prefer to + * use the full feature srv_add_to_idle_list(). + * + * Must be called with idle_conns_lock. + */ +void _srv_add_idle(struct server *srv, struct connection *conn, int is_safe) +{ + struct eb_root *tree = is_safe ? &srv->per_thr[tid].safe_conns : + &srv->per_thr[tid].idle_conns; + eb64_insert(tree, &conn->hash_node->node); +} + /* This adds an idle connection to the server's list if the connection is * reusable, not held by any owner anymore, but still has available streams. */ @@ -6022,11 +6039,11 @@ int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_saf if (is_safe) { conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST; - eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); + _srv_add_idle(srv, conn, 1); _HA_ATOMIC_INC(&srv->curr_safe_nb); } else { conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST; - eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); + _srv_add_idle(srv, conn, 0); _HA_ATOMIC_INC(&srv->curr_idle_nb); } HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); diff --git a/src/ssl_sock.c b/src/ssl_sock.c index c8c0638372..9cc8166a59 100644 --- a/src/ssl_sock.c +++ b/src/ssl_sock.c @@ -6345,10 +6345,7 @@ leave: struct server *srv = objt_server(conn->target); HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); - if (conn_in_list == CO_FL_SAFE_LIST) - eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); - else - eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); + _srv_add_idle(srv, conn, conn_in_list == CO_FL_SAFE_LIST); HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); } return t;