lib/prop/limiter.rb in prop-2.1.0 vs lib/prop/limiter.rb in prop-2.1.1

- old
+ new

@@ -68,13 +68,14 @@ # key - a custom request specific key, e.g. [ account.id, "download", request.remote_ip ] # options - request specific overrides to the defaults configured for this handle # (optional) a block of code that this throttle is guarding # # Returns true if the threshold for this handle has been reached, else returns false - def throttle(handle, key = nil, options = {}, &block) + def throttle(handle, key = nil, options = {}) options, cache_key = prepare(handle, key, options) - _throttle(handle, key, cache_key, options, &block).first + throttled = _throttle(handle, key, cache_key, options).first + block_given? && !throttled ? yield : throttled end # Public: Records a single action for the given handle/key combination. # # handle - the registered handle associated with the action @@ -84,11 +85,11 @@ # # Raises Prop::RateLimited if the threshold for this handle has been reached # Returns the value of the block if given a such, otherwise the current count of the throttle def throttle!(handle, key = nil, options = {}, &block) options, cache_key = prepare(handle, key, options) - throttled, counter = _throttle(handle, key, cache_key, options, &block) + throttled, counter = _throttle(handle, key, cache_key, options) if throttled raise Prop::RateLimited.new(options.merge( cache_key: cache_key, handle: handle, @@ -156,10 +157,9 @@ true end [result, counter] else - yield if block_given? [false, counter] end end def disabled?