Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This keeps us from having to reconnect to the DB on every query, removing the latency cost of repeated TCP handshakes, TLS negotiations, and TCP slow start. It made a huge difference just running the specs against my local Neo4j instance:
Before
With this PR
It removes the
Model.connection
class method and replaces it with a connection-checkout block for theConnectionPool
object:At the end of the block, the connection is checked back into the connection pool so another fiber can use it immediately. The default capacity for the connection pool is 25, but it will only create them as needed — if you're only ever handling one HTTP request at a time, for example, you'll only have a single connection, but if a second request comes in while the first one's query is still in flight, it'll spin up another connection.
The
pool
shard does allow for assigning the connection to the current fiber, but I don't know if I'd recommend using that functionality. In a web app, theHTTP::Server::Context
will have a new fiber for every request and then it gets thrown away. Rails, on the other hand, uses a pool of persistent threads, which is whyActiveRecord::Base.connection
makes sense there. Doing that here might leak connections as each fiber is thrown away.It's possible that that isn't true and that once the
Neo4j::Bolt::Connection
object goes out of scope and gets GCed its connection will be closed, but even in that case, we'd still be spinning up connections for each request if we used one connection per fiber.