lib/prop/limiter.rb in prop-2.8.0 vs lib/prop/limiter.rb in prop-2.9.0

- old
+ new

@@ -7,11 +7,11 @@ module Prop class Limiter class << self - attr_accessor :handles, :before_throttle_callback, :cache + attr_accessor :handles, :before_throttle_callback, :cache, :after_evaluated_callback def read(&blk) raise "Use .cache = " end @@ -37,10 +37,14 @@ def before_throttle(&blk) self.before_throttle_callback = blk end + def after_evaluated(&blk) + self.after_evaluated_callback = blk + end + # Public: Registers a handle for rate limiting # # handle - the name of the handle you wish to use in your code, e.g. :login_attempt # defaults - the settings for this handle, e.g. { threshold: 5, interval: 5.minutes } # @@ -148,15 +152,20 @@ def _throttle(strategy, handle, key, cache_key, options) return [false, strategy.zero_counter] if disabled? if leaky_bucket_strategy?(strategy) - return Prop::LeakyBucketStrategy._throttle_leaky_bucket(handle, key, cache_key, options) + is_over_limit, bucket_info_hash = Prop::LeakyBucketStrategy._throttle_leaky_bucket(handle, key, cache_key, options) + bucket_counter = bucket_info_hash.fetch(:bucket) + after_evaluated_callback.call(handle, bucket_counter, options.merge(bucket_info_hash)) if after_evaluated_callback + return [is_over_limit, bucket_info_hash] end counter = options.key?(:decrement) ? strategy.decrement(cache_key, options.fetch(:decrement), options) : strategy.increment(cache_key, options.fetch(:increment, 1), options) + + after_evaluated_callback.call(handle, counter, options) if after_evaluated_callback if strategy.compare_threshold?(counter, :>, options) before_throttle_callback && before_throttle_callback.call(handle, key, options[:threshold], options[:interval])