berater 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/berater/base_limiter.rb +1 -1
- data/lib/berater/concurrency_limiter.rb +6 -5
- data/lib/berater/version.rb +1 -1
- data/spec/concurrency_lock_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3452315467fe2c079071179703283dd9ea92eb8e6abcc959905f40e16f3e2414
|
4
|
+
data.tar.gz: 90d783190f15f57c3ac44dc2afc89fb5a000a392163ab9a8351ff498e323901a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 228f67adfb24027a6ea3c7b090c64aecee1a38e64ed00f7272b0c1af273dbcc73bbec8112fbb15595133cd450b45ac10f603e30ad252218d41e66d5206cc549d
|
7
|
+
data.tar.gz: 9068fd600128bb11f718318fa41f1030d17cd5ad36b9ce5884cbdc5f105fe56f92da73568dc60f12f6e1c7a64402652381259a64524c80889465d4c794a91e69
|
data/lib/berater/base_limiter.rb
CHANGED
@@ -33,11 +33,12 @@ module Berater
|
|
33
33
|
end
|
34
34
|
|
35
35
|
class Lock
|
36
|
-
attr_reader :limiter, :id
|
36
|
+
attr_reader :limiter, :id, :contention
|
37
37
|
|
38
|
-
def initialize(limiter, id)
|
38
|
+
def initialize(limiter, id, contention)
|
39
39
|
@limiter = limiter
|
40
40
|
@id = id
|
41
|
+
@contention = contention
|
41
42
|
@released = false
|
42
43
|
@locked_at = Time.now
|
43
44
|
end
|
@@ -127,11 +128,11 @@ module Berater
|
|
127
128
|
|
128
129
|
raise Incapacitated unless lock_id
|
129
130
|
|
130
|
-
lock = Lock.new(self, lock_id)
|
131
|
+
lock = Lock.new(self, lock_id, count)
|
131
132
|
|
132
133
|
if block_given?
|
133
134
|
begin
|
134
|
-
yield
|
135
|
+
yield lock
|
135
136
|
ensure
|
136
137
|
release(lock)
|
137
138
|
end
|
@@ -142,7 +143,7 @@ module Berater
|
|
142
143
|
|
143
144
|
def release(lock)
|
144
145
|
res = redis.zrem(key, lock.id)
|
145
|
-
res == true || res == 1
|
146
|
+
res == true || res == 1 # depending on which version of Redis
|
146
147
|
end
|
147
148
|
|
148
149
|
end
|
data/lib/berater/version.rb
CHANGED
@@ -31,4 +31,20 @@ describe Berater::ConcurrencyLimiter::Lock do
|
|
31
31
|
it { expect(subject.released?).to be false }
|
32
32
|
end
|
33
33
|
|
34
|
+
describe '#contention' do
|
35
|
+
let(:limiter) { Berater.new(:concurrency, 3) }
|
36
|
+
|
37
|
+
it 'tracks contention' do
|
38
|
+
lock_1 = limiter.limit
|
39
|
+
expect(lock_1.contention).to eq 1
|
40
|
+
|
41
|
+
lock_2 = limiter.limit
|
42
|
+
expect(lock_2.contention).to eq 2
|
43
|
+
|
44
|
+
limiter.limit do |lock_3|
|
45
|
+
expect(lock_3.contention).to eq 3
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
34
50
|
end
|