-
Notifications
You must be signed in to change notification settings - Fork 68
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 deadlocks, small improvements #33
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,17 @@ def initialize(name, opts = {}) | |
@expiration = opts.delete(:expiration) | ||
@resource_count = opts.delete(:resources) || 1 | ||
@stale_client_timeout = opts.delete(:stale_client_timeout) | ||
@redis = opts.delete(:redis) || Redis.new(opts) | ||
@use_local_time = opts.delete(:use_local_time) | ||
@redis = if @redis = opts.delete(:redis) | ||
@redis.dup | ||
else | ||
Redis.new(opts) | ||
end | ||
@tokens = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing |
||
end | ||
|
||
def exists_or_create! | ||
token = @redis.getset(exists_key, EXISTS_TOKEN) | ||
token = @redis.get(exists_key) | ||
|
||
if token.nil? | ||
create! | ||
|
@@ -90,11 +94,7 @@ def locked?(token = nil) | |
if token | ||
@redis.hexists(grabbed_key, token) | ||
else | ||
@tokens.each do |token| | ||
return true if locked?(token) | ||
end | ||
|
||
false | ||
@tokens.any? { |t| locked?(t) } | ||
end | ||
end | ||
|
||
|
@@ -156,7 +156,7 @@ def simple_mutex(key_name, expires = nil) | |
end | ||
|
||
def create! | ||
@redis.expire(exists_key, 10) | ||
@redis.setex(exists_key, 10, EXISTS_TOKEN) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary when keeping |
||
|
||
@redis.multi do | ||
@redis.del(grabbed_key) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,23 @@ | |
|
||
expect(@redis.keys.count).to eq(original_key_size) | ||
end | ||
|
||
it "don't enters deadlock" do | ||
i = 0 | ||
expect { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using {...} for multi-line blocks. |
||
redis = @redis.dup | ||
Timeout.timeout(2) do | ||
Array.new(2).map do | ||
Thread.new do | ||
Redis::Semaphore.new(:sem, redis: redis).lock do | ||
i += 1 | ||
end | ||
end | ||
end.each(&:join) | ||
end | ||
}.not_to raise_error | ||
expect(i).to eq(2) | ||
end | ||
end | ||
|
||
describe "semaphore with expiration" do | ||
|
@@ -211,7 +228,8 @@ | |
end | ||
|
||
it "without time support should return the same time as frozen time" do | ||
expect(@redis).to receive(:time).and_raise(Redis::CommandError) | ||
expect(semaphore.instance_variable_get(:@redis)).to receive(:time) | ||
.and_raise(Redis::CommandError) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Place the . on the previous line, together with the method call receiver. |
||
expect(semaphore.send(:current_time)).to eq(Time.now) | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
|
||
$TESTING=true | ||
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib') | ||
require 'timeout' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
require 'redis/semaphore' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to duplicate the passed in Redis client.