Class: ReconnectionPool

Inherits:
Object
  • Object
show all
Extended by:
RethinkDB::Shortcuts
Defined in:
lib/reconnection_pool.rb

Class Method Summary collapse

Class Method Details

.connect(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/reconnection_pool.rb', line 59

def empty?
  @pool ||= Queue.new

  @pool.empty?
end

.getObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



29
30
31
# File 'lib/reconnection_pool.rb', line 29

def return(conn)
  @pool << conn
end

.run(lazy_query) ⇒ Object

Executes a query and returns the connection to the database



51
52
53
54
55
56
57
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
# 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