redis-lock 0.1.2 → 0.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a7d5931088efd38747ab5b9c34d9ad2fd67fcdc
4
+ data.tar.gz: eccb2bdfa1a0b705b4927ba88857a3965e479307
5
+ SHA512:
6
+ metadata.gz: a48d69c32bd5eb05d13bbb6b12239c26dbf81d679760e811e770078450232e22a9385e699f61ddb850554a085aaade1eca2fb9a52875af4c44e9f43f5f1e569b
7
+ data.tar.gz: c1b857647f101f6be722eeebaf474dc9cf7d984f49760e7b9edd4afc819475ec8d185602aaba128aac6c1b1b1f11bc99e4c3633096b165be611a318d8698ac5d
@@ -4,4 +4,4 @@ class Redis
4
4
  include Redis::Lock
5
5
  end
6
6
 
7
- class RedisLockException < Exception; end
7
+ class RedisLockException < StandardError; end
@@ -1,5 +1,5 @@
1
- module Redis
1
+ class Redis
2
2
  module Lock
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -34,7 +34,7 @@ class Redis
34
34
  current_lock_key = lock_key(key)
35
35
  expiration_value = lock_expiration(timeout)
36
36
  attempt_counter = 0
37
- begin
37
+ while attempt_counter < max_attempts
38
38
  if self.setnx(current_lock_key, expiration_value)
39
39
  return true
40
40
  else
@@ -44,24 +44,15 @@ class Redis
44
44
  return true if compare_value == current_lock
45
45
  end
46
46
  end
47
-
48
- raise RedisLockException.new("Unable to acquire lock for #{key}.")
49
- rescue Exception => e
50
- if e.message == "Unable to acquire lock for #{key}."
51
- attempt_counter += 1
52
- if attempt_counter == max_attempts
53
- raise
54
- else
55
- sleep 1
56
- retry
57
- end
58
- else
59
- raise
60
- end
47
+
48
+ attempt_counter += 1
49
+ sleep 1 if attempt_counter < max_attempts
61
50
  end
51
+
52
+ raise RedisLockException.new("Unable to acquire lock for #{key}.")
62
53
  end
63
54
 
64
- # Unlock a previously locked key if it has not expired and the current process was the one that locked it.
55
+ # Unlock a previously locked key if it has not expired and the current process/thread was the one that locked it.
65
56
  #
66
57
  # Example:
67
58
  #
@@ -71,8 +62,8 @@ class Redis
71
62
  current_lock_key = lock_key(key)
72
63
  lock_value = self.get(current_lock_key)
73
64
  return true unless lock_value
74
- lock_timeout, lock_holder = lock_value.split('-')
75
- if (lock_timeout.to_i > Time.now.to_i) && (lock_holder.to_i == Process.pid)
65
+ lock_timeout, lock_process, lock_thread = lock_value.split('-')
66
+ if (lock_timeout.to_i > Time.now.to_i) && (lock_process.to_i == Process.pid) && lock_thread.to_i == Thread.current.object_id
76
67
  self.del(current_lock_key)
77
68
  return true
78
69
  else
@@ -83,7 +74,7 @@ class Redis
83
74
  private
84
75
 
85
76
  def lock_expiration(timeout)
86
- "#{Time.now.to_i + timeout + 1}-#{Process.pid}"
77
+ "#{Time.now.to_i + timeout + 1}-#{Process.pid}-#{Thread.current.object_id}"
87
78
  end
88
79
 
89
80
  def lock_key(key)
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.description = %q{Pessimistic locking for ruby redis}
12
12
  gem.summary = %q{Pessimistic locking for ruby redis}
13
13
  gem.homepage = "https://github.com/PatrickTulskie/redis-lock"
14
+ gem.license = "MIT"
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -44,26 +44,26 @@ describe 'redis' do
44
44
  end
45
45
 
46
46
  it "should raise an exception if unable to acquire lock" do
47
- @redis.lock('test_key', 9000)
48
- lambda { @redis.lock('test_key', 9000, 1) }.should raise_exception("Unable to acquire lock for test_key.")
47
+ @redis.lock('test_key', 1000)
48
+ lambda { @redis.lock('test_key', 1000, 1) }.should raise_exception("Unable to acquire lock for test_key.")
49
49
  end
50
50
 
51
51
  it "should execute a block during a lock_for_update transaction" do
52
- @redis.lock_for_update('test_key', 9000) { @redis.set('test_key', 'awesome') }
52
+ @redis.lock_for_update('test_key', 1000) { @redis.set('test_key', 'awesome') }
53
53
  @redis.get('test_key').should == 'awesome'
54
54
  end
55
55
 
56
56
  it "should unlock at the end of a lock_for_update" do
57
- @redis.lock_for_update('test_key', 9000) { @redis.set('test_key', 'awesome') }
57
+ @redis.lock_for_update('test_key', 1000) { @redis.set('test_key', 'awesome') }
58
58
  @redis.get('lock:test_key').should be_nil
59
59
  end
60
60
 
61
61
  it "should keep trying to lock a key" do
62
62
  time = DateTime.now
63
- @redis.lock('test_key', 9000)
64
- lambda { @redis.lock('test_key', 9000, 10) }.should raise_exception("Unable to acquire lock for test_key.")
65
- # Should have spent 9 seconds trying to lock
66
- DateTime.now.should >= time + Rational(9, 86400)
63
+ @redis.lock('test_key', 1000)
64
+ lambda { @redis.lock('test_key', 1000, 2) }.should raise_exception("Unable to acquire lock for test_key.")
65
+ # Should have spent 1 second trying to lock
66
+ DateTime.now.should >= time + Rational(1, 86400)
67
67
  end
68
68
 
69
69
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Patrick Tulskie
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-30 00:00:00.000000000 Z
11
+ date: 2014-06-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: redis
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Pessimistic locking for ruby redis
@@ -50,7 +45,7 @@ executables: []
50
45
  extensions: []
51
46
  extra_rdoc_files: []
52
47
  files:
53
- - .gitignore
48
+ - ".gitignore"
54
49
  - Gemfile
55
50
  - Gemfile.lock
56
51
  - LICENSE.txt
@@ -63,28 +58,28 @@ files:
63
58
  - spec/redis_spec.rb
64
59
  - spec/spec_helper.rb
65
60
  homepage: https://github.com/PatrickTulskie/redis-lock
66
- licenses: []
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
67
64
  post_install_message:
68
65
  rdoc_options: []
69
66
  require_paths:
70
67
  - lib
71
68
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
69
  requirements:
74
- - - ! '>='
70
+ - - ">="
75
71
  - !ruby/object:Gem::Version
76
72
  version: '0'
77
73
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
74
  requirements:
80
- - - ! '>='
75
+ - - ">="
81
76
  - !ruby/object:Gem::Version
82
77
  version: '0'
83
78
  requirements: []
84
79
  rubyforge_project:
85
- rubygems_version: 1.8.24
80
+ rubygems_version: 2.2.2
86
81
  signing_key:
87
- specification_version: 3
82
+ specification_version: 4
88
83
  summary: Pessimistic locking for ruby redis
89
84
  test_files:
90
85
  - spec/redis_spec.rb