-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_mutex.rb
94 lines (64 loc) · 2.23 KB
/
redis_mutex.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# ;ob/ruby/redis_mutex.rb
# cross-process/cross-server mutex
# See: http://blog.cloud66.com/ruby-mutex-mayhem/
# but ... beware of the misspelling of synchronize
#
# $redis is a global variable that encapsulates a connection to your Redis instance.
#
# Usage Example:
=begin
MY_MUTEX = RedisMutex.new('server_access', 10.seconds)
# ...
MY_MUTEX.synchronize(server.id) do
# do some stuff here that needs to be synchronized
# for this resource (across all application instances)
end
=end
class RedisMutex
attr_accessor :global_scope,
:max_lock_time,
:recheck_frequency
# NOTE: This is a Lua script be run (eval'ed) on the Redis server
LOCK_ACQUIRER = "return redis.call('setnx', KEYS[1], 1) == 1 and redis.call('expire', KEYS[1], KEYS[2]) and 1 or 0"
def initialize(global_scope, max_lock_time, recheck_frequency: 1)
# the global scope of this mutex (i.e "resource")
@global_scope = global_scope
# max time in seconds to hold the mutex
# (in case of greedy deadlock)
@max_lock_time = max_lock_time
# recheck frequency how often to check if the mutex is
# released when blocked
@recheck_frequency = recheck_frequency
end
def synchronize(local_scope = :global, &block)
# get the lock
acquire(local_scope)
begin
# execute the actions
return block.call
ensure
# release the lock
release(local_scope)
end
end
##################################################################
private
# attempt to acquire the lock
def acquire(local_scope = :global)
# construct the mutex key; the local scope
# of this mutex (i.e "resource_id")
mutex_key = "#{@global_scope}.#{local_scope}"
# while statement will either get the lock and
# set the expiry on the lock or do neither and return 0
while $redis.eval(LOCK_ACQUIRER, [mutex_key, @max_lock_time]) != 1 do
# wait and try again (until we can get in)
sleep(@recheck_frequency)
end
end
# release the lock
def release(local_scope = :global)
# return value indicating whether the lock was currently held
mutex_key = "#{@global_scope}.#{local_scope}"
return $redis.del(mutex_key) == 1
end
end # class RedisMutex