rails/rails — pull request #7675 by jrochkind (2012-09-17 21:36:09+00:00)
Title: backport fair connection pool 02b2335563 to 3-2-stable

In master, @pmahoney's 02b2335563 causes contention for connections in the ConnectionPool to be fair (first-waiting, first-satisfied) which is important for reasonable behavior in a high-contention short-checkout scenario. See also issue #6492 .

I have been using a hacky local monkey patched backport of that patch in my rails 3.2.x app for several months now -- and it's performing admirably. My app that had so much trouble in rails 3.2.x in the sorts of scenarios the patch is designed to fix -- now runs awesomely, as expected.

So I think it's valuable to backport this to 3-2-stable. (A couple others have asked me about these issues in 3.2). Which is what this pull request is.

I manually prepared the backport -- master had enough other semantic and implementation diffs from 3-2-stable that it was a bit tricky to figure out the right way to do this (and this stuff is confusing). But I think I got it right.

Note one significant difference in this backport patch from master: 3-2 still tries to rescue 'leaked' connections in #checkout, if no connections are available, `clear_stale_cached_connections!` is called. This behavior is still preserved with this backport in 3-2-stable, adding the `clear_stale_cached_connections!` check in the new `acquire_connection`. (When I forgot to do this, I indeed got failing tests in 3-2-stable `test_checkout_behaviour`, which passed again when I made sure `clear_stale_cached_connections!` still happened when required).

I am relatively confident I've gotten everything right, although this stuff is admittedly quite tricky.

@tenderlove, @rafaelfranca or anyone else with eyes on AR, appreciate feedback and/or merge, thanks!