redis-semaphore 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Code Climate](https://codeclimate.com/github/dv/redis-semaphore.png)](https://codeclimate.com/github/dv/redis-semaphore)
2
+
1
3
  redis-semaphore
2
4
  ===============
3
5
 
@@ -99,16 +101,16 @@ To allow for clients to die, and the token returned to the list, a stale-check w
99
101
  There are two ways to take advantage of this. You can either define a :stale\_client\_timeout upon initialization. This will check for stale locks everytime your program wants to lock the semaphore:
100
102
 
101
103
  ```ruby
102
- s = Redis::Semaphore(:stale_semaphore, :redis = r, :stale_client_timeout => 1000) # in ms
104
+ s = Redis::Semaphore.new(:stale_semaphore, :redis = r, :stale_client_timeout => 5) # in seconds
103
105
  ```
104
106
 
105
107
  Or you could start a different thread or program that frequently checks for stale locks. This has the advantage of unblocking blocking calls to Semaphore#lock as well:
106
108
 
107
109
  ```ruby
108
- normal_sem = Redis::Semaphore(:semaphore, :connection => "localhost")
110
+ normal_sem = Redis::Semaphore.new(:semaphore, :connection => "localhost")
109
111
 
110
112
  Thread.new do
111
- watchdog = Redis::Semaphore(:semaphore, :connection => "localhost", :stale_client_timeout => 1000)
113
+ watchdog = Redis::Semaphore.new(:semaphore, :connection => "localhost", :stale_client_timeout => 5)
112
114
 
113
115
  while(true) do
114
116
  watchdog.release_stale_locks!
@@ -172,9 +174,12 @@ Testing
172
174
  Changelog
173
175
  ---------
174
176
 
177
+ ###0.1.7 April 18, 2013
178
+ - Fix bug where ```release\_stale\_locks!``` was not public (thanks scomma!).
179
+
175
180
  ###0.1.6 March 31, 2013
176
181
  - Add non-ownership of tokens
177
- - Add stale client timeout (thanks timgaleckas!)
182
+ - Add stale client timeout (thanks timgaleckas!).
178
183
 
179
184
  ###0.1.5 October 1, 2012
180
185
  - Add detection of Redis::Namespace definition to avoid potential bug (thanks ruud!).
@@ -202,8 +207,9 @@ Author
202
207
  Contributors
203
208
  ------------
204
209
 
205
- Thanks to these awesome fellas for their contributions:
210
+ Thanks to these awesome peeps for their contributions:
206
211
 
207
212
  - [Rimas Silkaitis](https://github.com/neovintage)
208
213
  - [Tim Galeckas](https://github.com/timgaleckas)
209
214
  - [Ruurd Pels](https://github.com/ruurd)
215
+ - [Prathan Thananart](https://github.com/scomma)
@@ -112,6 +112,18 @@ class Redis
112
112
  end
113
113
  end
114
114
 
115
+ def release_stale_locks!
116
+ simple_mutex(:release_locks, 10) do
117
+ @redis.hgetall(grabbed_key).each do |token, locked_at|
118
+ timed_out_at = locked_at.to_f + @stale_client_timeout
119
+
120
+ if timed_out_at < Time.now.to_f
121
+ signal(token)
122
+ end
123
+ end
124
+ end
125
+ end
126
+
115
127
  private
116
128
  def simple_mutex(key_name, expires = nil)
117
129
  key_name = namespaced_key(key_name) if key_name.kind_of? Symbol
@@ -127,18 +139,6 @@ class Redis
127
139
  end
128
140
  end
129
141
 
130
- def release_stale_locks!
131
- simple_mutex(:release_locks, 10) do
132
- @redis.hgetall(grabbed_key).each do |token, locked_at|
133
- timed_out_at = locked_at.to_i + @stale_client_timeout
134
-
135
- if timed_out_at < Time.now.to_i
136
- signal(token)
137
- end
138
- end
139
- end
140
- end
141
-
142
142
  def create!
143
143
  @redis.expire(exists_key, 10)
144
144
 
@@ -118,6 +118,21 @@ describe "redis" do
118
118
 
119
119
  semaphore.available_count.should == 1
120
120
  end
121
+
122
+ it "can have stale locks released by a third process" do
123
+ watchdog = Redis::Semaphore.new(:my_semaphore, :redis => @redis, :stale_client_timeout => 1)
124
+ semaphore.lock
125
+
126
+ sleep 0.5
127
+
128
+ watchdog.release_stale_locks!
129
+ semaphore.locked?.should == true
130
+
131
+ sleep 0.6
132
+
133
+ watchdog.release_stale_locks!
134
+ semaphore.locked?.should == false
135
+ end
121
136
  end
122
137
 
123
138
  describe "semaphore with staleness checking" do
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.1.6
4
+ version: 0.1.7
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-03-30 00:00:00.000000000 Z
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis