]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: connection: Use a dedicated function to look for a session's connection
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 1 Jul 2020 14:36:51 +0000 (16:36 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 15 Jul 2020 12:08:14 +0000 (14:08 +0200)
The session_get_conn() must now be used to look for an available connection
matching a specific target for a given session. This simplifies a bit the
connect_server() function.

include/haproxy/session.h
src/backend.c

index 24f8e5d68aeaddd02eee51130bb0158c472f2502..5a46c4f5bbaac8c320962dcd7a94fb04bf57dac5 100644 (file)
@@ -145,6 +145,35 @@ static inline int session_check_idle_conn(struct session *sess, struct connectio
        return 0;
 }
 
+/* Look for an available connection matching the target <target> in the server
+ * list of the session <sess>. It returns a connection if found. Otherwise it
+ * returns NULL.
+ */
+static inline struct connection *session_get_conn(struct session *sess, void *target)
+{
+       struct connection *srv_conn = NULL;
+       struct sess_srv_list *srv_list;
+
+       list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
+               if (srv_list->target == target) {
+                       list_for_each_entry(srv_conn, &srv_list->conn_list, session_list) {
+                               if (srv_conn->mux && (srv_conn->mux->avail_streams(srv_conn) > 0)) {
+                                       if (srv_conn->flags & CO_FL_SESS_IDLE) {
+                                               srv_conn->flags &= ~CO_FL_SESS_IDLE;
+                                               sess->idle_conns--;
+                                       }
+                                       goto end;
+                               }
+                       }
+                       srv_conn = NULL; /* No available connection found */
+                       goto end;
+               }
+       }
+
+  end:
+       return srv_conn;
+}
+
 #endif /* _HAPROXY_SESSION_H */
 
 /*
index f346af558f151266bc39b7c595fb78fa770af1cb..8556d4c0a43f51d147a9482f32cc06b327321e77 100644 (file)
@@ -1197,10 +1197,8 @@ int connect_server(struct stream *s)
        struct connection *cli_conn = objt_conn(strm_orig(s));
        struct connection *srv_conn = NULL;
        struct conn_stream *srv_cs = NULL;
-       struct sess_srv_list *srv_list;
        struct server *srv;
        int reuse = 0;
-       int reuse_orphan = 0;
        int init_mux = 0;
        int err;
        int was_unused = 0;
@@ -1212,21 +1210,9 @@ int connect_server(struct stream *s)
        si_release_endpoint(&s->si[1]);
 
        /* first, search for a matching connection in the session's idle conns */
-       list_for_each_entry(srv_list, &s->sess->srv_list, srv_list) {
-               if (srv_list->target == s->target) {
-                       list_for_each_entry(srv_conn, &srv_list->conn_list, session_list) {
-                               if (conn_xprt_ready(srv_conn) &&
-                                   srv_conn->mux && (srv_conn->mux->avail_streams(srv_conn) > 0)) {
-                                       reuse = 1;
-                                       break;
-                               }
-                       }
-                       break;
-               }
-       }
-
-       if (!reuse)
-               srv_conn = NULL;
+       srv_conn = session_get_conn(s->sess, s->target);
+       if (srv_conn)
+               reuse = 1;
 
        srv = objt_server(s->target);
 
@@ -1287,7 +1273,6 @@ int connect_server(struct stream *s)
                         * pick.
                         */
                        if (srv_conn) {
-                               reuse_orphan = 1;
                                reuse = 1;
                                srv_conn->flags &= ~CO_FL_LIST_MASK;
                        }
@@ -1356,19 +1341,6 @@ int connect_server(struct stream *s)
                }
 
        }
-       /* If we're really reusing the connection, remove it from the orphan
-        * list and add it back to the idle list.
-        */
-       if (reuse) {
-               if (!reuse_orphan) {
-                       if (srv_conn->flags & CO_FL_SESS_IDLE) {
-                               struct session *sess = srv_conn->owner;
-
-                               srv_conn->flags &= ~CO_FL_SESS_IDLE;
-                               sess->idle_conns--;
-                       }
-               }
-       }
 
        if (reuse) {
                if (srv_conn->mux) {