prop 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: db081ee0a96819c4b270c57e0205527b122d93b214b2c42af7d50159cfb140d1
4
- data.tar.gz: f8ef4abcc3cb6762b70a993fed0446321585a14e685cbb91a1e37f355fe507e1
2
+ SHA1:
3
+ metadata.gz: 850a96785d6d002acd243a5edf5cca965bd46bd1
4
+ data.tar.gz: 9ffb56011562feb0d9cf2c5ab62705420d59d46e
5
5
  SHA512:
6
- metadata.gz: 7ff690ae0b93ebe4f15d05bb5e8b2b7f5f983607a56a9c9d1ad04c19a9de1aaa21b38b70d5763b091b5db6cd10c9890b3eb33bd5ca910b055f682eb0b5b23b07
7
- data.tar.gz: 268563b7482c713097888855260875a19777cdd20982496bec4644dfd1b494dde6666d09b10789617fd8d905d9a18b98265dfebcf1609b20734c46ee56e1a24b
6
+ metadata.gz: 8cfee5d2c8af78a8a9273bdc873ba83ee6bb81237ca7cb253fce98dbf6180cff96f1004759e5d2fc4ee8e01364dddfae7f4b94c3909f547b08bcd3b109c9d7d2
7
+ data.tar.gz: 61e6d22651a9017e827d34220511fd8dd6dc60acaa69908968082d71a0ee6926859c0509f08b12fdf5a4d4310f83b95d0cf7b4137f713323d70fb88defcd0bbc
data/README.md CHANGED
@@ -37,6 +37,9 @@ Example: Limit on accepted emails per hour from a given user, by defining a thre
37
37
 
38
38
  ```ruby
39
39
  Prop.configure(:mails_per_hour, threshold: 100, interval: 1.hour, description: "Mail rate limit exceeded")
40
+
41
+ # Block requests by setting threshold to 0
42
+ Prop.configure(:mails_per_hour, threshold: 0, interval: 1.hour, description: "All mail is blocked")
40
43
  ```
41
44
 
42
45
  ```ruby
@@ -3,7 +3,7 @@ require "prop/limiter"
3
3
  require "forwardable"
4
4
 
5
5
  module Prop
6
- VERSION = "2.3.0"
6
+ VERSION = "2.4.0"
7
7
 
8
8
  # Short hand for accessing Prop::Limiter methods
9
9
  class << self
@@ -56,8 +56,8 @@ module Prop
56
56
  end
57
57
 
58
58
  def validate_options!(options)
59
- validate_positive_integer(options[:threshold], :threshold)
60
- validate_positive_integer(options[:interval], :interval)
59
+ validate_threshold(options[:threshold], :threshold)
60
+ validate_interval(options[:interval], :interval)
61
61
 
62
62
  amount = options[:increment] || options[:decrement]
63
63
  if amount
@@ -67,7 +67,11 @@ module Prop
67
67
 
68
68
  private
69
69
 
70
- def validate_positive_integer(option, key)
70
+ def validate_threshold(option, key)
71
+ raise ArgumentError.new("#{key.inspect} must be a non-negative Integer") if !option.is_a?(Integer) || option < 0
72
+ end
73
+
74
+ def validate_interval(option, key)
71
75
  raise ArgumentError.new("#{key.inspect} must be a positive Integer") if !option.is_a?(Integer) || option <= 0
72
76
  end
73
77
 
@@ -8,9 +8,10 @@ module Prop
8
8
  def counter(cache_key, options)
9
9
  bucket = Prop::Limiter.cache.read(cache_key) || zero_counter
10
10
  now = Time.now.to_i
11
- leak_amount = (now - bucket.fetch(:last_updated)) / options.fetch(:interval) * options.fetch(:threshold)
11
+ leak_rate = (now - bucket.fetch(:last_updated)) / options.fetch(:interval).to_f
12
+ leak_amount = leak_rate * options.fetch(:threshold)
12
13
 
13
- bucket[:bucket] = [bucket.fetch(:bucket) - leak_amount, 0].max
14
+ bucket[:bucket] = [(bucket.fetch(:bucket) - leak_amount).to_i, 0].max
14
15
  bucket[:last_updated] = now
15
16
  bucket
16
17
  end
@@ -46,8 +46,7 @@ module Prop
46
46
  #
47
47
  # Raises Prop::RateLimited if the number if the threshold for this handle has been reached
48
48
  def configure(handle, defaults)
49
- raise ArgumentError.new("Invalid threshold setting") unless defaults[:threshold].to_i > 0
50
- raise ArgumentError.new("Invalid interval setting") unless defaults[:interval].to_i > 0
49
+ Prop::Options.validate_options!(defaults)
51
50
 
52
51
  self.handles ||= {}
53
52
  self.handles[handle] = defaults
@@ -56,7 +55,7 @@ module Prop
56
55
  # Public: Disables Prop for a block of code
57
56
  #
58
57
  # block - a block of code within which Prop will not raise
59
- def disabled(&block)
58
+ def disabled(&_block)
60
59
  @disabled = true
61
60
  yield
62
61
  ensure
@@ -12,17 +12,24 @@ module Prop
12
12
  result = defaults.merge(params)
13
13
 
14
14
  result[:key] = Prop::Key.normalize(key)
15
+ result[:strategy] = get_strategy(result)
15
16
 
16
- result[:strategy] = if leaky_bucket.include?(result[:strategy])
17
+ result[:strategy].validate_options!(result)
18
+ result
19
+ end
20
+
21
+ def self.validate_options!(options)
22
+ get_strategy(options).validate_options!(options)
23
+ end
24
+
25
+ def self.get_strategy(options)
26
+ if leaky_bucket.include?(options[:strategy])
17
27
  Prop::LeakyBucketStrategy
18
- elsif result[:strategy] == nil
28
+ elsif options[:strategy] == nil
19
29
  Prop::IntervalStrategy
20
30
  else
21
- result[:strategy] # allowing any new/unknown strategy to be used
31
+ options[:strategy] # allowing any new/unknown strategy to be used
22
32
  end
23
-
24
- result[:strategy].validate_options!(result)
25
- result
26
33
  end
27
34
 
28
35
  def self.leaky_bucket
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.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Primdahl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-15 00:00:00.000000000 Z
11
+ date: 2019-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubyforge_project:
119
- rubygems_version: 2.7.6
119
+ rubygems_version: 2.6.14.4
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: Gem for implementing rate limits.