How can I explicitly call close on all stale connections in the pool (including the ones in _in_use)
> Although we have written a decorator to explicitly call close on db connections after each call. There are instances when it remains open.

This sounds like you have a bug in your code or there is some problem in your environment. I'd strongly suggest you look into where and how you're leaking database connections. As a result, any of the mitigation strategies proposed are going to be hacks. Given the leaky connections, perhaps you should not use the pool? Making connections evertime is really heavy in google cloud, because of all the auth in place i guess. So no pooling is not an option. We are actively checking for the bugs, it works for most part but overtime we face this issue.

But is there anyway I can push the connections from _in_use to the usable pool? The entire code for the connection pooling implementation is ~150 lines of code, so I'm sure if you take the time to read and understand it you will be able to subclass/extend it to more aggressively handle your unclosed connections. I've added the requested feature:

* 22e8557e279f5174053ed711f342da8e03fae0c4
* c985636ca13bf8b712d37bf991ece8fdb9a022a8
We are using Pooled Mysql connections on appengine with peewee and flask.
Although we have written a decorator to explicitly call close on db connections after each call. There are instances when it remains open. As a result we are hitting the max connection exceeded error.
Here is the decorator

    def catchdb(dbobj):
       """TODO: Docstring for catchdb.
    
       :dbobj: TODO
       :returns: TODO
    
       """
       def decorator(func):
           @wraps(func)
           def foo(*args, **kwargs):
               result = None
               try:
                   result = func(*args, **kwargs)
               finally:
                   print "Closing current connection"
                   dbobj.close()
                   print "Number of in use connections: ", len(dbobj._in_use)
                   for conn, t in dbobj._in_use.items():
                       if (time.time() - t) > (10 * 60):  # 10 mins seconds before we close the stale connections.
                           print "Closing stale connection ", conn
                           del dbobj._in_use[conn]
               return result
           return foo
       return decorator

The solution we wanted to try is to explicitly close all the inactive connections (in_use for too much time) The problem is dbobj._in_use contains the id of the reference and not the actual reference.

It would be great if anyone can guide us in any right direction to solve this issue, for now we have removed max connection limit.