Class: BwLimiter
- Inherits:
-
Object
- Object
- BwLimiter
- Defined in:
- lib/tidy/bwlimit.rb
Overview
Limits the amount of data sent/recv. It acts as a authorisation-to-sent/recv-data dispenser Whoever wants to send/recv data must call the #dispense method to know how many it should send/recv at that time
Direct Known Subclasses
Instance Method Summary collapse
- #dispense ⇒ Object
-
#initialize(rate, ticks) ⇒ BwLimiter
constructor
A new instance of BwLimiter.
Constructor Details
#initialize(rate, ticks) ⇒ BwLimiter
Returns a new instance of BwLimiter.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tidy/bwlimit.rb', line 14 def initialize(rate, ticks) @bytes_per_tick = 1.0*rate/ticks @hz = 1.0/ticks if @bytes_per_tick < 1 raise "rate too low or ticks too high" end @bytes_per_tick = @bytes_per_tick.round if @bytes_per_tick * ticks != rate puts "Adjusting rate to #{@bytes_per_tick * ticks}" end @last_tick = Time.now end |
Instance Method Details
#dispense ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tidy/bwlimit.rb', line 26 def dispense curr = Time.now delta = curr - @last_tick if delta < @hz sleep(@hz - delta) end #puts "Releasing #{@bytes_per_tick}. Speed = #{@bytes_per_tick / (Time.now - curr)}" @last_tick = curr @bytes_per_tick end |