class ReconnectionPool

Public Class Methods

connect(options) click to toggle source
# File lib/reconnection_pool.rb, line 5
def connect(options)
  @pool ||= Queue.new
  @options = set_default_options(options)

  @options[:pool].times do
    @pool << r.connect(
      host: @options[:host],
      port: @options[:port],
      db: @options[:db],
      auth_key: @options[:auth_key]
    )
  end
end
empty?() click to toggle source
# File lib/reconnection_pool.rb, line 59
def empty?
  @pool ||= Queue.new

  @pool.empty?
end
get() click to toggle source
# File lib/reconnection_pool.rb, line 33
def get
  conn = @pool.pop
  if conn.closed?
    #If the database goes down, you need to be sure that the object knows its state
    conn.close
    begin
      #It keeps the last configuration used
      conn.connect
    rescue Errno::ECONNREFUSED
      # Just loggin', nothing more to do in this case
      p "Can't connect to the database"
    end
  end

  conn
end
return(conn) click to toggle source
# File lib/reconnection_pool.rb, line 29
def return(conn)
  @pool << conn
end
run(lazy_query) click to toggle source

Executes a query and returns the connection to the database

# File lib/reconnection_pool.rb, line 51
def run(lazy_query)
  conn = self.get
  handle = lazy_query.run(conn)

  self.return conn
  handle
end
set_default_options(options) click to toggle source
# File lib/reconnection_pool.rb, line 19
def set_default_options(options)
  options.tap do |o|
    o[:pool] ||= 1
    o[:host] ||= 'localhost'
    o[:port] ||= 28015
    o[:db] ||= 'test'
    o[:auth_key] ||= nil
  end
end