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

Returns a new instance of Context.



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

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.



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

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).



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

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

.last_used_connectionObject



61
62
63
# File 'lib/janus-ar/context.rb', line 61

def last_used_connection
  current.last_used_connection
end

.release_allObject



49
50
51
# File 'lib/janus-ar/context.rb', line 49

def release_all
  current.release_all
end

.stick_to_primaryObject



45
46
47
# File 'lib/janus-ar/context.rb', line 45

def stick_to_primary
  current.stick_to_primary
end

.use_primary?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/janus-ar/context.rb', line 57

def use_primary?
  current.use_primary?
end

.used_connection(connection) ⇒ Object



53
54
55
# File 'lib/janus-ar/context.rb', line 53

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

Instance Method Details

#release_allObject



29
30
31
32
# File 'lib/janus-ar/context.rb', line 29

def release_all
  @primary = false
  @last_used_connection = nil
end

#stick_to_primaryObject



25
26
27
# File 'lib/janus-ar/context.rb', line 25

def stick_to_primary
  @primary = true
end

#use_primary?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/janus-ar/context.rb', line 34

def use_primary?
  @primary
end

#used_connection(connection) ⇒ Object



38
39
40
# File 'lib/janus-ar/context.rb', line 38

def used_connection(connection)
  @last_used_connection = connection
end