redis-read-write-locks 0.1.0 → 0.3.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 +4 -4
- data/README.md +5 -5
- data/lib/redis-read-write-locks.rb +1 -0
- data/lib/redis_read_write_locks/base_lock.rb +8 -4
- data/lib/redis_read_write_locks/client.rb +4 -4
- data/lib/redis_read_write_locks/read_lock.rb +1 -1
- data/lib/redis_read_write_locks/version.rb +1 -1
- data/lib/redis_read_write_locks/write_lock.rb +1 -1
- data/redis-read-write-locks.gemspec +2 -2
- metadata +17 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9c9401651786ff69991b9b975ebbc62cf0442fd3f2639c79fad803fab00f335e
|
|
4
|
+
data.tar.gz: 724513d599b3a614e43a3be6b634ad52c1d64035c63ddfb1566e2c93d4a837a6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: be039491564babb54c6e1b5ce87e880bc643f4f761efa24faadbc9c2a890e019c48daac13ebbd83409b0db60885b21e23b53fa7fc2d83a879662690fafab2b2a
|
|
7
|
+
data.tar.gz: 765be840a9d1abba171df6cee27dc3c775eac4ad8edfdf38eebbc32c21a09e51912713ae7006d6863fc9ff8d6e6299dd1820be503231443169ebb36db62530b0
|
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
|
|
34
|
-
lock.acquire(timeout:
|
|
35
|
-
lock.synchronize(timeout:
|
|
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:
|
|
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:
|
|
44
|
+
client = RedisReadWriteLocks::Client.new(redis, default_ttl: 60_000)
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
### Options
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require_relative "redis_read_write_locks"
|
|
@@ -2,7 +2,7 @@ require "securerandom"
|
|
|
2
2
|
|
|
3
3
|
module RedisReadWriteLocks
|
|
4
4
|
class BaseLock
|
|
5
|
-
DEFAULT_TTL =
|
|
5
|
+
DEFAULT_TTL = 30_000
|
|
6
6
|
RETRY_INTERVAL = 0.01
|
|
7
7
|
|
|
8
8
|
attr_reader :name, :token
|
|
@@ -20,11 +20,11 @@ module RedisReadWriteLocks
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# Non-blocking: returns true/false.
|
|
23
|
-
# With timeout: retries for timeout
|
|
23
|
+
# With timeout: retries for timeout milliseconds, returns true or raises LockTimeoutError.
|
|
24
24
|
def acquire(timeout: nil)
|
|
25
25
|
return try_acquire if timeout.nil?
|
|
26
26
|
|
|
27
|
-
deadline = Time.now.to_f + timeout
|
|
27
|
+
deadline = Time.now.to_f + timeout / 1000.0
|
|
28
28
|
loop do
|
|
29
29
|
return true if try_acquire
|
|
30
30
|
raise LockTimeoutError, "Timeout acquiring #{lock_type} lock '#{@name}'" if Time.now.to_f >= deadline
|
|
@@ -61,7 +61,11 @@ module RedisReadWriteLocks
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def eval_script(script, keys:, argv:)
|
|
64
|
-
@redis.
|
|
64
|
+
if @redis.respond_to?(:eval)
|
|
65
|
+
@redis.eval(script, keys: keys, argv: argv)
|
|
66
|
+
else
|
|
67
|
+
@redis.call("EVAL", script, keys.length, *keys, *argv)
|
|
68
|
+
end
|
|
65
69
|
end
|
|
66
70
|
end
|
|
67
71
|
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, timeout: nil, &block)
|
|
9
9
|
lock = ReadLock.new(redis: @redis, name: name, ttl: ttl)
|
|
10
|
-
block ? lock.synchronize(&block) : lock
|
|
10
|
+
block ? lock.synchronize(timeout: timeout, &block) : lock
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def write_lock(name, ttl: @default_ttl, &block)
|
|
13
|
+
def write_lock(name, ttl: @default_ttl, timeout: nil, &block)
|
|
14
14
|
lock = WriteLock.new(redis: @redis, name: name, ttl: ttl)
|
|
15
|
-
block ? lock.synchronize(&block) : lock
|
|
15
|
+
block ? lock.synchronize(timeout: timeout, &block) : lock
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
end
|
|
@@ -14,8 +14,8 @@ Gem::Specification.new do |spec|
|
|
|
14
14
|
spec.files = Dir["lib/**/*.rb", "*.gemspec", "LICENSE", "README.md"]
|
|
15
15
|
spec.require_paths = ["lib"]
|
|
16
16
|
|
|
17
|
-
spec.
|
|
18
|
-
|
|
17
|
+
spec.add_development_dependency "redis", ">= 4.0"
|
|
18
|
+
spec.add_development_dependency "redis-client"
|
|
19
19
|
spec.add_development_dependency "rspec", "~> 3.0"
|
|
20
20
|
spec.add_development_dependency "rake", "~> 13.0"
|
|
21
21
|
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.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Umbrellio
|
|
@@ -16,13 +16,27 @@ dependencies:
|
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
18
|
version: '4.0'
|
|
19
|
-
type: :
|
|
19
|
+
type: :development
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
25
|
version: '4.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: redis-client
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
26
40
|
- !ruby/object:Gem::Dependency
|
|
27
41
|
name: rspec
|
|
28
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -60,6 +74,7 @@ extensions: []
|
|
|
60
74
|
extra_rdoc_files: []
|
|
61
75
|
files:
|
|
62
76
|
- README.md
|
|
77
|
+
- lib/redis-read-write-locks.rb
|
|
63
78
|
- lib/redis_read_write_locks.rb
|
|
64
79
|
- lib/redis_read_write_locks/base_lock.rb
|
|
65
80
|
- lib/redis_read_write_locks/client.rb
|