MINOR: connection: Use a dedicated function to look for a session's connection

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.
diff --git a/include/haproxy/session.h b/include/haproxy/session.h
index 24f8e5d..5a46c4f 100644
--- a/include/haproxy/session.h
+++ b/include/haproxy/session.h
@@ -145,6 +145,35 @@
 	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 */
 
 /*
diff --git a/src/backend.c b/src/backend.c
index f346af5..8556d4c 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -1197,10 +1197,8 @@
 	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 @@
 	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 @@
 			 * pick.
 			 */
 			if (srv_conn) {
-				reuse_orphan = 1;
 				reuse = 1;
 				srv_conn->flags &= ~CO_FL_LIST_MASK;
 			}
@@ -1356,19 +1341,6 @@
 		}
 
 	}
-	/* 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) {