Skip to content
Merged
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
13 changes: 11 additions & 2 deletions lib/redis/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Semaphore
# Redis::Semaphore.new(:my_semaphore, :path => "bla")
def initialize(name, opts = {})
@name = name
@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)
Expand All @@ -30,6 +31,7 @@ def exists_or_create!
elsif token != API_VERSION
raise "Semaphore exists but running as wrong version (version #{token} vs #{API_VERSION})."
else
set_expiration
true
end
end
Expand Down Expand Up @@ -80,7 +82,7 @@ def locked?(token = nil)
@tokens.each do |token|
return true if locked?(token)
end

false
end
end
Expand Down Expand Up @@ -150,10 +152,17 @@ def create!
@resource_count.times do |index|
@redis.rpush(available_key, index)
end

# Persist key
@redis.del(exists_key)
@redis.set(exists_key, API_VERSION)
set_expiration
end
end

def set_expiration
if @expiration
@redis.expire(available_key, @expiration)
@redis.expire(exists_key, @expiration)
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/semaphore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@
end
end

describe "semaphore with expiration" do
let(:semaphore) { Redis::Semaphore.new(:my_semaphore, :redis => @redis, :expiration => 2) }
let(:multisem) { Redis::Semaphore.new(:my_semaphore_2, :resources => 2, :redis => @redis, :expiration => 2) }

it_behaves_like "a semaphore"

it "expires keys" do
original_key_size = @redis.keys.count
semaphore.exists_or_create!
sleep 3.0
expect(@redis.keys.count).to eq(original_key_size)
end
end

describe "semaphore without staleness checking" do
let(:semaphore) { Redis::Semaphore.new(:my_semaphore, :redis => @redis) }
let(:multisem) { Redis::Semaphore.new(:my_semaphore_2, :resources => 2, :redis => @redis) }
Expand Down