Class: Janus::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/janus-ar/context.rb

Overview

Per-execution state that records whether the current unit of work has been pinned to the primary (e.g. after a write, so subsequent reads stay consistent). State is stored in ActiveSupport::IsolatedExecutionState so it follows the application's configured isolation level (thread or fiber), matching ActiveRecord itself.

Because pooled threads/fibers are reused across requests and jobs, the context MUST be released between units of work or a thread that performed a single write would keep routing every later read to the primary. The Rails integration (see Janus::Railtie) does this automatically; outside Rails, call Janus::Context.release_all yourself (e.g. in a Sidekiq middleware).

Constant Summary collapse

STATE_KEY =
:janus_ar_context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary: false) ⇒ Context



18
19
20
21
# File 'lib/janus-ar/context.rb', line 18

def initialize(primary: false)
  @primary = primary
  @last_used_connection = :primary
end

Instance Attribute Details

#last_used_connectionObject (readonly)

Returns the value of attribute last_used_connection.



40
41
42
# File 'lib/janus-ar/context.rb', line 40

def last_used_connection
  @last_used_connection
end

Class Method Details

.install_reset_hook(executor) ⇒ Object

Release the context at the start of every unit of work wrapped by the given ActiveSupport executor (web requests, ActiveJob and Sidekiq-on-Rails jobs all run inside it).



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

def install_reset_hook(executor)
  executor.to_run { Janus::Context.release_all }
end

.last_used_connectionObject



59
60
61
# File 'lib/janus-ar/context.rb', line 59

def last_used_connection
  current.last_used_connection
end

.release_allObject



47
48
49
# File 'lib/janus-ar/context.rb', line 47

def release_all
  current.release_all
end

.stick_to_primaryObject



43
44
45
# File 'lib/janus-ar/context.rb', line 43

def stick_to_primary
  current.stick_to_primary
end

.use_primary?Boolean



55
56
57
# File 'lib/janus-ar/context.rb', line 55

def use_primary?
  current.use_primary?
end

.used_connection(connection) ⇒ Object



51
52
53
# File 'lib/janus-ar/context.rb', line 51

def used_connection(connection)
  current.used_connection(connection)
end

Instance Method Details

#release_allObject



27
28
29
30
# File 'lib/janus-ar/context.rb', line 27

def release_all
  @primary = false
  @last_used_connection = nil
end

#stick_to_primaryObject



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

def stick_to_primary
  @primary = true
end

#use_primary?Boolean



32
33
34
# File 'lib/janus-ar/context.rb', line 32

def use_primary?
  @primary
end

#used_connection(connection) ⇒ Object



36
37
38
# File 'lib/janus-ar/context.rb', line 36

def used_connection(connection)
  @last_used_connection = connection
end