Class: Syntropy::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/syntropy/connection_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, fn, max_conn) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



9
10
11
12
13
14
15
16
# File 'lib/syntropy/connection_pool.rb', line 9

def initialize(machine, fn, max_conn)
  @machine = machine
  @fn = fn
  @count = 0
  @max_conn = max_conn
  @queue = UM::Queue.new
  @key = :"db_#{fn}"
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/syntropy/connection_pool.rb', line 7

def count
  @count
end

Instance Method Details

#execute(sql) ⇒ Object



38
39
40
# File 'lib/syntropy/connection_pool.rb', line 38

def execute(sql, *, **)
  with_db { it.execute(sql, *, **) }
end

#query(sql) ⇒ Object



34
35
36
# File 'lib/syntropy/connection_pool.rb', line 34

def query(sql, *, **, &)
  with_db { it.query(sql, *, **, &) }
end

#with_dbObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/syntropy/connection_pool.rb', line 18

def with_db
  if (db = Thread.current[@key])
    @machine.snooze
    return yield(db)
  end

  db = checkout
  begin
    Thread.current[@key] = db
    yield(db)
  ensure
    Thread.current[@key] = nil
    checkin(db)
  end
end