Class: Cifrado::RateLimit
- Inherits:
-
Object
- Object
- Cifrado::RateLimit
- Defined in:
- lib/cifrado/rate_limit.rb
Instance Method Summary collapse
-
#initialize(bwlimit) ⇒ RateLimit
constructor
A new instance of RateLimit.
- #limit(read) ⇒ Object
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 |