Module: RaceGuard::DBLockAuditor::ReadModifyWrite::Patches
- Defined in:
- lib/race_guard/db_lock_auditor/read_modify_write.rb
Overview
Prepended to ActiveRecord::Base.
Constant Summary collapse
- RMW_IN_READ_HOOK =
:__race_guard_rmw_in_read_hook
Instance Method Summary collapse
-
#_read_attribute(*args, **kwargs) ⇒ Object
Generated readers (e.g.
#balance) call_read_attribute, notread_attribute. -
#lock!(lock = true) ⇒ Object
Match ActiveRecord::Locking::Pessimistic#lock!; default
truematches upstream. - #read_attribute(*args, **kwargs) ⇒ Object
- #save(*args, **kwargs) ⇒ Object
- #save!(*args, **kwargs) ⇒ Object
- #with_lock(*args, &block) ⇒ Object
Instance Method Details
#_read_attribute(*args, **kwargs) ⇒ Object
Generated readers (e.g. #balance) call _read_attribute, not read_attribute. Re-entrant _read_attribute (e.g. id while resolving a journal key) must skip instrumentation to avoid infinite recursion.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/race_guard/db_lock_auditor/read_modify_write.rb', line 18 def _read_attribute(*args, **kwargs, &) return super if Thread.current[RMW_IN_READ_HOOK] Thread.current[RMW_IN_READ_HOOK] = true val = super ReadModWriteImpl.note_read_from__read_attribute(self, args[0], val) val ensure Thread.current[RMW_IN_READ_HOOK] = false end |
#lock!(lock = true) ⇒ Object
Match ActiveRecord::Locking::Pessimistic#lock!; default true matches upstream.
67 68 69 70 71 |
# File 'lib/race_guard/db_lock_auditor/read_modify_write.rb', line 67 def lock!(lock = true) # rubocop:disable Style/OptionalBooleanParameter result = super ReadModWriteImpl.record_pessimistic_lock_for_record!(self) result end |
#read_attribute(*args, **kwargs) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/race_guard/db_lock_auditor/read_modify_write.rb', line 29 def read_attribute(*args, **kwargs, &) return super if Thread.current[RMW_IN_READ_HOOK] Thread.current[RMW_IN_READ_HOOK] = true val = super ReadModWriteImpl.note_read_from_read_attribute(self, args[0], val) val ensure Thread.current[RMW_IN_READ_HOOK] = false end |
#save(*args, **kwargs) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/race_guard/db_lock_auditor/read_modify_write.rb', line 40 def save(*args, **kwargs) ReadModWriteImpl.enter_persisted_write! result = super ReadModWriteImpl.after_save(self, result, 'save') result ensure ReadModWriteImpl.leave_persisted_write! end |
#save!(*args, **kwargs) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/race_guard/db_lock_auditor/read_modify_write.rb', line 49 def save!(*args, **kwargs) ReadModWriteImpl.enter_persisted_write! result = super ReadModWriteImpl.after_save(self, true, 'save!') result ensure ReadModWriteImpl.leave_persisted_write! end |
#with_lock(*args, &block) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/race_guard/db_lock_auditor/read_modify_write.rb', line 58 def with_lock(*args, &block) return super unless block super do ReadModWriteImpl.around_with_lock_user_block(self) { block.call } end end |