Class: Cifrado::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/cifrado/rate_limit.rb

Instance Method Summary collapse

Constructor Details

#initialize(bwlimit) ⇒ RateLimit

Returns a new instance of RateLimit.



4
5
6
7
8
9
# File 'lib/cifrado/rate_limit.rb', line 4

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

Instance Method Details

#limit(read) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cifrado/rate_limit.rb', line 11

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