Module: Janus::AdapterExtensions

Overview

Behaviour shared by the Janus MySQL2 and Trilogy adapters.

Each Janus adapter subclasses the matching ActiveRecord adapter and owns the primary connection (reached via ‘super`). This module routes every statement to the primary, a lazily created replica connection, or both, and keeps Janus::Context up to date.

Including adapters only need to implement #replica_adapter_class.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/janus-ar/adapter_extensions.rb', line 23

def config
  @config
end

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/janus-ar/adapter_extensions.rb', line 13

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#clear_cache!Object



82
83
84
85
# File 'lib/janus-ar/adapter_extensions.rb', line 82

def clear_cache!(...)
  replica_connection.clear_cache!(...)
  super
end

#connect!Object



67
68
69
70
# File 'lib/janus-ar/adapter_extensions.rb', line 67

def connect!(...)
  replica_connection.connect!(...)
  super
end

#disconnect!Object



77
78
79
80
# File 'lib/janus-ar/adapter_extensions.rb', line 77

def disconnect!(...)
  replica_connection.disconnect!(...)
  super
end

#execute(sql) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/janus-ar/adapter_extensions.rb', line 54

def execute(sql, *, **)
  case where_to_send?(sql)
  when :all
    send_to_replica(sql, :all)
    super
  when :replica
    send_to_replica(sql, :replica)
  else
    mark_primary(sql)
    super
  end
end

#initialize(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/janus-ar/adapter_extensions.rb', line 25

def initialize(*args)
  config = args[0]
  config[:janus]['replica']['database'] = config[:database]
  config[:janus]['primary']['database'] = config[:database]

  @replica_config = config[:janus]['replica'].symbolize_keys
  args[0] = config[:janus]['primary'].symbolize_keys

  super
  @connection_parameters ||= args[0]
end

#raw_execute(sql) ⇒ Object

The argument lists below intentionally use anonymous splats and a bare ‘super`: ActiveRecord’s ‘raw_execute`/`execute` signatures differ between versions, so we forward whatever we are given unchanged rather than restating (and pinning ourselves to) the current signature.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/janus-ar/adapter_extensions.rb', line 41

def raw_execute(sql, *, **)
  case where_to_send?(sql)
  when :all
    send_to_replica(sql, :all)
    super
  when :replica
    send_to_replica(sql, :replica)
  else
    mark_primary(sql)
    super
  end
end

#reconnect!Object



72
73
74
75
# File 'lib/janus-ar/adapter_extensions.rb', line 72

def reconnect!(...)
  replica_connection.reconnect!(...)
  super
end

#replica_connectionObject



87
88
89
# File 'lib/janus-ar/adapter_extensions.rb', line 87

def replica_connection
  @replica_connection ||= replica_adapter_class.new(@replica_config)
end