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:.
-
#discarded? ⇒ Boolean
: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
#connection_cache_key, 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?
56 57 58 59 |
# File 'lib/active_record/bogacs/false_pool.rb', line 56 def active_connection? connection_id = connection_cache_key(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
184 185 186 187 |
# File 'lib/active_record/bogacs/false_pool.rb', line 184 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.
169 170 171 172 173 174 175 176 |
# File 'lib/active_record/bogacs/false_pool.rb', line 169 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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_record/bogacs/false_pool.rb', line 123 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.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_record/bogacs/false_pool.rb', line 153 def clear_stale_cached_connections! keys = Thread.list.find_all { |t| t.alive? }.map { |t| connection_cache_key(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.
84 |
# File 'lib/active_record/bogacs/false_pool.rb', line 84 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.
50 51 52 53 |
# File 'lib/active_record/bogacs/false_pool.rb', line 50 def connection connection_id = connection_cache_key(current_thread) @thread_cached_conns[connection_id] ||= checkout end |
#connections ⇒ Object
87 |
# File 'lib/active_record/bogacs/false_pool.rb', line 87 def connections; @thread_cached_conns.values end |
#discard! ⇒ Object
:nodoc:
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_record/bogacs/false_pool.rb', line 106 def discard! # :nodoc: synchronize do return if discarded? @connected.make_false connections.each do |conn| conn.discard! end @thread_cached_conns = nil end end |
#discarded? ⇒ Boolean
:nodoc:
118 119 120 |
# File 'lib/active_record/bogacs/false_pool.rb', line 118 def discarded? # :nodoc: @thread_cached_conns.nil? end |
#disconnect! ⇒ Object
Disconnects all connections in the pool, and clears the pool.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_record/bogacs/false_pool.rb', line 90 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
200 201 202 |
# File 'lib/active_record/bogacs/false_pool.rb', line 200 def flush(minimum_idle = nil) # we do not really manage the connection pool end |
#flush! ⇒ Object
204 205 206 207 |
# File 'lib/active_record/bogacs/false_pool.rb', line 204 def flush! reap flush(-1) end |
#reap ⇒ Object
196 197 198 |
# File 'lib/active_record/bogacs/false_pool.rb', line 196 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.
64 65 66 67 |
# File 'lib/active_record/bogacs/false_pool.rb', line 64 def release_connection(owner_thread = Thread.current) conn = @thread_cached_conns.delete(connection_cache_key(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.
191 192 193 |
# File 'lib/active_record/bogacs/false_pool.rb', line 191 def remove(conn) release(conn) end |
#stat ⇒ Object
209 210 211 212 213 |
# File 'lib/active_record/bogacs/false_pool.rb', line 209 def stat { connections: connections.size } end |
#verify_active_connections! ⇒ Object
Verify active connections and remove and disconnect connections associated with stale threads.
143 144 145 146 147 148 |
# File 'lib/active_record/bogacs/false_pool.rb', line 143 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.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_record/bogacs/false_pool.rb', line 72 def with_connection connection_id = connection_cache_key unless conn = @thread_cached_conns[connection_id] conn = connection fresh_connection = true end yield conn ensure release_connection if fresh_connection end |