Class: Postamt::ConnectionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/postamt/connection_handler.rb

Instance Method Summary collapse

Constructor Details

#initializeConnectionHandler

Returns a new instance of ConnectionHandler.



5
6
7
# File 'lib/postamt/connection_handler.rb', line 5

def initialize
  @process_pid = ThreadSafe::Util::AtomicReference.new(nil)
end

Instance Method Details

#active_connections?Boolean

Returns true if there are any active connections among the connection pools that the ConnectionHandler is managing.

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/postamt/connection_handler.rb', line 21

def active_connections?
  self.ensure_ready
  self.connection_pool_list.any?(&:active_connection?)
end

#clear_active_connections!Object

Returns any connections in use by the current thread back to the pool, and also returns connections to the pool cached by threads that are no longer alive.



29
30
31
32
# File 'lib/postamt/connection_handler.rb', line 29

def clear_active_connections!
  self.ensure_ready
  self.connection_pool_list.each(&:release_connection)
end

#clear_all_connections!Object



40
41
42
43
# File 'lib/postamt/connection_handler.rb', line 40

def clear_all_connections!
  self.ensure_ready
  self.connection_pool_list.each(&:disconnect!)
end

#clear_reloadable_connections!Object

Clears the cache which maps classes.



35
36
37
38
# File 'lib/postamt/connection_handler.rb', line 35

def clear_reloadable_connections!
  self.ensure_ready
  self.connection_pool_list.each(&:clear_reloadable_connections!)
end

#connected?(klass) ⇒ Boolean

Returns true if a connection that’s accessible to this class has already been opened.

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/postamt/connection_handler.rb', line 57

def connected?(klass)
  return false if Process.pid != @process_pid.get
  conn = self.retrieve_connection_pool(klass)
  conn && conn.connected?
end

#connection_pool_listObject



14
15
16
17
# File 'lib/postamt/connection_handler.rb', line 14

def connection_pool_list
  self.ensure_ready
  @pools.values
end

#connection_poolsObject



9
10
11
12
# File 'lib/postamt/connection_handler.rb', line 9

def connection_pools
  # See https://github.com/rails/rails/commit/c3ca7ac09e960fa1287adc730e8ddc713e844c37
  Hash[self.connection_pool_list.map { |pool| [pool.spec, pool] }]
end

#remove_connection(owner) ⇒ Object

Only called in ActiveRecord test code, so performance isn’t an issue.



64
65
66
67
68
# File 'lib/postamt/connection_handler.rb', line 64

def remove_connection(owner)
  self.clear_cache
  # Don't return a ConnectionSpecification hash since we've disabled establish_connection anyway
  return nil
end

#retrieve_connection(klass) ⇒ Object

Locate the connection of the nearest super class. This can be an active or defined connection: if it is the latter, it will be opened and set as the active connection for the class it was defined for (not necessarily the current class).



49
50
51
52
53
# File 'lib/postamt/connection_handler.rb', line 49

def retrieve_connection(klass) #:nodoc:
  self.ensure_ready
  pool = self.retrieve_connection_pool(klass)
  (pool && pool.connection) or raise ActiveRecord::ConnectionNotEstablished
end

#retrieve_connection_pool(klass) ⇒ Object

Called by ActiveRecord::ConnectionHandling#connection_pool.



71
72
73
74
# File 'lib/postamt/connection_handler.rb', line 71

def retrieve_connection_pool(klass)
  self.ensure_ready
  self.pool_for(klass)
end