ejfinneran / ratelimit

A Redis-backed rate limiter written in Ruby
MIT License
260 stars 57 forks source link

Interface improvement #32

Open rodrigomonteirof opened 6 years ago

rodrigomonteirof commented 6 years ago

I've been wondering... Why not allow receive some configurations on initialize method?

Today:

def with_ratelimit(&block)                                                                                               
  ratelimit = Ratelimit.new("whatsapp", { bucket_interval: BUCKET_INTERVAL })                                            

  ratelimit.exec_within_threshold('send_message',  threshold: THRESHOLD, interval: INTERVAL) do                          
    ratelimit.add('send_message')                                                                                        
    yield                                                                                                                
  end                                                                                                                    
end 

Desire:

def with_ratelimit(&block)
  opts = { bucket_interval: BUCKET_INTERVAL, threshold: THRESHOLD, interval: INTERVAL}

  Ratelimit.new("whatsapp::send_message", opts).exec_within_threshold do
   # doesnt need to use add method anymore                                                                             
    yield                                                                                                                
  end                                                                                                                    
end

I can implement this modification if you agree, what do you think?

ejfinneran commented 6 years ago

Sorry for the delay. This looks good to me. My only concern is changing the bucket interval between calls to the same key might yield strange results. We'd just need to test for that.