prop 2.8.0 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -1
  3. data/lib/prop/limiter.rb +11 -2
  4. data/lib/prop.rb +2 -2
  5. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53d713a87d828786d3721a7bad4f8f55942690e313316d8c112fbdc0d52573f7
4
- data.tar.gz: 5bdce358ff1c577d117f97aedc866e9fdd0fa1856b4061a224cfa54c07fb774e
3
+ metadata.gz: cbbc31d7770e04a3a7142fe8b7cb300cd2b3aeac66eb4e779be6030490e6563d
4
+ data.tar.gz: c6278f0489ab81f3e0feb100334faca7042552a008b7381ef9b95043093f34b3
5
5
  SHA512:
6
- metadata.gz: '095f555efdd1842c7a1f83ad0d946ec068aa322fac35b5592349d70fe8a495fbaae45fe91fd0c4e97da76dcb7d47a7804d86afa94cc16b13711f0a01faa871f3'
7
- data.tar.gz: 55209fa3a51a9eb878e20f1f0faaf9b1f4bf8bbae85a2d97f852801d4d23c98084750500ff5cef5032042af7e35363da348f59f529096d04dc7d7a73d2d35cf1
6
+ metadata.gz: 15ebfffc8652e339fcb9a419d843ac18f9bfaa3e804d7289231d22edd944de25d23025d147dabf303f45b8787c14877b6c6019e5ed5351b10d462b2572f153d0
7
+ data.tar.gz: df16b882ca625eeccd2c11b67574eee8f905996199dee963b05e188c292e92f0a6d8361293698ab65f5bfadbe190882ed2f105172df5184da050276cba775325
data/README.md CHANGED
@@ -31,6 +31,17 @@ Prop.before_throttle do |handle, key, threshold, interval|
31
31
  end
32
32
  ```
33
33
 
34
+ ## Setting an After Evaluated Callback
35
+
36
+ You can define an optional callback that is invoked when a rate limit is checked. The callback will be invoked regardless
37
+ of the result of the evaluation.
38
+
39
+ ```ruby
40
+ Prop.after_evaluated do |handle, counter, options|
41
+ Rails.logger.info "Prop #{handle} has just been check. current value: #{counter}"
42
+ end
43
+ ````
44
+
34
45
  ## Defining thresholds
35
46
 
36
47
  Example: Limit on accepted emails per hour from a given user, by defining a threshold and interval (in seconds):
@@ -43,7 +54,7 @@ Prop.configure(:mails_per_hour, threshold: 0, interval: 1.hour, description: "Al
43
54
  ```
44
55
 
45
56
  ```ruby
46
- # Throws Prop::RateLimitExceededError if the threshold/interval has been reached
57
+ # Throws Prop::RateLimited if the threshold/interval has been reached
47
58
  Prop.throttle!(:mails_per_hour)
48
59
 
49
60
  # Prop can be used to guard a block of code
data/lib/prop/limiter.rb CHANGED
@@ -9,7 +9,7 @@ module Prop
9
9
  class Limiter
10
10
 
11
11
  class << self
12
- attr_accessor :handles, :before_throttle_callback, :cache
12
+ attr_accessor :handles, :before_throttle_callback, :cache, :after_evaluated_callback
13
13
 
14
14
  def read(&blk)
15
15
  raise "Use .cache = "
@@ -39,6 +39,10 @@ module Prop
39
39
  self.before_throttle_callback = blk
40
40
  end
41
41
 
42
+ def after_evaluated(&blk)
43
+ self.after_evaluated_callback = blk
44
+ end
45
+
42
46
  # Public: Registers a handle for rate limiting
43
47
  #
44
48
  # handle - the name of the handle you wish to use in your code, e.g. :login_attempt
@@ -150,13 +154,18 @@ module Prop
150
154
  return [false, strategy.zero_counter] if disabled?
151
155
 
152
156
  if leaky_bucket_strategy?(strategy)
153
- return Prop::LeakyBucketStrategy._throttle_leaky_bucket(handle, key, cache_key, options)
157
+ is_over_limit, bucket_info_hash = Prop::LeakyBucketStrategy._throttle_leaky_bucket(handle, key, cache_key, options)
158
+ bucket_counter = bucket_info_hash.fetch(:bucket)
159
+ after_evaluated_callback.call(handle, bucket_counter, options.merge(bucket_info_hash)) if after_evaluated_callback
160
+ return [is_over_limit, bucket_info_hash]
154
161
  end
155
162
 
156
163
  counter = options.key?(:decrement) ?
157
164
  strategy.decrement(cache_key, options.fetch(:decrement), options) :
158
165
  strategy.increment(cache_key, options.fetch(:increment, 1), options)
159
166
 
167
+ after_evaluated_callback.call(handle, counter, options) if after_evaluated_callback
168
+
160
169
  if strategy.compare_threshold?(counter, :>, options)
161
170
  before_throttle_callback &&
162
171
  before_throttle_callback.call(handle, key, options[:threshold], options[:interval])
data/lib/prop.rb CHANGED
@@ -3,12 +3,12 @@ require "prop/limiter"
3
3
  require "forwardable"
4
4
 
5
5
  module Prop
6
- VERSION = "2.8.0"
6
+ VERSION = "2.9.0"
7
7
 
8
8
  # Short hand for accessing Prop::Limiter methods
9
9
  class << self
10
10
  extend Forwardable
11
11
  def_delegators :"Prop::Limiter", :read, :write, :cache, :cache=, :configure, :configurations, :disabled, :before_throttle
12
- def_delegators :"Prop::Limiter", :throttle, :throttle!, :throttled?, :count, :query, :reset
12
+ def_delegators :"Prop::Limiter", :throttle, :throttle!, :throttled?, :count, :query, :reset, :after_evaluated
13
13
  end
14
14
  end
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.8.0
4
+ version: 2.9.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: 2023-03-23 00:00:00.000000000 Z
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: primdahl@me.com
@@ -38,14 +38,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: '2.2'
41
+ version: '2.7'
42
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  requirements: []
48
- rubygems_version: 3.0.3.1
48
+ rubygems_version: 3.5.3
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: Gem for implementing rate limits.