Class: RaceGuard::Distributed::MemoryLockStore

Inherits:
Object
  • Object
show all
Includes:
LockStore
Defined in:
lib/race_guard/distributed/memory_lock_store.rb

Overview

In-process LockStore for tests and single-process simulations. Uses monotonic clock for TTL expiry (no wall-clock sleep required for correctness).

Instance Method Summary collapse

Constructor Details

#initializeMemoryLockStore

Returns a new instance of MemoryLockStore.



12
13
14
15
16
# File 'lib/race_guard/distributed/memory_lock_store.rb', line 12

def initialize
  @mon = Monitor.new
  @entries = {} # key => { token:, expires_at: Float monotonic }
  @time_skew = 0.0
end

Instance Method Details

#advance_time!(seconds) ⇒ Object

Test helper: advance logical time so TTLs expire without sleeping.



61
62
63
# File 'lib/race_guard/distributed/memory_lock_store.rb', line 61

def advance_time!(seconds)
  @mon.synchronize { @time_skew += seconds.to_f }
end

#claim(key:, token:, ttl:) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/race_guard/distributed/memory_lock_store.rb', line 18

def claim(key:, token:, ttl:)
  raise ArgumentError, 'ttl must be positive' unless ttl.is_a?(Integer) && ttl.positive?

  now = monotonic
  @mon.synchronize do
    prune_expired_unlocked!(now)
    rec = @entries[key]
    if rec && rec[:expires_at] > now
      false
    else
      @entries[key] = { token: token.to_s, expires_at: now + ttl }
      true
    end
  end
end

#release(key:, token:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/race_guard/distributed/memory_lock_store.rb', line 48

def release(key:, token:)
  now = monotonic
  @mon.synchronize do
    prune_expired_unlocked!(now)
    rec = @entries[key]
    return false unless rec && rec[:expires_at] > now && rec[:token] == token.to_s

    @entries.delete(key)
    true
  end
end

#renew(key:, token:, ttl:) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/race_guard/distributed/memory_lock_store.rb', line 34

def renew(key:, token:, ttl:)
  raise ArgumentError, 'ttl must be positive' unless ttl.is_a?(Integer) && ttl.positive?

  now = monotonic
  @mon.synchronize do
    prune_expired_unlocked!(now)
    rec = @entries[key]
    return false unless rec && rec[:expires_at] > now && rec[:token] == token.to_s

    rec[:expires_at] = now + ttl
    true
  end
end