diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 8fe0a9c53..12e5a6016 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -3132,34 +3132,29 @@ MySQL_Connection * MySrvConnList::get_random_MyConn(MySQL_Session *sess, bool ff unsigned int pct_max_connections = (3 * mysrvc->max_connections) / 4; unsigned int connections_to_free = 0; - if (pct_max_connections <= (conns_free + conns_used)) { - connections_to_free = (conns_free + conns_used) - pct_max_connections; - connections_to_free = connections_to_free == 0 ? 1 : connections_to_free; - } + if (conns_free >= 1) { + // connection cleanup is triggered when connectinos exceed 3/4 of the total + // allowed max connections, this cleanup ensures that at least *one connection* + // will be freed. + if (pct_max_connections <= (conns_free + conns_used)) { + connections_to_free = (conns_free + conns_used) - pct_max_connections; + if (connections_to_free == 0) connections_to_free = 1; + } - while (conns_free && connections_to_free) { - MySQL_Connection* conn = mysrvc->ConnectionsFree->remove(0); - delete conn; + while (conns_free && connections_to_free) { + MySQL_Connection* conn = mysrvc->ConnectionsFree->remove(0); + delete conn; - conns_free = mysrvc->ConnectionsFree->conns_length(); - connections_to_free -= 1; + conns_free = mysrvc->ConnectionsFree->conns_length(); + connections_to_free -= 1; + } } - conns_used = mysrvc->ConnectionsUsed->conns_length(); - - if (mysrvc->max_connections > (conns_used + conns_free)) { - // we must create a new connection - conn = new MySQL_Connection(); - conn->parent=mysrvc; - __sync_fetch_and_add(&MyHGM->status.server_connections_created, 1); - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL Connection %p, server %s:%d\n", conn, conn->parent->address, conn->parent->port); - } else { - // no suitable connection was found, and the number of - // used connections already reached 'max_connections' - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "No suitable connection found, 'max_connections' reached for server %s:%d\n", conn->parent->address, conn->parent->port); - __sync_fetch_and_add(&MyHGM->status.server_connections_delayed, 1); - return NULL; - } + // we must create a new connection + conn = new MySQL_Connection(); + conn->parent=mysrvc; + __sync_fetch_and_add(&MyHGM->status.server_connections_created, 1); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL Connection %p, server %s:%d\n", conn, conn->parent->address, conn->parent->port); } break; case 1: //tracked options are OK , but CHANGE USER is required