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

Instance Method Summary collapse

Methods included from ThreadSafe::Synchronized

#synchronize

Methods included from PoolSupport

#current_connection_id, #new_connection

Constructor Details

#initialize(spec) ⇒ FalsePool

Returns a new instance of FalsePool.



16
17
18
19
20
21
22
23
24
# File 'lib/active_record/bogacs/false_pool.rb', line 16

def initialize(spec)
  @connected = nil

  @spec = spec
  @size = nil
  @automatic_reconnect = nil

  @reserved_connections = ThreadSafe::Map.new #:initial_capacity => @size
end

Instance Attribute Details

#automatic_reconnectObject

Returns the value of attribute automatic_reconnect.



12
13
14
# File 'lib/active_record/bogacs/false_pool.rb', line 12

def automatic_reconnect
  @automatic_reconnect
end

#sizeObject (readonly)

Returns the value of attribute size.



14
15
16
# File 'lib/active_record/bogacs/false_pool.rb', line 14

def size
  @size
end

#specObject (readonly)

Returns the value of attribute spec.



14
15
16
# File 'lib/active_record/bogacs/false_pool.rb', line 14

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)


45
46
47
48
# File 'lib/active_record/bogacs/false_pool.rb', line 45

def active_connection?
  conn = @reserved_connections[current_connection_id]
  conn ? conn.in_use? : false
end

#checkin(conn, do_release = true) ⇒ Object

Check-in a database connection back into the pool, indicating that you no longer need this connection.

conn: an AbstractAdapter object, which was obtained by earlier by calling checkout on this pool.



154
155
156
157
158
159
160
161
# File 'lib/active_record/bogacs/false_pool.rb', line 154

def checkin(conn, do_release = true)
  release(conn) if do_release
  #synchronize do
    _run_checkin_callbacks(conn)
    #release conn
    #@available.add conn
  #end
end

#checkoutObject

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.

This is done by either returning and leasing existing connection, or by creating a new connection and leasing it.

If all connections are leased and the pool is at capacity (meaning the number of currently leased connections is greater than or equal to the size limit set), an ActiveRecord::ConnectionTimeoutError exception will be raised.

Returns: an AbstractAdapter object.

Raises:

  • ConnectionTimeoutError: no connection can be obtained from the pool.



140
141
142
143
144
145
146
147
# File 'lib/active_record/bogacs/false_pool.rb', line 140

def checkout
  #synchronize do
    conn = checkout_new_connection # acquire_connection
    conn.lease
    _run_checkout_callbacks(conn) # checkout_and_verify(conn)
    conn
  #end
end

#checkout_timeoutObject



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

def checkout_timeout; end

#clear_reloadable_connections!Object

Clears the cache which maps classes.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_record/bogacs/false_pool.rb', line 88

def clear_reloadable_connections!
  synchronize do
    @connected = false

    connections = @reserved_connections.values
    @reserved_connections.clear

    connections.each do |conn|
      checkin conn
      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.



115
116
117
118
119
120
121
122
123
124
# File 'lib/active_record/bogacs/false_pool.rb', line 115

def clear_stale_cached_connections!
  keys = Thread.list.find_all { |t| t.alive? }.map(&:object_id)
  keys = @reserved_connections.keys - keys
  keys.each do |key|
    if conn = @reserved_connections[key]
      checkin conn, false # no release
      @reserved_connections.delete(key)
    end
  end
end

#connected?Boolean

Returns true if a connection has already been opened.

Returns:

  • (Boolean)


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

def connected?; @connected 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.



40
41
42
# File 'lib/active_record/bogacs/false_pool.rb', line 40

def connection
  @reserved_connections[current_connection_id] ||= checkout
end

#connectionsObject



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

def connections; @reserved_connections.values end

#disconnect!Object

Disconnects all connections in the pool, and clears the pool.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_record/bogacs/false_pool.rb', line 73

def disconnect!
  synchronize do
    @connected = false

    connections = @reserved_connections.values
    @reserved_connections.clear

    connections.each do |conn|
      checkin conn
      conn.disconnect!
    end
  end
end

#reapObject



170
171
172
# File 'lib/active_record/bogacs/false_pool.rb', line 170

def reap
  # we do not really manage the connection pool - nothing to do ...
end

#reaperObject



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

def reaper; end

#release_connection(with_id = current_connection_id) ⇒ 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.



53
54
55
56
# File 'lib/active_record/bogacs/false_pool.rb', line 53

def release_connection(with_id = current_connection_id)
  conn = @reserved_connections.delete(with_id)
  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.



165
166
167
# File 'lib/active_record/bogacs/false_pool.rb', line 165

def remove(conn)
  release(conn)
end

#verify_active_connections!Object

Verify active connections and remove and disconnect connections associated with stale threads.



105
106
107
108
109
110
# File 'lib/active_record/bogacs/false_pool.rb', line 105

def verify_active_connections!
  synchronize do
    clear_stale_cached_connections!
    @reserved_connections.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.



61
62
63
64
65
66
67
# File 'lib/active_record/bogacs/false_pool.rb', line 61

def with_connection
  connection_id = current_connection_id
  fresh_connection = true unless active_connection?
  yield connection
ensure
  release_connection(connection_id) if fresh_connection
end