mlanett-redis-lock 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/redis-lock.rb +5 -3
- data/lib/redis-lock/version.rb +1 -1
- data/spec/helper.rb +1 -1
- data/test/stress.rb +3 -13
- metadata +4 -4
data/README.md
CHANGED
@@ -30,14 +30,14 @@ rather than acquiring the lock with a very long lifetime which will result in lo
|
|
30
30
|
|
31
31
|
A lock needs an owner. Redis::Lock defaults to using an owner id of HOSTNAME:PID.
|
32
32
|
|
33
|
-
A lock may need more than one attempt to acquire it. Redis::Lock offers a timeout; this defaults to
|
33
|
+
A lock may need more than one attempt to acquire it. Redis::Lock offers a timeout; this defaults to 10 seconds.
|
34
34
|
It uses exponential backoff with sleeps so it's fairly safe to use longer timeouts.
|
35
35
|
|
36
36
|
## Usage
|
37
37
|
|
38
38
|
This gem adds lock() and unlock() to Redis instances.
|
39
39
|
lock() takes a block and is safer than using lock() and unlock() separately.
|
40
|
-
lock() takes a key and lifetime and optionally a timeout (otherwise defaulting to
|
40
|
+
lock() takes a key and lifetime and optionally a timeout (otherwise defaulting to 10 second).
|
41
41
|
|
42
42
|
redis.lock("test") { do_something }
|
43
43
|
|
data/lib/redis-lock.rb
CHANGED
@@ -31,7 +31,7 @@ class Redis
|
|
31
31
|
@life = options[:life] || 60
|
32
32
|
end
|
33
33
|
|
34
|
-
def lock( timeout =
|
34
|
+
def lock( timeout = 10, &block )
|
35
35
|
do_lock_with_timeout(timeout) or raise LockNotAcquired.new(key)
|
36
36
|
if block then
|
37
37
|
begin
|
@@ -214,8 +214,10 @@ class Redis
|
|
214
214
|
|
215
215
|
# Convenience methods
|
216
216
|
|
217
|
-
|
218
|
-
|
217
|
+
# @option timeout defaults to 10 seconds
|
218
|
+
def lock( key, options = {}, &block )
|
219
|
+
acquire = options.delete(:acquire) || 10
|
220
|
+
Lock.new( self, key, options ).lock( acquire, &block )
|
219
221
|
end
|
220
222
|
|
221
223
|
def unlock( key )
|
data/lib/redis-lock/version.rb
CHANGED
data/spec/helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
|
3
3
|
require "bundler/setup" # set up gem paths
|
4
|
-
require "ruby-debug" # because sometimes you need it
|
4
|
+
#require "ruby-debug" # because sometimes you need it
|
5
5
|
|
6
6
|
require "simplecov" # code coverage
|
7
7
|
SimpleCov.start # must be loaded before our own code
|
data/test/stress.rb
CHANGED
@@ -23,18 +23,6 @@ OptionParser.new do |opts|
|
|
23
23
|
opts.on( "-h", "--help", "Display this usage summary" ) { puts opts; exit }
|
24
24
|
end.parse!
|
25
25
|
|
26
|
-
class Thing
|
27
|
-
attr :id
|
28
|
-
attr :activity
|
29
|
-
def initialize( id, activity )
|
30
|
-
@id = id
|
31
|
-
@activity = activity
|
32
|
-
end
|
33
|
-
def process
|
34
|
-
Kernel.sleep( rand activity )
|
35
|
-
end
|
36
|
-
end # Thing
|
37
|
-
|
38
26
|
class Runner
|
39
27
|
|
40
28
|
attr :options
|
@@ -48,7 +36,7 @@ class Runner
|
|
48
36
|
end
|
49
37
|
|
50
38
|
def test( key, time )
|
51
|
-
redis.lock( key, time, life: time*2 ) do
|
39
|
+
redis.lock( key, acquire: time, life: time*2 ) do
|
52
40
|
val1 = rand(65536)
|
53
41
|
redis.set( "#{key}:widget", val1 )
|
54
42
|
Kernel.sleep( time )
|
@@ -99,6 +87,8 @@ end
|
|
99
87
|
|
100
88
|
# main
|
101
89
|
|
90
|
+
puts "[#{Process.pid}] Starting with #{options.inspect}"
|
91
|
+
|
102
92
|
redis = ::Redis.connect(TEST_REDIS)
|
103
93
|
redis.flushall # clean before run
|
104
94
|
redis.client.disconnect # don't keep when forking
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-07 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
|
-
requirement: &
|
16
|
+
requirement: &70349318263680 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70349318263680
|
25
25
|
description: Pessimistic locking using Redis
|
26
26
|
email:
|
27
27
|
- mark.lanett@gmail.com
|