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.



21
22
23
# File 'lib/janus-ar/adapter_extensions.rb', line 21

def config
  @config
end

Class Method Details

.included(base) ⇒ Object



11
12
13
# File 'lib/janus-ar/adapter_extensions.rb', line 11

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

Instance Method Details

#clear_cache!Object



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

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

#connect!Object



65
66
67
68
# File 'lib/janus-ar/adapter_extensions.rb', line 65

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

#discard!Object



75
76
77
78
# File 'lib/janus-ar/adapter_extensions.rb', line 75

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

#disconnect!Object



80
81
82
83
# File 'lib/janus-ar/adapter_extensions.rb', line 80

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

#execute(sql) ⇒ Object



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

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



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

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 argument forwarding 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.



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

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

#reconnect!Object



70
71
72
73
# File 'lib/janus-ar/adapter_extensions.rb', line 70

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

#replica_connectionObject



90
91
92
# File 'lib/janus-ar/adapter_extensions.rb', line 90

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