Class: RubyReactor::Lock
- Inherits:
-
Object
- Object
- RubyReactor::Lock
- Defined in:
- lib/ruby_reactor/lock.rb
Defined Under Namespace
Classes: AcquisitionError
Constant Summary collapse
- MIN_EXTEND_INTERVAL =
Minimum interval between auto-extend pings; protects very small TTLs.
1.0
Instance Attribute Summary collapse
-
#auto_extend ⇒ Object
readonly
Returns the value of attribute auto_extend.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
-
#wait ⇒ Object
readonly
Returns the value of attribute wait.
Instance Method Summary collapse
- #acquire ⇒ Object
-
#initialize(key, owner:, ttl: 60, wait: 0, auto_extend: true) ⇒ Lock
constructor
A new instance of Lock.
- #release ⇒ Object
- #synchronize ⇒ Object
Constructor Details
#initialize(key, owner:, ttl: 60, wait: 0, auto_extend: true) ⇒ Lock
Returns a new instance of Lock.
12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby_reactor/lock.rb', line 12 def initialize(key, owner:, ttl: 60, wait: 0, auto_extend: true) @key = "lock:#{key}" @owner = owner @ttl = ttl @wait = wait @auto_extend = auto_extend @extender = nil @extender_running = false end |
Instance Attribute Details
#auto_extend ⇒ Object (readonly)
Returns the value of attribute auto_extend.
10 11 12 |
# File 'lib/ruby_reactor/lock.rb', line 10 def auto_extend @auto_extend end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/ruby_reactor/lock.rb', line 10 def key @key end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
10 11 12 |
# File 'lib/ruby_reactor/lock.rb', line 10 def owner @owner end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
10 11 12 |
# File 'lib/ruby_reactor/lock.rb', line 10 def ttl @ttl end |
#wait ⇒ Object (readonly)
Returns the value of attribute wait.
10 11 12 |
# File 'lib/ruby_reactor/lock.rb', line 10 def wait @wait end |
Instance Method Details
#acquire ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby_reactor/lock.rb', line 22 def acquire start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) loop do if adapter.lock_acquire(@key, @owner, @ttl) start_extender if @auto_extend return true end if @wait.zero? || (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) >= @wait raise AcquisitionError, "Could not acquire lock '#{@key}' for owner '#{@owner}'" end sleep 0.1 end end |
#release ⇒ Object
39 40 41 42 |
# File 'lib/ruby_reactor/lock.rb', line 39 def release stop_extender adapter.lock_release(@key, @owner) end |
#synchronize ⇒ Object
44 45 46 47 48 49 |
# File 'lib/ruby_reactor/lock.rb', line 44 def synchronize acquire yield ensure release end |