Class: ActiveRecord::Bogacs::FalsePool

Inherits:
Object
  • Object
show all
Includes:
PoolSupport, ThreadSafe::Synchronized
Defined in:
lib/active_record/bogacs/false_pool.rb

Instance Attribute Summary collapse

Attributes included from PoolSupport

#schema_cache

Instance Method Summary collapse

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_reconnectObject

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

#sizeObject (readonly)

Returns the value of attribute size.



19
20
21
# File 'lib/active_record/bogacs/false_pool.rb', line 19

def size
  @size
end

#specObject (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?

Returns:

  • (Boolean)


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

Parameters:

  • conn (ActiveRecord::ConnectionAdapters::AbstractAdapter)

    connection

See Also:



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

#checkoutActiveRecord::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.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)

Raises:

  • (ActiveRecord::ConnectionTimeoutError)

    no connection can be obtained from the pool



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_timeoutObject



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.

Returns:

  • (Boolean)


79
# File 'lib/active_record/bogacs/false_pool.rb', line 79

def connected?; @connected.true? end

#connectionObject

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

#connectionsObject



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

#reapObject



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

#reaperObject



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

#statObject



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_connectionObject

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