amphp/mysql

Connection pool pop() or push() should modify to release idle connection.

xpader opened this issue · 2 comments

In source code, the pop() method invoke idle->shift() to get top of pool list connection to use, and push return it back to the bottom of list.

In timeoutWatcher first check bottom of the list connection, if not idle will do nothing.

If connection pool has expanded at peak, when the traffic drops back to a low peak, as long as there is one query in each idle time, all connection will never released!!! but only one or two connection is enough now.

So I think idle->push() should change to idle->unshift(), then real idle connection in bottom will be released in time, or timeoutWatcher check top, and pop() change to real idle->pop().

There's another problem,

//In Loop::run() or call()
$result = yield $pool->query("SELECT * FROM some_table LIMIT 1");

//if, not while
if (yield $result->advance()) {
    $row = $result->getCurrent();
    //In this code, if not call advance() or nextResultSet() again, next query will create a new connection.
    //yield $result->nextResultSet();
    print_r($row);
    yield $pool->execute("UPDATE some_table SET counter=counter+1 WHERE id=?", [$row['id']);
}