Sha256: e5e79910c64eb222c9759daaaa527108361817781d2fe1f064406b4dbaf9696c

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module GluttonRatelimit
  
  class AveragedThrottle < ParentLimiter
    
    def reset_bucket
      @before_previous_execution = Time.now if @before_previous_execution.nil?
      @oldest_timestamp = Time.now
      @tokens = @executions
      @total_task_time = 0
    end
    
    def executed_this_period
      @executions - @tokens
    end
    
    def average_task_time
      @total_task_time.to_f / executed_this_period
    end
    
    def wait
      reset_bucket if @tokens.nil?
      
      now = Time.now
      delta_since_previous = now - @before_previous_execution
      @total_task_time += delta_since_previous
      remaining_time = @time_period - (now - @oldest_timestamp)
      
      if @tokens.zero?
        sleep(remaining_time) if remaining_time > 0
        reset_bucket
      elsif executed_this_period != 0
        throttle = (remaining_time.to_f + delta_since_previous) / (@tokens+1) - average_task_time
        sleep throttle if throttle > 0
      end
      
      @tokens -= 1
      @before_previous_execution = Time.now
    end
    
  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glutton_ratelimit-0.1.0 lib/glutton_ratelimit/averaged_throttle.rb