Sha256: 4ec4beb76ca235ff9197c1164d112d7aa85c13645bf242ba3ee5c2b1e90fe71b

Contents?: true

Size: 452 Bytes

Versions: 6

Compression:

Stored size: 452 Bytes

Contents

module Cifrado
  class RateLimit

    def initialize(bwlimit)
      @time = Time.now.to_f 
      @read = 0 
      @sleep_time = 0.01 
      @bwlimit = bwlimit
    end

    def limit(read)
      bps = @read/(Time.now.to_f - @time)
      if bps > @bwlimit
        Log.debug 'limiting rate'
        sleep @sleep_time
        @sleep_time += 0.01
      else
        @sleep_time -= 0.01 if @sleep_time >= 0.02
      end
      @read += read
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cifrado-0.2.1 lib/cifrado/rate_limit.rb
cifrado-0.2.0 lib/cifrado/rate_limit.rb
cifrado-0.1.3 lib/cifrado/rate_limit.rb
cifrado-0.1.2 lib/cifrado/rate_limit.rb
cifrado-0.1.1 lib/cifrado/rate_limit.rb
cifrado-0.1 lib/cifrado/rate_limit.rb