Class: Syntropy::Storage::ConnectionPool
- Inherits:
-
Object
- Object
- Syntropy::Storage::ConnectionPool
- Defined in:
- lib/syntropy/storage/connection_pool.rb
Overview
ConnectionPool implements concurrent access to an SQLite database.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #close ⇒ Object
- #execute(sql) ⇒ Object
-
#initialize(machine, fn, max_conn) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #query(sql) ⇒ Object
- #with_db ⇒ Object
Constructor Details
#initialize(machine, fn, max_conn) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
11 12 13 14 15 16 17 18 |
# File 'lib/syntropy/storage/connection_pool.rb', line 11 def initialize(machine, fn, max_conn) @machine = machine @fn = fn @count = 0 @max_conn = max_conn @queue = UM::Queue.new @key = :"connection_pool_#{object_id}" end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
9 10 11 |
# File 'lib/syntropy/storage/connection_pool.rb', line 9 def count @count end |
Instance Method Details
#close ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/syntropy/storage/connection_pool.rb', line 44 def close while @queue.count > 0 db = @machine.shift(@queue) db.close @count -= 1 end end |
#execute(sql) ⇒ Object
40 41 42 |
# File 'lib/syntropy/storage/connection_pool.rb', line 40 def execute(sql, *, **) with_db { it.execute(sql, *, **) } end |
#query(sql) ⇒ Object
36 37 38 |
# File 'lib/syntropy/storage/connection_pool.rb', line 36 def query(sql, *, **, &) with_db { it.query(sql, *, **, &) } end |
#with_db ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/syntropy/storage/connection_pool.rb', line 20 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 |