Class: Janus::Context
- Inherits:
-
Object
- Object
- Janus::Context
- 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
-
#last_used_connection ⇒ Object
readonly
Returns the value of attribute last_used_connection.
Class Method Summary collapse
-
.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).
- .last_used_connection ⇒ Object
- .release_all ⇒ Object
- .stick_to_primary ⇒ Object
- .use_primary? ⇒ Boolean
- .used_connection(connection) ⇒ Object
Instance Method Summary collapse
-
#initialize(primary: false) ⇒ Context
constructor
A new instance of Context.
- #release_all ⇒ Object
- #stick_to_primary ⇒ Object
- #use_primary? ⇒ Boolean
- #used_connection(connection) ⇒ Object
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_connection ⇒ Object (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_connection ⇒ Object
59 60 61 |
# File 'lib/janus-ar/context.rb', line 59 def last_used_connection current.last_used_connection end |
.release_all ⇒ Object
47 48 49 |
# File 'lib/janus-ar/context.rb', line 47 def release_all current.release_all end |
.stick_to_primary ⇒ Object
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_all ⇒ Object
27 28 29 30 |
# File 'lib/janus-ar/context.rb', line 27 def release_all @primary = false @last_used_connection = nil end |
#stick_to_primary ⇒ Object
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 |