redis_locks 0.1.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d50bb61032babb4eb127f5f4cf8a4b8804d84c0
4
- data.tar.gz: 658610bbd101c4bce5004415c08517a44b23dd35
3
+ metadata.gz: 518aef2c939e2b41ead60befec91c025b82c65f6
4
+ data.tar.gz: 52d9fc52e779061090109b865a919536f939e5d0
5
5
  SHA512:
6
- metadata.gz: b512b7584dc46896361d1c622a1c183ba6efc3ec5c22144e1162e614f47bc0bbe0c7e9e93bcb8323a1103982ceb26cd97df538d0f375b92a1b66eef13f675a60
7
- data.tar.gz: 461ba90c3336dc56296053475a24141a1f0454e6cbb96cfe2d16c590eee7b7080147559a369a5713020f2af334495b7c7bb522737358930bffcc90242cca7619
6
+ metadata.gz: d800f5854643134b3d4e18da510e6cb5674a5abd7452f9610bb88a321cfab5958b7d5e04a5aba5a196a2dd8d3ab778f77c4b83907664b68f11f3c9cd0172fa1c
7
+ data.tar.gz: a3ec08d9ddbc21735a4091b8067847a472160be86bafe686ab5877535953616ca525cca62c71adcb52750aa98cd3012c2e64201cb3255890051fac89fe392ae6
@@ -1,4 +1,5 @@
1
1
  require 'redis_locks/version'
2
+ require 'redis_locks/lock_error'
2
3
  require 'redis_locks/resource_unavailable'
3
4
  require 'redis_locks/evalsha_or_eval'
4
5
  require 'redis_locks/mutex'
@@ -0,0 +1,4 @@
1
+ module RedisLocks
2
+ class LockError < RuntimeError
3
+ end
4
+ end
@@ -5,6 +5,12 @@ module RedisLocks
5
5
  end
6
6
  end
7
7
 
8
+ class MutexExpired < LockError
9
+ def initialize(key)
10
+ super("Mutex [#{key}] has expired!")
11
+ end
12
+ end
13
+
8
14
  class Mutex
9
15
 
10
16
  NAMESPACE = "mutex"
@@ -59,13 +65,23 @@ module RedisLocks
59
65
 
60
66
  # To prevent deleting a lock acquired from another process, only delete
61
67
  # the key if it's still valid, and will be for another 2 seconds
62
- if Time.now.utc.to_i - 2 < @expires_at
68
+ if !expired?(safety_margin: 2)
63
69
  @redis.with { |conn| conn.del(@key) }
64
70
  end
65
71
 
66
72
  @expires_at = nil
67
73
  end
68
74
 
75
+ def expired?(safety_margin: 0)
76
+ return true unless @expires_at
77
+
78
+ Time.now.utc.to_i + safety_margin >= @expires_at
79
+ end
80
+
81
+ def not_expired!(safety_margin: 0)
82
+ raise MutexExpired.new(@key) if expired?(safety_margin: safety_margin)
83
+ end
84
+
69
85
  private
70
86
 
71
87
  def return_or_yield
@@ -1,4 +1,4 @@
1
1
  module RedisLocks
2
- class ResourceUnavailable < RuntimeError
2
+ class ResourceUnavailable < LockError
3
3
  end
4
4
  end
@@ -1,3 +1,3 @@
1
1
  module RedisLocks
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -45,6 +45,22 @@ describe RedisLocks::Mutex do
45
45
  expect { mutex.lock! }.to raise_error(RedisLocks::AlreadyLocked)
46
46
  end
47
47
 
48
+ it 'says not expired' do
49
+ expect(mutex.expired?).to be_falsey
50
+ end
51
+
52
+ it 'says not expired with small safety margin' do
53
+ expect(mutex.expired?(safety_margin: 2)).to be_falsey
54
+ end
55
+
56
+ it 'says expired with large safety margin' do
57
+ expect(mutex.expired?(safety_margin: 60 * 60 * 24 * 365)).to be_truthy
58
+ end
59
+
60
+ it 'asserts not_expired!' do
61
+ mutex.not_expired!
62
+ end
63
+
48
64
  context 'and then unlocked' do
49
65
  before do
50
66
  mutex.unlock
@@ -65,5 +81,13 @@ describe RedisLocks::Mutex do
65
81
  it 'allows lock' do
66
82
  expect(mutex.lock).to be_truthy
67
83
  end
84
+
85
+ it 'says expired' do
86
+ expect(mutex.expired?).to be_truthy
87
+ end
88
+
89
+ it 'fails not_expired!' do
90
+ expect { mutex.not_expired! }.to raise_error(RedisLocks::MutexExpired)
91
+ end
68
92
  end
69
93
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_locks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Academia.edu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-25 00:00:00.000000000 Z
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -83,6 +83,7 @@ files:
83
83
  - lib/redis_locks.rb
84
84
  - lib/redis_locks/connections.rb
85
85
  - lib/redis_locks/evalsha_or_eval.rb
86
+ - lib/redis_locks/lock_error.rb
86
87
  - lib/redis_locks/mutex.rb
87
88
  - lib/redis_locks/resource_unavailable.rb
88
89
  - lib/redis_locks/semaphore.rb
@@ -117,9 +118,5 @@ rubygems_version: 2.4.5.1
117
118
  signing_key:
118
119
  specification_version: 4
119
120
  summary: Various locking utilities for Ruby using Redis
120
- test_files:
121
- - spec/redis_locks/mutex_spec.rb
122
- - spec/redis_locks/semaphore_spec.rb
123
- - spec/redis_locks/token_bucket_spec.rb
124
- - spec/spec_helper.rb
121
+ test_files: []
125
122
  has_rdoc: