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

Ensure EXISTS key is not orphaned when expire is used #39

Open
wants to merge 2 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
5 changes: 1 addition & 4 deletions lib/redis/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def exists_or_create!
if token == API_VERSION && @redis.get(version_key).nil?
@redis.set(version_key, API_VERSION)
end

set_expiration_if_necessary
true
end
end
Expand Down Expand Up @@ -163,8 +163,6 @@ def simple_mutex(key_name, expires = nil)
end

def create!
@redis.expire(exists_key, 10)

@redis.multi do
@redis.del(grabbed_key)
@redis.del(available_key)
Expand All @@ -173,7 +171,6 @@ def create!
end
@redis.set(version_key, API_VERSION)
@redis.persist(exists_key)

set_expiration_if_necessary
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/semaphore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@
sleep 3.0
expect(@redis.keys.count).to eq(original_key_size)
end

it "does not leave a key without expiration if expiration given" do
queue = Queue.new
threads = Array.new(2) do
Thread.new do
opts = { redis: @redis, expiration: 3 }
Redis::Semaphore.new(:my_semaphore, opts).lock(5) do
sleep 1
end
end
end
sleep 4.0
@redis2 = Redis.new db: 15
threads.each(&:kill) # ensure signal step fails
exist_key = @redis2.ttl("SEMAPHORE:my_semaphore:EXISTS").to_s
expect(exist_key).to_not eql("-1")
sleep 4.0 # allow blpop timeout to occur
thrd = Thread.new do
opts = { redis: @redis2, expiration: 3 }
Redis::Semaphore.new(:my_semaphore, opts).lock(5) do
queue << "work"
end
end
thrd.join(3)
expect(queue.size).to eql(1)
expect(@redis.ttl("SEMAPHORE:my_semaphore:EXISTS").to_s).to_not eql("-1")
end
end

describe "semaphore without staleness checking" do
Expand Down