redis-read-write-locks 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea7b9828f43be08a2e5cf9442c3fcc4704a93297934305d4688d4439e44b22a7
4
- data.tar.gz: 317dbc6377d5e429d538e4c046043bbcbe694bdd8c2a01119ebbd0b16aff4f3d
3
+ metadata.gz: cf0e4824a200a3a7edd4b4dfe626ebfb8cb243b69ed87ebeba49f8e545067708
4
+ data.tar.gz: 24165395045c8fb6ab49d243beeb711d693efbbb79d709b4bb680e6a94947566
5
5
  SHA512:
6
- metadata.gz: 17a5fd925b81b59c5639b9f7225766ee0986f472b6eb87759aedff9ebb70f1f78470c5f4cae1750e18e048d3142af11cadf41f0a81cd759fe5c9682cb1337db0
7
- data.tar.gz: 44474007063f41629590bd8e29822bf3532af425ec0d2099e649e25dedd79256cf4a5eaaca12e48fd62d5ffd3e295605175f704d758ec043c4862bbfec5787b6
6
+ metadata.gz: 93156958fcdb655a7539aa1dc00f5d2ba8a287d374f3eedd65352bff7af1ac27a031058d1bba1db04afa8c99af72bde2ca9c2763797ddb48f2d0fd5bf839bea8
7
+ data.tar.gz: be70fc26075e27252d6070c1770ad1aa0ca8cdfece6b85aa6ac226db41d522878a4d0aebb847535da029666f2f778ddb6f51ac69a43cffcc2d8891b32eaea4c2
data/README.md CHANGED
@@ -30,18 +30,18 @@ lock = client.read_lock("my_resource")
30
30
  lock.acquire # => true / false (non-blocking)
31
31
  lock.release
32
32
 
33
- # Block on contention — retries for up to N seconds
34
- lock.acquire(timeout: 5) # raises LockTimeoutError if timeout exceeded
35
- lock.synchronize(timeout: 5) { } # acquire + yield + release
33
+ # Block on contention — retries for up to N milliseconds
34
+ lock.acquire(timeout: 5000) # raises LockTimeoutError if timeout exceeded
35
+ lock.synchronize(timeout: 5000) { } # acquire + yield + release
36
36
 
37
37
  # Per-lock TTL override
38
- client.write_lock("my_resource", ttl: 60) { long_operation }
38
+ client.write_lock("my_resource", ttl: 60_000) { long_operation }
39
39
  ```
40
40
 
41
41
  ### Client options
42
42
 
43
43
  ```ruby
44
- client = RedisReadWriteLocks::Client.new(redis, default_ttl: 60)
44
+ client = RedisReadWriteLocks::Client.new(redis, default_ttl: 60_000)
45
45
  ```
46
46
 
47
47
  ### Options
@@ -2,8 +2,8 @@ require "securerandom"
2
2
 
3
3
  module RedisReadWriteLocks
4
4
  class BaseLock
5
- DEFAULT_TTL = 30
6
- RETRY_INTERVAL = 0.01
5
+ DEFAULT_TTL = 30_000
6
+ DEFAULT_RETRY_DELAY = 100
7
7
 
8
8
  attr_reader :name, :token
9
9
 
@@ -19,23 +19,20 @@ module RedisReadWriteLocks
19
19
  @acquired
20
20
  end
21
21
 
22
- # Non-blocking: returns true/false.
23
- # With timeout: retries for timeout seconds, returns true or raises LockTimeoutError.
24
- def acquire(timeout: nil)
25
- return try_acquire if timeout.nil?
22
+ def acquire(retry_count: nil, retry_delay: DEFAULT_RETRY_DELAY)
23
+ return try_acquire if retry_count.nil?
26
24
 
27
- deadline = Time.now.to_f + timeout
28
- loop do
25
+ return true if try_acquire
26
+ retry_count.times do
27
+ sleep retry_delay / 1000.0
29
28
  return true if try_acquire
30
- raise LockTimeoutError, "Timeout acquiring #{lock_type} lock '#{@name}'" if Time.now.to_f >= deadline
31
- sleep RETRY_INTERVAL
32
29
  end
30
+ raise LockTimeoutError, "Could not acquire #{lock_type} lock '#{@name}' after #{retry_count} retries"
33
31
  end
34
32
 
35
- # Acquires lock, yields, releases. Raises LockNotAcquiredError if non-blocking acquire fails.
36
- def synchronize(timeout: nil, &block)
37
- if timeout
38
- acquire(timeout: timeout)
33
+ def synchronize(retry_count: nil, retry_delay: DEFAULT_RETRY_DELAY, &block)
34
+ if retry_count
35
+ acquire(retry_count: retry_count, retry_delay: retry_delay)
39
36
  else
40
37
  acquire || raise(LockNotAcquiredError, "Could not acquire #{lock_type} lock '#{@name}'")
41
38
  end
@@ -5,14 +5,14 @@ module RedisReadWriteLocks
5
5
  @default_ttl = default_ttl
6
6
  end
7
7
 
8
- def read_lock(name, ttl: @default_ttl, &block)
8
+ def read_lock(name, ttl: @default_ttl, retry_count: nil, retry_delay: BaseLock::DEFAULT_RETRY_DELAY, &block)
9
9
  lock = ReadLock.new(redis: @redis, name: name, ttl: ttl)
10
- block ? lock.synchronize(&block) : lock
10
+ block ? lock.synchronize(retry_count: retry_count, retry_delay: retry_delay, &block) : lock
11
11
  end
12
12
 
13
- def write_lock(name, ttl: @default_ttl, &block)
13
+ def write_lock(name, ttl: @default_ttl, retry_count: nil, retry_delay: BaseLock::DEFAULT_RETRY_DELAY, &block)
14
14
  lock = WriteLock.new(redis: @redis, name: name, ttl: ttl)
15
- block ? lock.synchronize(&block) : lock
15
+ block ? lock.synchronize(retry_count: retry_count, retry_delay: retry_delay, &block) : lock
16
16
  end
17
17
  end
18
18
  end
@@ -12,7 +12,7 @@ module RedisReadWriteLocks
12
12
 
13
13
  def try_acquire
14
14
  now = Time.now.to_i
15
- expiry = now + @ttl
15
+ expiry = now + @ttl / 1000
16
16
 
17
17
  result = eval_script(
18
18
  LockScripts::ACQUIRE_READ,
@@ -1,3 +1,3 @@
1
1
  module RedisReadWriteLocks
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -14,7 +14,7 @@ module RedisReadWriteLocks
14
14
  result = eval_script(
15
15
  LockScripts::ACQUIRE_WRITE,
16
16
  keys: [writer_key, readers_key],
17
- argv: [@token, @ttl, Time.now.to_i],
17
+ argv: [@token, @ttl / 1000, Time.now.to_i],
18
18
  )
19
19
 
20
20
  @acquired = result == 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-read-write-locks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Umbrellio