prop 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/prop.rb +1 -1
- data/lib/prop/interval_strategy.rb +2 -4
- data/lib/prop/leaky_bucket_strategy.rb +2 -5
- data/lib/prop/limiter.rb +4 -1
- 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: 65fa8acae93ced0dce793a91e6e050268560387d
|
4
|
+
data.tar.gz: b5086f3161ff68f0f27907360861d602184de0d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 702914150a4438bfe43f5d4ef9ea3abc2686d4c1705d4072864b449790797fbecd8cb6133e89044286b5eab6ffc02d38807ee6393e245f93b692bedf4cbc3dee
|
7
|
+
data.tar.gz: 0d7cfde00a0c68bb2ac1ba644f603cc9ae91c6603e875df6b1eb5f0491473ec8db43411fcbb21116b69bbe2f318a7f60b45f9746b97899db921607d726cf14db
|
data/README.md
CHANGED
@@ -158,7 +158,6 @@ Prop.throttle!(:execute_time, account.id, increment: (Benchmark.realtime { execu
|
|
158
158
|
```
|
159
159
|
|
160
160
|
Decrement can be used to for example throttle before an expensive action and then give quota back when some condition is met.
|
161
|
-
`:decrement` is only supported for `IntervalStrategy` for now
|
162
161
|
|
163
162
|
```ruby
|
164
163
|
Prop.throttle!(:api_counts, request.remote_ip, decrement: 1)
|
data/lib/prop.rb
CHANGED
@@ -13,10 +13,8 @@ module Prop
|
|
13
13
|
Prop::Limiter.cache.read(cache_key).to_i
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
-(options.fetch(:decrement)) :
|
19
|
-
options.fetch(:increment, 1)
|
16
|
+
# options argument is kept for api consistency for all strategies
|
17
|
+
def change(cache_key, amount, options = {})
|
20
18
|
raise ArgumentError, "Change amount must be a Fixnum, was #{amount.class}" unless amount.is_a?(Fixnum)
|
21
19
|
cache = Prop::Limiter.cache
|
22
20
|
cache.increment(cache_key, amount) || (cache.write(cache_key, amount, raw: true) && amount) # WARNING: potential race condition
|
@@ -17,12 +17,9 @@ 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 change(cache_key, options)
|
21
|
-
# please open a PR if you know how to support this
|
22
|
-
raise ArgumentError, "decrement is not supported for LeakyBucketStrategy" if options.key?(:decrement)
|
23
|
-
|
20
|
+
def change(cache_key, amount, options)
|
24
21
|
counter = counter(cache_key, options)
|
25
|
-
counter[:bucket] +=
|
22
|
+
counter[:bucket] += amount
|
26
23
|
Prop::Limiter.cache.write(cache_key, counter)
|
27
24
|
counter
|
28
25
|
end
|
data/lib/prop/limiter.rb
CHANGED
@@ -146,7 +146,10 @@ module Prop
|
|
146
146
|
def _throttle(handle, key, cache_key, options)
|
147
147
|
return [false, @strategy.zero_counter] if disabled?
|
148
148
|
|
149
|
-
|
149
|
+
amount = options.key?(:decrement) ?
|
150
|
+
-(options.fetch(:decrement)) :
|
151
|
+
options.fetch(:increment, 1)
|
152
|
+
counter = @strategy.change(cache_key, amount, options)
|
150
153
|
|
151
154
|
if @strategy.compare_threshold?(counter, :>, options)
|
152
155
|
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.1
|
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-08-
|
11
|
+
date: 2016-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|