-
-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommendation on connection recycling #1103
Comments
the easiest way to reset all connections would be to close all of them via |
not sure what you mean by reseting a connection? Also if you close individual connection pool should track that it's closed and remove from list of active connections |
Also not sure I follow here. Connections are never recreated, wether when managed by pool or not. When connection is closed there is no way to re-attach it again. It notifies all pending commands about error ( if closed forcefully ) and thats it |
I'm concerned that if I close out the pool, any function that has borrowed a connection from it at the time, would abruptly disconnect. I'm not sure if there's a way to drain the connections from a pool and say close it when all the connections are free, as the application starts using the newly created pool. By "resetting connection" I meant closing and reopening (mostly to get the updated DNS info). By
every 15 minutes, it's possible that every time |
pool.end() waits until last connection is closed, example let pool = mysql.createPool({...});
pool.query('select sleep(1)');
pool.query('select sleep(1)');
pool.query('select sleep(1)');
pool.end();
pool = mysql.createPool({...}); all 3 selects will be processed in the background while you can immediately use new pool |
Interesting, thanks for that input. I think recreating the pool could be the solution in that case. I'll try that. A follow-up suggestion (taken from the handbook link shared in the original description):
Follow-up reference: Full aurora support could be a different issue, but I'm suggesting that this could be a potential feature that every connection in the pool has a lifetime and is closed after that time automatically and a new connection is created lazily. Sorry about the segue. Let me know if that's interesting. I'll update this issue once I confirm recreating the pool helps in recycling connections. |
I'll need to check what's the status of pool connection |
@sidorares Did you get a chance to check on the https://github.com/sidorares/node-mysql2/blob/master/lib/pool_connection.js I also tested (and stress-tested) the I think there could be a better approach here as you suggested (and some other alternatives):
Let me know what do you think. Open to any other suggestions for Aurora balancing (any reference/inspirations from MariaDB Smart Driver suggested in AWS docs, as shared in comments above). |
We have the very same use case with AWS Aurora (dynamic number of read replicas so in order to use both connection pool and discover new hosts, we need to close the connection after some time and recreate new with refreshed DNS). The only viable solution seems (as @nitsnwits said) adding maxConnectionLifetime option per connection that will be checked when the connection is released. We already have several java applications using the very same "functionality" and it seems like it should not be hard to add when I look at mysqljs/mysql#2218 which solves a very similar issue. @sidorares Would you be open for such change? |
Is't might be already implemented? @sidorares |
It would be great to see this or a similar add to the mysql2 pool to make it more cluster aware and "Aurora friendly". When we horizontally scale our Aurora nodes the pool doesn't pick the new nodes up in a timely fashion as idle connections don't get regularly reestablished in order to query the Aurora endpoint to find the additional nodes. |
Any updates on this for mysql2? |
Idle connections can be released by setting Although it would be great to have the option to set a maximum lifetime for the connections. Along with a minimum-maximum pool configuration. |
Based on the recommendation from AWS Aurora MySQL DBA Handbook
Can you please suggest a good way to recycle connections in a pool? Aurora has DNS based endpoints and any DNS changes do not reflect in the connections right away. Moreover, if there are multiple readers, creating a pool doesn't uniformly distribute the connection load amongst the readers. If the connections are periodically reset (closed/reopened) before application accesses it, in the longer run, it can fully utilize multiple readers for read-scalability. I did not find a way where I can get all the connections from the pool and reset them. I can potentially loop through
_allConnections.length
, keep some state based onconnectionId
, but there's no way to free a connection and not get it back again. If I recreate the pool, that seems very expensive to perform every 15 minutes and there could be other instances/functions using the pool to get connections at the time it's recreated. I could potentially get a connection from the pool and remove it from the pool and close it. Would the pool recreate the connection lazily again in that case? It still won't guarantee that every time this operation is run, the same connection is not closed and recreated.Any suggestions are welcome, happy to create a PR if this could be a useful feature in longer run.
The text was updated successfully, but these errors were encountered: