lib/prop/limiter.rb in prop-1.0.0 vs lib/prop/limiter.rb in prop-1.0.1

- old
+ new

@@ -4,20 +4,24 @@ module Prop class Limiter class << self - attr_accessor :handles, :reader, :writer + attr_accessor :handles, :reader, :writer, :before_throttle_callback def read(&blk) self.reader = blk end def write(&blk) self.writer = blk end + def before_throttle(&blk) + self.before_throttle_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 } # @@ -53,9 +57,13 @@ options, cache_key = prepare(handle, key, options) counter = reader.call(cache_key).to_i unless disabled? if at_threshold?(counter, options[:threshold]) + unless before_throttle_callback.nil? + before_throttle_callback.call(handle, key, options[:threshold], options[:interval]) + end + raise Prop::RateLimited.new(options.merge(:cache_key => cache_key, :handle => handle)) else increment = options.key?(:increment) ? options[:increment].to_i : 1 counter = writer.call(cache_key, counter + increment) end