klobuczek / active_node

ActiveRecord style Object Graph Mapping for neo4j
MIT License
14 stars 5 forks source link

Connection Pooling #5

Closed OpenCoderX closed 10 years ago

OpenCoderX commented 10 years ago

Would anyone be interested in connection pooling? If I'm understanding the Neo class correctly, each time Neo.db is called it creates a new connection. I've got connection pooling working in my branch, should I send a pull request?

https://github.com/chrisgogreen/active_node

I added some readme to my branch for using the connection pool. I wrapped @db in an if else to check for a globally defined db connection.

module ActiveNode
  class Neo
    def self.db
     if defined? $db
     @db = $db
     else
     @db ||= Neography::Rest.new
     end
    end
  end
end

If you define $db when your application initializes you can use a configurable connection pool:

require 'connection_pool'
$db = ConnectionPool::Wrapper.new(size: 50, timeout: 15) { Neography::Rest.new }

I'm not sure if using a global is a good idea in this case, but it got me loading data much faster last night. Before I made this change my sidekiq workers were loading about 20 nodes per second(one node per job), right now they are loading about 100.

Maybe instead of checking if a global named $db is defined I could check if any instances of the ConnectionPool class exist?

klobuczek commented 10 years ago

Could you add a spec which exposes the time difference before and after using a connection pool.

OpenCoderX commented 10 years ago

I'll do that.

klobuczek commented 10 years ago

Please see my response in https://github.com/maxdemarzi/neography/issues/153