Skip to content

Commit

Permalink
Merge pull request #3202 from sysown/v2.0.16-3201
Browse files Browse the repository at this point in the history
Closes #3201: Connections not being cleanup in case of 'match_tracked_options' always failing
  • Loading branch information
renecannao authored Dec 27, 2020
2 parents 5124f0e + c88a9a9 commit d6239aa
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions lib/MySQL_HostGroups_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2701,11 +2701,38 @@ MySQL_Connection * MySrvConnList::get_random_MyConn(MySQL_Session *sess, bool ff
// 2 : tracked options are OK , CHANGE USER is not required, but some SET statement or INIT_DB needs to be executed
switch (connection_quality_level) {
case 0: // not found any good connection, tracked options are not OK
// 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);
// we must check if connections need to be freed before
// creating a new connection
{
unsigned int conns_free = mysrvc->ConnectionsFree->conns_length();
unsigned int conns_used = mysrvc->ConnectionsUsed->conns_length();
unsigned int pct_max_connections = (3 * mysrvc->max_connections) / 4;
unsigned int connections_to_free = 0;

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;

conns_free = mysrvc->ConnectionsFree->conns_length();
connections_to_free -= 1;
}
}

// 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
// we may consider creating a new connection
Expand Down

0 comments on commit d6239aa

Please sign in to comment.