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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65fa8acae93ced0dce793a91e6e050268560387d
4
- data.tar.gz: b5086f3161ff68f0f27907360861d602184de0d2
3
+ metadata.gz: 498e7350ae324646b31fc30a658b2443db9d3c63
4
+ data.tar.gz: 0dcb095217edd32a792d28ce7d2f4f1a0326a161
5
5
  SHA512:
6
- metadata.gz: 702914150a4438bfe43f5d4ef9ea3abc2686d4c1705d4072864b449790797fbecd8cb6133e89044286b5eab6ffc02d38807ee6393e245f93b692bedf4cbc3dee
7
- data.tar.gz: 0d7cfde00a0c68bb2ac1ba644f603cc9ae91c6603e875df6b1eb5f0491473ec8db43411fcbb21116b69bbe2f318a7f60b45f9746b97899db921607d726cf14db
6
+ metadata.gz: 58474567c9da9f71375bb6d6f8495cb3a6de8c40d80745893666d1f76a68c76cf37d6cec7c193ff09ba23531e9cdcbec2f1a67211b8ace2c1a374853cfbd9b31
7
+ data.tar.gz: 296742370594a33d4247a3dc90b61597e27dac960eb56701bd7b18661b110673022ed27222ea58de7230b547e8f2bb1f684ed44ed47cf035ba88bca2a62ec4e5
@@ -3,7 +3,7 @@ require "prop/limiter"
3
3
  require "forwardable"
4
4
 
5
5
  module Prop
6
- VERSION = "2.2.1"
6
+ VERSION = "2.2.2"
7
7
 
8
8
  # Short hand for accessing Prop::Limiter methods
9
9
  class << self
@@ -10,18 +10,22 @@ module Prop
10
10
  end
11
11
 
12
12
  def counter(cache_key, options)
13
- Prop::Limiter.cache.read(cache_key).to_i
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 change(cache_key, amount, options = {})
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
- Prop::Limiter.cache.write(cache_key, zero_counter, raw: true)
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 change(cache_key, amount, options)
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
@@ -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
- amount = options.key?(:decrement) ?
150
- -(options.fetch(:decrement)) :
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.1
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-08-29 00:00:00.000000000 Z
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake