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 deadlocks, small improvements #33

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
16 changes: 8 additions & 8 deletions lib/redis/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

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.

else
Redis.new(opts)
end
@tokens = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing #getset destroys the atomicity of initialization. We want to keep getset.

end

def exists_or_create!
token = @redis.getset(exists_key, EXISTS_TOKEN)
token = @redis.get(exists_key)

if token.nil?
create!
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary when keeping #getset.


@redis.multi do
@redis.del(grabbed_key)
Expand Down
20 changes: 19 additions & 1 deletion spec/semaphore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@

expect(@redis.keys.count).to eq(original_key_size)
end

it "don't enters deadlock" do
i = 0
expect {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'timeout'
Copy link
Collaborator

Choose a reason for hiding this comment

The 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'