Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition when setting mutex key #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/mega_mutex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,3 @@ def configuration
end
end
end

6 changes: 3 additions & 3 deletions lib/mega_mutex/distributed_mutex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def locked_by_me?
end

def set_current_lock(new_lock)
cache.set(@key, my_lock_id)
# expire redis key after 1 hour
cache.expire(@key, 3600)
# nx: only set key if it doesn't exist
# px: expire redis key after 1 hour (in milliseconds)
cache.set(@key, my_lock_id, nx: true, px: 3_600_000)
end

def my_lock_id
Expand Down
2 changes: 1 addition & 1 deletion mega_mutex.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency(%q<redis>, ["~> 4"])
s.add_runtime_dependency(%q<logging>, [">= 1.1.4"])
s.add_development_dependency("rspec", ["= 1.3.0"])
s.add_development_dependency("rake", [">= 1.0"])
s.add_development_dependency("rake", ["< 11.0"])

end
8 changes: 4 additions & 4 deletions spec/lib/mega_mutex_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../spec_helper'
require_relative '../spec_helper'

module MegaMutex
describe MegaMutex do
Expand Down Expand Up @@ -39,7 +39,7 @@ def logger

describe "with the same lock key" do
before(:each) do
Redis.new({:host => 'redis.dev', :port => 6379}).delete(mutex_id)
Redis.new({:host => 'redis.dev', :port => 6379}).del(mutex_id)
end

def mutex_id
Expand Down Expand Up @@ -106,7 +106,6 @@ def mutex_id
end

describe "with a timeout" do

it "should raise an error if the code blocks for longer than the timeout" do
@exception = nil
@first_thread_has_started = false
Expand All @@ -126,7 +125,8 @@ def mutex_id
end
end
wait_for_threads_to_finish
assert @exception.is_a?(MegaMutex::TimeoutError), "Expected TimeoutError to be raised, but wasn't"

@exception.should be_kind_of(MegaMutex::TimeoutError), "Expected TimeoutError to be raised, but wasn't"
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/../lib/mega_mutex')
require 'test/unit/assertions'
require_relative '../lib/mega_mutex'

# Logging::Logger[:root].add_appenders(Logging::Appenders.stdout)

Expand Down Expand Up @@ -28,5 +27,4 @@ def wait_for_threads_to_finish
Spec::Runner.configure do |config|
config.extend ThreadHelper
config.include ThreadExampleHelper
config.include Test::Unit::Assertions
end