@@ -9,7 +9,7 @@ module Prop
99 class Limiter
1010
1111 class << self
12- attr_accessor :handles , :before_throttle_callback , :cache
12+ attr_accessor :handles , :before_throttle_callback , :cache , :after_evaluated_callback
1313
1414 def read ( &blk )
1515 raise "Use .cache = "
@@ -39,6 +39,10 @@ def before_throttle(&blk)
3939 self . before_throttle_callback = blk
4040 end
4141
42+ def after_evaluated ( &blk )
43+ self . after_evaluated_callback = blk
44+ end
45+
4246 # Public: Registers a handle for rate limiting
4347 #
4448 # handle - the name of the handle you wish to use in your code, e.g. :login_attempt
@@ -150,13 +154,18 @@ def _throttle(strategy, handle, key, cache_key, options)
150154 return [ false , strategy . zero_counter ] if disabled?
151155
152156 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 ]
154161 end
155162
156163 counter = options . key? ( :decrement ) ?
157164 strategy . decrement ( cache_key , options . fetch ( :decrement ) , options ) :
158165 strategy . increment ( cache_key , options . fetch ( :increment , 1 ) , options )
159166
167+ after_evaluated_callback . call ( handle , counter , options ) if after_evaluated_callback
168+
160169 if strategy . compare_threshold? ( counter , :> , options )
161170 before_throttle_callback &&
162171 before_throttle_callback . call ( handle , key , options [ :threshold ] , options [ :interval ] )
0 commit comments