Skip to content

Switch to LIFO for the connection pool - #28742

Merged
matthewd merged 3 commits into
rails:masterfrom
quixoten:stack_conn_pool
Nov 17, 2017
Merged

matthewd merged 3 commits into
rails:masterfrom
quixoten:stack_conn_pool

Conversation

@quixoten

Copy link
Copy Markdown
Contributor

Summary

Using a FIFO for the connection pool can lead to issues when there are
upstream components (pgbouncer, haproxy, etc.) that terminate
connections that are idle after a period of time. Switching to a LIFO
reduces the probability that a thread will checkout a connection that is
about to be closed by an idle timeout in an upstream component.

Other Information

I would also be interested in adding an idle timeout and a max lifetime into the connection pool itself. Putting the idle timeout in the connection pool will eliminate the possibility that an upstream closes a connection as its being checked out by a thread. Adding the max lifetime would create a way to drain connections from an upstream component. I wanted to start with this small change to start the discussion and gauge the receptiveness to changes in this part of the code base.

Using a FIFO for the connection pool can lead to issues when there are
upstream components (pgbouncer, haproxy, etc.) that terminate
connections that are idle after a period of time. Switching to a LIFO
reduces the probability that a thread will checkout a connection that is
about to be closed by an idle timeout in an upstream component.
@rails-bot

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @senny (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@quixoten

Copy link
Copy Markdown
Contributor Author

This is loosely related to #23815.

@matthewd

Copy link
Copy Markdown
Member

Hi!

A LIFO queue is a stack, so some renaming may be called for. OTOH, it still feels a bit more queuey than stacky, conceptually, to me. Hmm. 😕

Naming notwithstanding, this invalidates the "could be using stdlib" comment.

Without actually going and checking, I suspect we're better off using push & pop instead of unshift & shift, if we're only/primarily working from one end.

And as we're now declaring that we care, we should add a test demonstrating that the connections get reused in the right order.

Seems a fair change in principle. 👍🏻

r? @matthewd
cc @thedarkone

@matthewd matthewd assigned matthewd and unassigned senny Apr 12, 2017
@matthewd

Copy link
Copy Markdown
Member

Sorry I took ages to come back to this. 😔

If you're interested in pursuing something along the lines of #23815, I do think that could be interesting. My current implementation manages to feel both overcomplicated and simplistic, so I'm open to thoughts on better approaches. Switching to LIFO makes some new options available, for a start.

@matthewd
matthewd merged commit eed3d3f into rails:master Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants