jeremyevans/sequel — pull request #816 by dmuso (2014-05-19 04:28:28+00:00)
Title: valid_connection? now rescues gracefully on Timeout::Error

We use Sequel in production (using the Mysql2 adapter, threaded connection pooling and the connection_validator extension) for several micro services. Our services run for a long time, and we have noticed that if something happens on our network to make MySQL go away, Sequel does not realise that the connection in the pool is bad and keeps attempting to use it for queries.

The connection_validator extension uses valid_connection? as a condition on whether to remove the current connection from the pool and re-establish a new one. However, I've noticed that when the Mysql2 adapter returns a Timeout::Error, Sequel does not rescue this error. Therefore, Sequel returns true for valid_connection? even when the Timeout::Error is being raised by the adapter.

I've written an example spec and a patch to specifically rescue this exception, and from testing with some code that behaves similarly to our micro services, I'm fairly confident this fixes the bug we're seeing in production.

I've tried my best to keep with the style of code already in place, if it needs any tweaks, please let me know.

Thanks,
Dan