MEDIUM: sessions: Keep track of which connections are idle.

Instead of keeping track of the number of connections we're responsible for,
keep track of the number of connections we're responsible for that we are
currently considering idling (ie that we are not using, they may be in use
by other sessions), that way we can actually reuse connections when we have
more connections than the max configured.
diff --git a/include/proto/connection.h b/include/proto/connection.h
index 28e7580..79722d8 100644
--- a/include/proto/connection.h
+++ b/include/proto/connection.h
@@ -676,7 +676,8 @@
 	/* Remove ourself from the session's connections list, if any. */
 	if (!LIST_ISEMPTY(&conn->session_list)) {
 		struct session *sess = conn->owner;
-		sess->resp_conns--;
+		if (conn->flags & CO_FL_SESS_IDLE)
+			sess->idle_conns--;
 		session_unown_conn(sess, conn);
 	}
 
diff --git a/include/proto/session.h b/include/proto/session.h
index c34866d..d54e945 100644
--- a/include/proto/session.h
+++ b/include/proto/session.h
@@ -112,7 +112,6 @@
 		LIST_INIT(&srv_list->conn_list);
 		LIST_ADDQ(&sess->srv_list, &srv_list->srv_list);
 	}
-	sess->resp_conns++;
 	LIST_ADDQ(&srv_list->conn_list, &conn->session_list);
 	return 1;
 }
@@ -120,17 +119,19 @@
 /* Returns 0 if the session can keep the idle conn, -1 if it was destroyed, or 1 if it was added to the server list */
 static inline int session_check_idle_conn(struct session *sess, struct connection *conn)
 {
-	if (sess->resp_conns > sess->fe->max_out_conns) {
+	if (sess->idle_conns > sess->fe->max_out_conns) {
 		/* We can't keep the connection, let's try to add it to the server idle list */
 		session_unown_conn(sess, conn);
 		conn->owner = NULL;
-		sess->resp_conns--;
 		if (!srv_add_to_idle_list(objt_server(conn->target), conn)) {
 			/* The server doesn't want it, let's kill the connection right away */
 			conn->mux->destroy(conn);
 			return -1;
 		}
 		return 1;
+	} else {
+		conn->flags |= CO_FL_SESS_IDLE;
+		sess->idle_conns++;
 	}
 	return 0;
 }
diff --git a/include/types/connection.h b/include/types/connection.h
index f1fd7a0..b273ece 100644
--- a/include/types/connection.h
+++ b/include/types/connection.h
@@ -196,6 +196,8 @@
 	/* This flag is used to know that a PROXY protocol header was sent by the client */
 	CO_FL_RCVD_PROXY    = 0x20000000,
 
+	/* The connection is unused by its owner */
+	CO_FL_SESS_IDLE     = 0x40000000,
 	/* unused : 0x40000000 */
 
 	/* This last flag indicates that the transport layer is used (for instance
diff --git a/include/types/session.h b/include/types/session.h
index 1675f8f..328372d 100644
--- a/include/types/session.h
+++ b/include/types/session.h
@@ -55,7 +55,7 @@
 	struct vars vars;               /* list of variables for the session scope. */
 	struct task *task;              /* handshake timeout processing */
 	long t_handshake;               /* handshake duration, -1 = not completed */
-	int resp_conns;                 /* Number of connections we're currently responsible for */
+	int idle_conns;                 /* Number of connections we're currently responsible for that we are not using */
 	struct list srv_list;           /* List of servers and the connections the session is currently responsible for */
 };