Class: ActiveRecord::Bogacs::FalsePool
- Inherits:
-
Object
- Object
- ActiveRecord::Bogacs::FalsePool
- Includes:
- PoolSupport, ThreadSafe::Synchronized
- Defined in:
- lib/active_record/bogacs/false_pool.rb
Instance Attribute Summary collapse
-
#automatic_reconnect ⇒ Object
Returns the value of attribute automatic_reconnect.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Attributes included from PoolSupport
Instance Method Summary collapse
-
#active_connection? ⇒ Boolean
Is there an open connection that is being used for the current thread?.
-
#checkin(conn, released = nil) ⇒ Object
Check-in a database connection back into the pool, indicating that you no longer need this connection.
-
#checkout ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Check-out a database connection from the pool, indicating that you want to use it.
- #checkout_timeout ⇒ Object
-
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
-
#clear_stale_cached_connections! ⇒ Object
Return any checked-out connections back to the pool by threads that are no longer alive.
-
#connected? ⇒ Boolean
Returns true if a connection has already been opened.
-
#connection ⇒ Object
Retrieve the connection associated with the current thread, or call #checkout to obtain one if necessary.
- #connections ⇒ Object
-
#discard! ⇒ Object
:nodoc:.
-
#disconnect! ⇒ Object
Disconnects all connections in the pool, and clears the pool.
- #flush(minimum_idle = nil) ⇒ Object
- #flush! ⇒ Object
-
#initialize(spec) ⇒ FalsePool
constructor
A new instance of FalsePool.
- #reap ⇒ Object
- #reaper ⇒ Object
-
#release_connection(owner_thread = Thread.current) ⇒ Object
Signal that the thread is finished with the current connection.
-
#remove(conn) ⇒ Object
Remove a connection from the connection pool.
- #stat ⇒ Object
-
#verify_active_connections! ⇒ Object
Verify active connections and remove and disconnect connections associated with stale threads.
-
#with_connection ⇒ Object
If a connection already exists yield it to the block.
Methods included from PoolSupport
#current_connection_id, included, #lock_thread=, #new_connection
Constructor Details
#initialize(spec) ⇒ FalsePool
Returns a new instance of FalsePool.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/active_record/bogacs/false_pool.rb', line 21 def initialize(spec) super() @spec = spec @size = nil @automatic_reconnect = nil @lock_thread = false @thread_cached_conns = ThreadSafe::Map.new @connected = ::Concurrent::AtomicBoolean.new end |
Instance Attribute Details
#automatic_reconnect ⇒ Object
Returns the value of attribute automatic_reconnect.
17 18 19 |
# File 'lib/active_record/bogacs/false_pool.rb', line 17 def automatic_reconnect @automatic_reconnect end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
19 20 21 |
# File 'lib/active_record/bogacs/false_pool.rb', line 19 def size @size end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
19 20 21 |
# File 'lib/active_record/bogacs/false_pool.rb', line 19 def spec @spec end |
Instance Method Details
#active_connection? ⇒ Boolean
Is there an open connection that is being used for the current thread?
51 52 53 54 |
# File 'lib/active_record/bogacs/false_pool.rb', line 51 def active_connection? connection_id = current_connection_id(current_thread) @thread_cached_conns[connection_id] end |
#checkin(conn, released = nil) ⇒ Object
Check-in a database connection back into the pool, indicating that you no longer need this connection.
object, which was obtained earlier by calling #checkout on this pool
175 176 177 178 |
# File 'lib/active_record/bogacs/false_pool.rb', line 175 def checkin(conn, released = nil) release(conn) unless released _run_checkin_callbacks(conn) end |
#checkout ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Check-out a database connection from the pool, indicating that you want to use it. You should call #checkin when you no longer need this.
160 161 162 163 164 165 166 167 |
# File 'lib/active_record/bogacs/false_pool.rb', line 160 def checkout conn = checkout_new_connection # acquire_connection synchronize do conn.lease _run_checkout_callbacks(conn) # checkout_and_verify(conn) end conn end |
#checkout_timeout ⇒ Object
38 |
# File 'lib/active_record/bogacs/false_pool.rb', line 38 def checkout_timeout; end |
#clear_reloadable_connections! ⇒ Object
Clears the cache which maps classes.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/active_record/bogacs/false_pool.rb', line 114 def clear_reloadable_connections! synchronize do @connected.make_false connections = @thread_cached_conns.values @thread_cached_conns.clear connections.each do |conn| if conn.in_use? conn.steal! checkin conn end conn.disconnect! if conn.requires_reloading? end end end |
#clear_stale_cached_connections! ⇒ Object
Return any checked-out connections back to the pool by threads that are no longer alive.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/active_record/bogacs/false_pool.rb', line 144 def clear_stale_cached_connections! keys = Thread.list.find_all { |t| t.alive? }.map { |t| current_connection_id(t) } keys = @thread_cached_conns.keys - keys keys.each do |key| if conn = @thread_cached_conns[key] checkin conn, true # no release @thread_cached_conns.delete(key) end end end |
#connected? ⇒ Boolean
Returns true if a connection has already been opened.
79 |
# File 'lib/active_record/bogacs/false_pool.rb', line 79 def connected?; @connected.true? end |
#connection ⇒ Object
Retrieve the connection associated with the current thread, or call #checkout to obtain one if necessary.
#connection can be called any number of times; the connection is held in a hash keyed by the thread id.
45 46 47 48 |
# File 'lib/active_record/bogacs/false_pool.rb', line 45 def connection connection_id = current_connection_id(current_thread) @thread_cached_conns[connection_id] ||= checkout end |
#connections ⇒ Object
82 |
# File 'lib/active_record/bogacs/false_pool.rb', line 82 def connections; @thread_cached_conns.values end |
#discard! ⇒ Object
:nodoc:
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/active_record/bogacs/false_pool.rb', line 101 def discard! # :nodoc: synchronize do return if @thread_cached_conns.nil? # already discarded @connected.make_false connections.each do |conn| conn.discard! end @thread_cached_conns = nil end end |
#disconnect! ⇒ Object
Disconnects all connections in the pool, and clears the pool.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_record/bogacs/false_pool.rb', line 85 def disconnect! synchronize do @connected.make_false connections = @thread_cached_conns.values @thread_cached_conns.clear connections.each do |conn| if conn.in_use? conn.steal! checkin conn end conn.disconnect! end end end |
#flush(minimum_idle = nil) ⇒ Object
191 192 193 |
# File 'lib/active_record/bogacs/false_pool.rb', line 191 def flush(minimum_idle = nil) # we do not really manage the connection pool end |
#flush! ⇒ Object
195 196 197 198 |
# File 'lib/active_record/bogacs/false_pool.rb', line 195 def flush! reap flush(-1) end |
#reap ⇒ Object
187 188 189 |
# File 'lib/active_record/bogacs/false_pool.rb', line 187 def reap # we do not really manage the connection pool - nothing to do ... end |
#reaper ⇒ Object
35 |
# File 'lib/active_record/bogacs/false_pool.rb', line 35 def reaper; end |
#release_connection(owner_thread = Thread.current) ⇒ Object
Signal that the thread is finished with the current connection. #release_connection releases the connection-thread association and returns the connection to the pool.
59 60 61 62 |
# File 'lib/active_record/bogacs/false_pool.rb', line 59 def release_connection(owner_thread = Thread.current) conn = @thread_cached_conns.delete(current_connection_id(owner_thread)) checkin conn if conn end |
#remove(conn) ⇒ Object
Remove a connection from the connection pool. The connection will remain open and active but will no longer be managed by this pool.
182 183 184 |
# File 'lib/active_record/bogacs/false_pool.rb', line 182 def remove(conn) release(conn) end |
#stat ⇒ Object
200 201 202 203 204 |
# File 'lib/active_record/bogacs/false_pool.rb', line 200 def stat { connections: connections.size } end |
#verify_active_connections! ⇒ Object
Verify active connections and remove and disconnect connections associated with stale threads.
134 135 136 137 138 139 |
# File 'lib/active_record/bogacs/false_pool.rb', line 134 def verify_active_connections! synchronize do clear_stale_cached_connections! @thread_cached_conns.values.each(&:verify!) end end |
#with_connection ⇒ Object
If a connection already exists yield it to the block. If no connection exists checkout a connection, yield it to the block, and checkin the connection when finished.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_record/bogacs/false_pool.rb', line 67 def with_connection connection_id = current_connection_id unless conn = @thread_cached_conns[connection_id] conn = connection fresh_connection = true end yield conn ensure release_connection if fresh_connection end |