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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5a9acb5c4023850aae92780dca0a51f4ec7e6cc
4
- data.tar.gz: b237584c3a9d4e78d8f525d858bc8d1a7a6ecfc2
3
+ metadata.gz: 65fa8acae93ced0dce793a91e6e050268560387d
4
+ data.tar.gz: b5086f3161ff68f0f27907360861d602184de0d2
5
5
  SHA512:
6
- metadata.gz: dab07c90b327eff3b46f96f2b7324aaeb9f63f3888fb2c61ab46b10b0403366f2cddfdf493bfdec32ee0e678df6ae0f7bbf9aaae792e91106a620c51f436d76a
7
- data.tar.gz: e47254422205e73130f9c604d613d0e6cf39105c11ff8757fc2967237e81745a9b2cce95424d2931ad733c3bc2413586379f7e4446e3b72feb1270032153fb88
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
@@ -3,7 +3,7 @@ require "prop/limiter"
3
3
  require "forwardable"
4
4
 
5
5
  module Prop
6
- VERSION = "2.2.0"
6
+ VERSION = "2.2.1"
7
7
 
8
8
  # Short hand for accessing Prop::Limiter methods
9
9
  class << self
@@ -13,10 +13,8 @@ module Prop
13
13
  Prop::Limiter.cache.read(cache_key).to_i
14
14
  end
15
15
 
16
- def change(cache_key, options)
17
- amount = options.key?(:decrement) ?
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] += options.fetch(:increment, 1)
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
- counter = @strategy.change(cache_key, options)
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.0
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-24 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake