Sha256: 3122b562d4a698cacdc80a2f4bdc32dcc7e81616789ce7e4e9a0fb0ae1b863da

Contents?: true

Size: 545 Bytes

Versions: 1

Compression:

Stored size: 545 Bytes

Contents

module GluttonRatelimit
  
  class BurstyTokenBucket < ParentLimiter
    def initialize executions, time_period
      super(executions, time_period)
      @tokens = nil
    end
    
    def reset_bucket
      @oldest_timestamp = Time.now
      @tokens = @executions
    end
    
    def wait
      reset_bucket if @tokens.nil?
      
      if @tokens.zero?
        delta = Time.now - @oldest_timestamp
        sleep(@time_period - delta) if delta < @time_period
        reset_bucket
      end
      
      @tokens -= 1
    end
    
  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glutton_ratelimit-1.0.0 lib/glutton_ratelimit/bursty_token_bucket.rb