Skip to content
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

Is it correct to cache this per-fiber? #168

Open
ioquatix opened this issue Oct 28, 2022 · 2 comments
Open

Is it correct to cache this per-fiber? #168

ioquatix opened this issue Oct 28, 2022 · 2 comments

Comments

@ioquatix
Copy link

ioquatix commented Oct 28, 2022

if ::Thread.current[@key]

Looking at this code, I noticed it caches a connection per fiber.

I'm a little concerned that someone might expect:

pool.with |connection1|
  connection1 ... # start a pipeline
  pool.with |connection2|
    connection2.get(...) # get some value
  end
  connection1 ... # end/commit pipeline
end

I don't understand why connection1 and connection2 should be the same connection. I would probably expect them to be distinct, otherwise the state from one might leak into another. It's probably even more odd if this operation is inside a library, that might be expecting a clean connection, but is instead gets spooky action at a distance with a user connection. This could result in some pretty hard to debug issues.

@mperham
Copy link
Owner

mperham commented Oct 28, 2022

Yes this is intentional: while a connection is checked out, further checkouts will return the same connection. It's had this semantic for many years now.

@ioquatix
Copy link
Author

Based on your response, there are two things I think we can do:

  • Change the behaviour so that the same connection is not returned. Because a connection pool is a cache, I don't think anyone should be depending on this behaviour, i.e. a connection pool could be validly implemented by just returning a completely new connection every time (but would be slow), OR
  • Add documentation regarding the situations in which with do |connection| can actually return a connection which is already in use and may have state (e.g. transactions, pipeline, etc) attached to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants