redis-read-write-locks 0.3.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: 9c9401651786ff69991b9b975ebbc62cf0442fd3f2639c79fad803fab00f335e
4
- data.tar.gz: 724513d599b3a614e43a3be6b634ad52c1d64035c63ddfb1566e2c93d4a837a6
3
+ metadata.gz: cf0e4824a200a3a7edd4b4dfe626ebfb8cb243b69ed87ebeba49f8e545067708
4
+ data.tar.gz: 24165395045c8fb6ab49d243beeb711d693efbbb79d709b4bb680e6a94947566
5
5
  SHA512:
6
- metadata.gz: be039491564babb54c6e1b5ce87e880bc643f4f761efa24faadbc9c2a890e019c48daac13ebbd83409b0db60885b21e23b53fa7fc2d83a879662690fafab2b2a
7
- data.tar.gz: 765be840a9d1abba171df6cee27dc3c775eac4ad8edfdf38eebbc32c21a09e51912713ae7006d6863fc9ff8d6e6299dd1820be503231443169ebb36db62530b0
6
+ metadata.gz: 93156958fcdb655a7539aa1dc00f5d2ba8a287d374f3eedd65352bff7af1ac27a031058d1bba1db04afa8c99af72bde2ca9c2763797ddb48f2d0fd5bf839bea8
7
+ data.tar.gz: be70fc26075e27252d6070c1770ad1aa0ca8cdfece6b85aa6ac226db41d522878a4d0aebb847535da029666f2f778ddb6f51ac69a43cffcc2d8891b32eaea4c2
@@ -3,7 +3,7 @@ require "securerandom"
3
3
  module RedisReadWriteLocks
4
4
  class BaseLock
5
5
  DEFAULT_TTL = 30_000
6
- RETRY_INTERVAL = 0.01
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 milliseconds, 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 / 1000.0
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, timeout: nil, &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(timeout: timeout, &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, timeout: nil, &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(timeout: timeout, &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
@@ -1,3 +1,3 @@
1
1
  module RedisReadWriteLocks
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Umbrellio