Class: AutoReplica::ConnectionHandler
- Inherits:
-
Object
- Object
- AutoReplica::ConnectionHandler
- Defined in:
- lib/activerecord_autoreplica.rb
Overview
The connection handler that wraps the ActiveRecord one. Everything gets forwarded to the wrapped object, but a "spiked" connection adapter gets returned from retrieve_connection.
Direct Known Subclasses
Instance Method Summary collapse
-
#clear_all_connections! ⇒ Object
Disconnect both the original handler AND the read pool.
-
#disconnect_read_pool! ⇒ Object
Close all the connections maintained by the read pool.
-
#finish ⇒ Object
When finishing, releases the borrowed connection back into the pool.
-
#initialize(original_handler, read_pool) ⇒ ConnectionHandler
constructor
a proxy for ActiveRecord::ConnectionAdapters::ConnectionHandler.
- #method_missing(method_name, *args, &blk) ⇒ Object
- #release_read_pool_connection ⇒ Object
-
#respond_to_missing?(method_name) ⇒ Boolean
The duo for method proxying without delegate.
-
#retrieve_connection(for_ar_class) ⇒ Object
Overridden method which gets called by ActiveRecord to get a connection related to a specific ActiveRecord::Base subclass.
Constructor Details
#initialize(original_handler, read_pool) ⇒ ConnectionHandler
a proxy for ActiveRecord::ConnectionAdapters::ConnectionHandler
81 82 83 84 |
# File 'lib/activerecord_autoreplica.rb', line 81 def initialize(original_handler, read_pool) @original_handler = original_handler @read_pool = read_pool end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
113 114 115 |
# File 'lib/activerecord_autoreplica.rb', line 113 def method_missing(method_name, *args, &blk) @original_handler.public_send(method_name, *args, &blk) end |
Instance Method Details
#clear_all_connections! ⇒ Object
Disconnect both the original handler AND the read pool
104 105 106 107 |
# File 'lib/activerecord_autoreplica.rb', line 104 def clear_all_connections! disconnect_read_pool! @original_handler.clear_all_connections! end |
#disconnect_read_pool! ⇒ Object
Close all the connections maintained by the read pool
99 100 101 |
# File 'lib/activerecord_autoreplica.rb', line 99 def disconnect_read_pool! @read_pool.disconnect! end |
#finish ⇒ Object
When finishing, releases the borrowed connection back into the pool
118 119 120 |
# File 'lib/activerecord_autoreplica.rb', line 118 def finish release_read_pool_connection end |
#release_read_pool_connection ⇒ Object
94 95 96 |
# File 'lib/activerecord_autoreplica.rb', line 94 def release_read_pool_connection @read_pool.release_connection end |
#respond_to_missing?(method_name) ⇒ Boolean
The duo for method proxying without delegate
110 111 112 |
# File 'lib/activerecord_autoreplica.rb', line 110 def respond_to_missing?(method_name) @original_handler.respond_to?(method_name) end |
#retrieve_connection(for_ar_class) ⇒ Object
Overridden method which gets called by ActiveRecord to get a connection related to a specific ActiveRecord::Base subclass.
88 89 90 91 92 |
# File 'lib/activerecord_autoreplica.rb', line 88 def retrieve_connection(for_ar_class) connection_for_writes = @original_handler.retrieve_connection(for_ar_class) connection_for_reads = @read_pool.connection Adapter.new(connection_for_writes, connection_for_reads) end |