forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request discourse#2257 from vikhyat/distributed-mutex
Extract cross-process locking from the scheduler into DistributedMutex
- Loading branch information
Showing
4 changed files
with
100 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Cross-process locking using Redis. | ||
class DistributedMutex | ||
attr_accessor :redis | ||
attr_reader :got_lock | ||
|
||
def initialize(key, redis=nil) | ||
@key = key | ||
@redis = redis || $redis | ||
@got_lock = false | ||
end | ||
|
||
def try_to_get_lock | ||
if redis.setnx @key, Time.now.to_i + 60 | ||
redis.expire @key, 60 | ||
@got_lock = true | ||
else | ||
begin | ||
redis.watch @key | ||
time = redis.get @key | ||
if time && time.to_i < Time.now.to_i | ||
@got_lock = redis.multi do | ||
redis.set @key, Time.now.to_i + 60 | ||
end | ||
end | ||
ensure | ||
redis.unwatch | ||
end | ||
end | ||
end | ||
|
||
def get_lock | ||
return if @got_lock | ||
|
||
start = Time.now | ||
while !@got_lock | ||
try_to_get_lock | ||
end | ||
end | ||
|
||
def release_lock | ||
redis.del @key | ||
@got_lock = false | ||
end | ||
|
||
def synchronize | ||
get_lock | ||
yield | ||
ensure | ||
release_lock | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
require_dependency 'distributed_mutex' | ||
|
||
describe DistributedMutex do | ||
it "allows only one mutex object to have the lock at a time" do | ||
m1 = DistributedMutex.new("test_mutex_key") | ||
m2 = DistributedMutex.new("test_mutex_key") | ||
|
||
m1.get_lock | ||
m2.got_lock.should be_false | ||
|
||
t = Thread.new do | ||
m2.get_lock | ||
end | ||
|
||
m1.release_lock | ||
t.join | ||
m2.got_lock.should == true | ||
end | ||
|
||
it "synchronizes correctly" do | ||
array = [] | ||
t = Thread.new do | ||
DistributedMutex.new("correct_sync").synchronize do | ||
sleep 0.01 | ||
array.push 1 | ||
end | ||
end | ||
sleep 0.005 | ||
DistributedMutex.new("correct_sync").synchronize do | ||
array.push 2 | ||
end | ||
t.join | ||
array.should == [1, 2] | ||
end | ||
end |