mlanett-redis-lock 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redis-lock.rb CHANGED
@@ -21,14 +21,16 @@ class Redis
21
21
  # @param key is a unique string identifying the object to lock, e.g. "user-1"
22
22
  # @param options[:life] may be set, but defaults to 1 minute
23
23
  # @param options[:owner] may be set, but defaults to HOSTNAME:PID
24
+ # @param options[:sleep] optional, number of milliseconds to sleep when lock is held, defaults to 125
24
25
  def initialize( redis, key, options = {} )
25
- check_keys( options, :owner, :life )
26
+ check_keys( options, :owner, :life, :sleep )
26
27
  @redis = redis
27
28
  @key = key
28
29
  @okey = "lock:owner:#{key}"
29
30
  @oval = options[:owner] || "#{`hostname`.strip}:#{Process.pid}"
30
31
  @xkey = "lock:expire:#{key}"
31
32
  @life = options[:life] || 60
33
+ @sleep_in_ms = options[:sleep] || 125
32
34
  end
33
35
 
34
36
  def lock( timeout = 10, &block )
@@ -167,11 +169,11 @@ class Redis
167
169
  # @returns true if successful, false otherwise
168
170
  def with_timeout( timeout, &block )
169
171
  expire = Time.now + timeout.to_f
170
- sleepy = 0.125
172
+ sleepy = @sleep_in_ms / 1000.to_f()
171
173
  # this looks inelegant compared to while Time.now < expire, but does not oversleep
172
174
  loop do
173
175
  return true if block.call
174
- log :debug, "Timeout" and return false if Time.now + sleepy > expire
176
+ log :debug, "Timeout for #{@key}" and return false if Time.now + sleepy > expire
175
177
  sleep(sleepy)
176
178
  # might like a different strategy, but general goal is not use 100% cpu while contending for a lock.
177
179
  # sleepy = [ sleepy * 2, ( expire - Time.now ) / 4 ].min
@@ -1,5 +1,5 @@
1
1
  class Redis
2
2
  class Lock
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mlanett-redis-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
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-02-12 00:00:00.000000000 Z
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -66,18 +66,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
66
  - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- segments:
70
- - 0
71
- hash: -3504077138606239435
72
69
  required_rubygems_version: !ruby/object:Gem::Requirement
73
70
  none: false
74
71
  requirements:
75
72
  - - ! '>='
76
73
  - !ruby/object:Gem::Version
77
74
  version: '0'
78
- segments:
79
- - 0
80
- hash: -3504077138606239435
81
75
  requirements: []
82
76
  rubyforge_project:
83
77
  rubygems_version: 1.8.23