uncomplicated_mutex 1.0.1 → 1.1.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/lib/uncomplicated_mutex.rb +38 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef209e4d08baa69dfded75606346ec2da70471de
|
4
|
+
data.tar.gz: da46ce0c406935be7ca4a472d7d9a33739b8d59e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cc35a073f8b4a114328323ae4da7b0d944cd905bf39220b23ef2fe2ccc4c67a846f5ba9683daebdb9e359f8d52794cfd216f4e1aea4b2da7b5833a6eaf5766f
|
7
|
+
data.tar.gz: ca51e08793f7496088a3523f34019b486f40a7462ac726bade21e56b232001567671d5893e2803a4d5dd7924ecae335e5ff559e468767de41823f950051fdd0b
|
data/lib/uncomplicated_mutex.rb
CHANGED
@@ -12,12 +12,10 @@ class UncomplicatedMutex
|
|
12
12
|
def initialize(obj, opts = {})
|
13
13
|
@verbose = opts[:verbose]
|
14
14
|
@timeout = opts[:timeout] || 300
|
15
|
-
@fail_on_timeout = opts[:fail_on_timeout]
|
16
|
-
@ticks = opts[:ticks] || 100
|
17
|
-
@wait_tick = @timeout.to_f / @ticks.to_f
|
18
15
|
@redis = opts[:redis] || Redis.new
|
19
16
|
@lock_name = "lock:#{obj.class.name}:#{obj.id}".squeeze(":")
|
20
17
|
@token = Digest::MD5.new.hexdigest("#{@lock_name}_#{Time.now.to_f}")
|
18
|
+
set_expiration_time
|
21
19
|
end
|
22
20
|
|
23
21
|
def acquire_mutex
|
@@ -25,6 +23,10 @@ class UncomplicatedMutex
|
|
25
23
|
@redis.eval(LUA_ACQUIRE, [ @lock_name ], [ @timeout, @token ]) == 1
|
26
24
|
end
|
27
25
|
|
26
|
+
def current_token_value
|
27
|
+
@redis.get(@lock_name)
|
28
|
+
end
|
29
|
+
|
28
30
|
def destroy_mutex
|
29
31
|
puts("Destroying the lock #{@lock_name}") if @verbose
|
30
32
|
@redis.del(@lock_name)
|
@@ -39,13 +41,9 @@ class UncomplicatedMutex
|
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
42
|
-
def overwrite_mutex
|
43
|
-
puts("Replacing the lock #{@lock_name} with #{@token}") if @verbose
|
44
|
-
@redis.set(@lock_name, @token)
|
45
|
-
end
|
46
|
-
|
47
44
|
def recurse_until_ready(depth = 1)
|
48
|
-
return false if
|
45
|
+
return false if time_has_expired
|
46
|
+
@initial_token = current_token_value if depth == 1
|
49
47
|
wait_a_tick if depth > 1
|
50
48
|
acquire_mutex || recurse_until_ready(depth + 1)
|
51
49
|
end
|
@@ -55,9 +53,37 @@ class UncomplicatedMutex
|
|
55
53
|
@redis.eval(LUA_RELEASE, [ @lock_name ], [ @token ])
|
56
54
|
end
|
57
55
|
|
56
|
+
def same_token_as_before
|
57
|
+
new_token = current_token_value
|
58
|
+
if new_token == @initial_token
|
59
|
+
true
|
60
|
+
else
|
61
|
+
@initial_token = new_token
|
62
|
+
false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_expiration_time
|
67
|
+
@expiration_time = Time.now.to_i + @timeout
|
68
|
+
end
|
69
|
+
|
70
|
+
def time_has_expired
|
71
|
+
if Time.now.to_i > @expiration_time
|
72
|
+
if same_token_as_before
|
73
|
+
true
|
74
|
+
else
|
75
|
+
set_expiration_time
|
76
|
+
false
|
77
|
+
end
|
78
|
+
else
|
79
|
+
false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
58
83
|
def wait_a_tick
|
59
|
-
|
60
|
-
|
84
|
+
sleep_time = rand(100).to_f / 100.0
|
85
|
+
puts("Sleeping #{sleep_time} for the lock #{@lock_name} to become available") if @verbose
|
86
|
+
sleep(sleep_time)
|
61
87
|
end
|
62
88
|
|
63
89
|
def wait_for_mutex
|
@@ -65,8 +91,7 @@ class UncomplicatedMutex
|
|
65
91
|
puts("Acquired lock #{@lock_name}") if @verbose
|
66
92
|
else
|
67
93
|
puts("Failed to acquire the lock") if @verbose
|
68
|
-
raise MutexTimeout.new("Failed to acquire the lock")
|
69
|
-
overwrite_mutex
|
94
|
+
raise MutexTimeout.new("Failed to acquire the lock")
|
70
95
|
end
|
71
96
|
end
|
72
97
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uncomplicated_mutex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Coleman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|