Class: AutoReplica::ConnectionHandler

Inherits:
Object
  • Object
show all
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

AdHocConnectionHandler

Instance Method Summary collapse

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

#finishObject

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_connectionObject



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

Returns:

  • (Boolean)


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