prop 2.2.1 → 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/prop.rb +1 -1
- data/lib/prop/interval_strategy.rb +12 -4
- data/lib/prop/leaky_bucket_strategy.rb +9 -1
- data/lib/prop/limiter.rb +4 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 498e7350ae324646b31fc30a658b2443db9d3c63
|
4
|
+
data.tar.gz: 0dcb095217edd32a792d28ce7d2f4f1a0326a161
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58474567c9da9f71375bb6d6f8495cb3a6de8c40d80745893666d1f76a68c76cf37d6cec7c193ff09ba23531e9cdcbec2f1a67211b8ace2c1a374853cfbd9b31
|
7
|
+
data.tar.gz: 296742370594a33d4247a3dc90b61597e27dac960eb56701bd7b18661b110673022ed27222ea58de7230b547e8f2bb1f684ed44ed47cf035ba88bca2a62ec4e5
|
data/lib/prop.rb
CHANGED
@@ -10,18 +10,22 @@ module Prop
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def counter(cache_key, options)
|
13
|
-
|
13
|
+
cache.read(cache_key).to_i
|
14
14
|
end
|
15
15
|
|
16
16
|
# options argument is kept for api consistency for all strategies
|
17
|
-
def
|
17
|
+
def increment(cache_key, amount, options = {})
|
18
18
|
raise ArgumentError, "Change amount must be a Fixnum, was #{amount.class}" unless amount.is_a?(Fixnum)
|
19
|
-
cache = Prop::Limiter.cache
|
20
19
|
cache.increment(cache_key, amount) || (cache.write(cache_key, amount, raw: true) && amount) # WARNING: potential race condition
|
21
20
|
end
|
22
21
|
|
22
|
+
def decrement(cache_key, amount, options = {})
|
23
|
+
raise ArgumentError, "Change amount must be a Fixnum, was #{amount.class}" unless amount.is_a?(Fixnum)
|
24
|
+
cache.decrement(cache_key, amount) || (cache.write(cache_key, 0, raw: true) && 0) # WARNING: potential race condition
|
25
|
+
end
|
26
|
+
|
23
27
|
def reset(cache_key)
|
24
|
-
|
28
|
+
cache.write(cache_key, zero_counter, raw: true)
|
25
29
|
end
|
26
30
|
|
27
31
|
def compare_threshold?(counter, operator, options)
|
@@ -66,6 +70,10 @@ module Prop
|
|
66
70
|
def validate_positive_integer(option, key)
|
67
71
|
raise ArgumentError.new("#{key.inspect} must be a positive Integer") if !option.is_a?(Fixnum) || option <= 0
|
68
72
|
end
|
73
|
+
|
74
|
+
def cache
|
75
|
+
Prop::Limiter.cache
|
76
|
+
end
|
69
77
|
end
|
70
78
|
end
|
71
79
|
end
|
@@ -17,13 +17,21 @@ module Prop
|
|
17
17
|
|
18
18
|
# WARNING: race condition
|
19
19
|
# this increment is not atomic, so it might miss counts when used frequently
|
20
|
-
def
|
20
|
+
def increment(cache_key, amount, options)
|
21
21
|
counter = counter(cache_key, options)
|
22
22
|
counter[:bucket] += amount
|
23
23
|
Prop::Limiter.cache.write(cache_key, counter)
|
24
24
|
counter
|
25
25
|
end
|
26
26
|
|
27
|
+
def decrement(cache_key, amount, options)
|
28
|
+
counter = counter(cache_key, options)
|
29
|
+
counter[:bucket] -= amount
|
30
|
+
counter[:bucket] = 0 unless counter[:bucket] > 0
|
31
|
+
Prop::Limiter.cache.write(cache_key, counter)
|
32
|
+
counter
|
33
|
+
end
|
34
|
+
|
27
35
|
def reset(cache_key)
|
28
36
|
Prop::Limiter.cache.write(cache_key, zero_counter)
|
29
37
|
end
|
data/lib/prop/limiter.rb
CHANGED
@@ -20,7 +20,7 @@ module Prop
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def cache=(cache)
|
23
|
-
[:read, :write, :increment].each do |method|
|
23
|
+
[:read, :write, :increment, :decrement].each do |method|
|
24
24
|
next if cache.respond_to?(method)
|
25
25
|
raise ArgumentError, "Cache needs to respond to #{method}"
|
26
26
|
end
|
@@ -146,10 +146,9 @@ module Prop
|
|
146
146
|
def _throttle(handle, key, cache_key, options)
|
147
147
|
return [false, @strategy.zero_counter] if disabled?
|
148
148
|
|
149
|
-
|
150
|
-
|
151
|
-
options.fetch(:increment, 1)
|
152
|
-
counter = @strategy.change(cache_key, amount, options)
|
149
|
+
counter = options.key?(:decrement) ?
|
150
|
+
@strategy.decrement(cache_key, options.fetch(:decrement), options) :
|
151
|
+
@strategy.increment(cache_key, options.fetch(:increment, 1), options)
|
153
152
|
|
154
153
|
if @strategy.compare_threshold?(counter, :>, options)
|
155
154
|
before_throttle_callback &&
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morten Primdahl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|