Module: Switchman::ActiveRecord::ConnectionPool
- Defined in:
- lib/switchman/active_record/connection_pool.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #checkout_new_connection ⇒ Object
- #clear_idle_connections!(since_when) ⇒ Object
- #connection ⇒ Object
- #default_schema ⇒ Object
- #release_connection(with_id = nil) ⇒ Object
- #switch_database(conn) ⇒ Object
Instance Attribute Details
#shard ⇒ Object
6 7 8 |
# File 'lib/switchman/active_record/connection_pool.rb', line 6 def shard @shard || Shard.default end |
Instance Method Details
#checkout_new_connection ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/switchman/active_record/connection_pool.rb', line 18 def checkout_new_connection # TODO: this might be a threading issue spec.config[:shard_name] = self.shard.name conn = super conn.shard = self.shard conn end |
#clear_idle_connections!(since_when) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/switchman/active_record/connection_pool.rb', line 46 def clear_idle_connections!(since_when) synchronize do @connections.reject! do |conn| if conn.last_query_at < since_when && !conn.in_use? conn.disconnect! true else false end end @available.clear @connections.each do |conn| @available.add conn end end end |
#connection ⇒ Object
27 28 29 30 31 |
# File 'lib/switchman/active_record/connection_pool.rb', line 27 def connection conn = super switch_database(conn) if conn.shard != self.shard conn end |
#default_schema ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/switchman/active_record/connection_pool.rb', line 10 def default_schema raise "Not postgres!" unless self.spec.config[:adapter] == 'postgresql' connection unless @schemas # default shard will not switch databases immediately, so it won't be set yet @schemas ||= connection.current_schemas @schemas.first end |
#release_connection(with_id = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/switchman/active_record/connection_pool.rb', line 33 def release_connection(with_id = nil) with_id ||= if ::Rails.version >= '5' Thread.current else current_connection_id end super(with_id) if spec.config[:idle_timeout] clear_idle_connections!(Time.now - spec.config[:idle_timeout].to_i) end end |
#switch_database(conn) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/switchman/active_record/connection_pool.rb', line 63 def switch_database(conn) if !@schemas && conn.adapter_name == 'PostgreSQL' && !self.shard.database_server.config[:shard_name] @schemas = conn.current_schemas end spec.config[:shard_name] = self.shard.name case conn.adapter_name when 'MySQL', 'Mysql2' conn.execute("USE #{spec.config[:database]}") when 'PostgreSQL' if conn.schema_search_path != spec.config[:schema_search_path] conn.schema_search_path = spec.config[:schema_search_path] end when 'SQLite' # This is an artifact of the adapter modifying the path to be an absolute path when it is instantiated; just let it slide else raise("Cannot switch databases on same DatabaseServer with adapter type: #{conn.adapter_name}. Limit one Shard per DatabaseServer.") end conn.shard = shard end |