Class: Praxis::Mapper::ConnectionFactories::Sequel
- Inherits:
-
Object
- Object
- Praxis::Mapper::ConnectionFactories::Sequel
- Defined in:
- lib/praxis-mapper/connection_factories/sequel.rb
Instance Method Summary collapse
- #acquire(thread) ⇒ Object
- #checkout(connection_manager) ⇒ Object
-
#initialize(connection: nil, **opts) ⇒ Sequel
constructor
A new instance of Sequel.
- #release(connection_manager, connection) ⇒ Object
Constructor Details
#initialize(connection: nil, **opts) ⇒ Sequel
Returns a new instance of Sequel.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/praxis-mapper/connection_factories/sequel.rb', line 6 def initialize(connection:nil, **opts) raise ArgumentError, 'May not provide both a connection and opts' if connection && !opts.empty? if connection @connection = connection else @connection = ::Sequel.connect(**opts) end # steal timeout values so we can replicate the same timeout behavior @timeout = @connection.pool.instance_variable_get(:@timeout) @sleep_time = @connection.pool.instance_variable_get(:@sleep_time) # connections that we created explicitly @owned_connections = Hash.new end |
Instance Method Details
#acquire(thread) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/praxis-mapper/connection_factories/sequel.rb', line 51 def acquire(thread) # check connection's pool to see if it already has a connection # if so, re-use it. otherwise, acquire a new one and mark that we # "own" it for future releasing. if @connection.pool.send(:owned_connection, thread) return true else conn = @connection.pool.send(:acquire, thread) @owned_connections[thread] = conn true end end |
#checkout(connection_manager) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/praxis-mapper/connection_factories/sequel.rb', line 23 def checkout(connection_manager) # copied from Sequel's ThreadedConnectionPool#hold # to ensure consistent behavior unless acquire(connection_manager.thread) time = Time.now timeout = time + @timeout sleep_time = @sleep_time sleep sleep_time until acquire(connection_manager.thread) raise(::Sequel::PoolTimeout) if Time.now > timeout sleep sleep_time end end @connection end |
#release(connection_manager, connection) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/praxis-mapper/connection_factories/sequel.rb', line 40 def release(connection_manager, connection) # ensure we only release connections we own, in case # we've acquired a connection from Sequel that # is likely still in use. if (@owned_connections.delete(connection_manager.thread)) @connection.pool.send(:sync) do @connection.pool.send(:release,connection_manager.thread) end end end |