redis-semaphore 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -1
- data/lib/redis/semaphore.rb +12 -2
- data/spec/semaphore_spec.rb +23 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -130,6 +130,8 @@ normal_sem.lock(5) # will block until the watchdog releases the previous lock af
|
|
130
130
|
Advanced
|
131
131
|
--------
|
132
132
|
|
133
|
+
### Wait and Signal
|
134
|
+
|
133
135
|
The methods ```wait``` and ```signal```, the traditional method names of a Semaphore, are also implemented. ```wait``` is aliased to lock, while ```signal``` puts the specified token back on the semaphore, or generates a unique new token and puts that back if none is passed:
|
134
136
|
|
135
137
|
```ruby
|
@@ -160,6 +162,19 @@ semaphore.signal(new_job) # Job can be any string, it will be passed unmodified
|
|
160
162
|
Used in this fashion, a timeout does not make sense. Using the :stale\_client\_timeout here is not recommended.
|
161
163
|
|
162
164
|
|
165
|
+
### Use local time
|
166
|
+
|
167
|
+
When calculating the timeouts, redis-semaphore uses the Redis TIME command by default, which fetches the time on the Redis server. This is good if you're running distributed semaphores to keep all clients on the same clock, but does incur an extra round-trip for every action that requires the time.
|
168
|
+
|
169
|
+
You can add the option ```:use_local_time => true``` during initialization to use the local time of the client instead of the Redis server time, which saves one extra roundtrip. This is good if e.g. you're only running one client.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
s = Redis::Semaphore.new(:local_semaphore, :redis = r, :stale_client_timeout => 5, :use_local_time => true)
|
173
|
+
```
|
174
|
+
|
175
|
+
Redis servers earlier than version 2.6 don't support the TIME command. In that case we fall back to using the local time automatically.
|
176
|
+
|
177
|
+
|
163
178
|
Installation
|
164
179
|
------------
|
165
180
|
|
@@ -174,8 +189,12 @@ Testing
|
|
174
189
|
Changelog
|
175
190
|
---------
|
176
191
|
|
192
|
+
###0.2.1 August 6, 2013
|
193
|
+
- Remove dependency on Redis 2.6+ using fallback for TIME command (thanks dubdromic!).
|
194
|
+
- Add ```:use_local_time``` option
|
195
|
+
|
177
196
|
###0.2.0 June 2, 2013
|
178
|
-
- Use Redis TIME command for lock timeouts (thanks
|
197
|
+
- Use Redis TIME command for lock timeouts (thanks dubdromic!).
|
179
198
|
- Version increase because of new dependency on Redis 2.6+
|
180
199
|
|
181
200
|
###0.1.7 April 18, 2013
|
data/lib/redis/semaphore.rb
CHANGED
@@ -18,6 +18,7 @@ class Redis
|
|
18
18
|
@resource_count = opts.delete(:resources) || 1
|
19
19
|
@stale_client_timeout = opts.delete(:stale_client_timeout)
|
20
20
|
@redis = opts.delete(:redis) || Redis.new(opts)
|
21
|
+
@use_local_time = opts.delete(:use_local_time)
|
21
22
|
@tokens = []
|
22
23
|
end
|
23
24
|
|
@@ -184,8 +185,17 @@ class Redis
|
|
184
185
|
end
|
185
186
|
|
186
187
|
def current_time
|
187
|
-
|
188
|
-
|
188
|
+
if @use_local_time
|
189
|
+
Time.now
|
190
|
+
else
|
191
|
+
begin
|
192
|
+
instant = @redis.time
|
193
|
+
Time.at(instant[0], instant[1])
|
194
|
+
rescue
|
195
|
+
@use_local_time = true
|
196
|
+
current_time
|
197
|
+
end
|
198
|
+
end
|
189
199
|
end
|
190
200
|
end
|
191
201
|
end
|
data/spec/semaphore_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
3
3
|
describe "redis" do
|
4
4
|
before(:all) do
|
5
|
-
# use database 15 for testing so we dont accidentally step on
|
5
|
+
# use database 15 for testing so we dont accidentally step on your real data
|
6
6
|
@redis = Redis.new :db => 15
|
7
7
|
end
|
8
8
|
|
@@ -149,4 +149,26 @@ describe "redis" do
|
|
149
149
|
hyper_aggressive_sem.lock(1).should_not == false
|
150
150
|
end
|
151
151
|
end
|
152
|
+
|
153
|
+
describe "redis time" do
|
154
|
+
let(:semaphore) { Redis::Semaphore.new(:my_semaphore, :redis => @redis, :stale_client_timeout => 5) }
|
155
|
+
|
156
|
+
before(:all) do
|
157
|
+
Timecop.freeze(Time.local(1990))
|
158
|
+
end
|
159
|
+
|
160
|
+
it "with time support should return a different time than frozen time" do
|
161
|
+
semaphore.send(:current_time).should_not == Time.now
|
162
|
+
end
|
163
|
+
|
164
|
+
it "with use_local_time should return the same time as frozen time" do
|
165
|
+
semaphore = Redis::Semaphore.new(:my_semaphore, :redis => @redis, :stale_client_timeout => 5, :use_local_time => true)
|
166
|
+
semaphore.send(:current_time).should == Time.now
|
167
|
+
end
|
168
|
+
|
169
|
+
it "without time support should return the same time as frozen time" do
|
170
|
+
@redis.stub(:time) { raise Redis::CommandError }
|
171
|
+
semaphore.send(:current_time).should == Time.now
|
172
|
+
end
|
173
|
+
end
|
152
174
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-semaphore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06
|
12
|
+
date: 2013-08-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|