Class: RubyTorrent::RateMeter
- Inherits:
-
Object
- Object
- RubyTorrent::RateMeter
- Defined in:
- lib/rubytorrent/peer.rb
Overview
estimate a rate. basically copied from bram’s code.
Instance Attribute Summary collapse
-
#amt ⇒ Object
readonly
Returns the value of attribute amt.
Instance Method Summary collapse
- #add(new_amt) ⇒ Object
- #bytes_until(new_rate) ⇒ Object
-
#initialize(window = 20) ⇒ RateMeter
constructor
A new instance of RateMeter.
- #rate ⇒ Object
Constructor Details
Instance Attribute Details
#amt ⇒ Object (readonly)
Returns the value of attribute amt.
62 63 64 |
# File 'lib/rubytorrent/peer.rb', line 62 def amt @amt end |
Instance Method Details
#add(new_amt) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/rubytorrent/peer.rb', line 72 def add(new_amt) now = Time.now @m.synchronize do @amt += new_amt @rate = ((@rate * (@last - @since)) + new_amt).to_f / (now - @since) @last = now @since = [@since, now - @window].max end end |
#bytes_until(new_rate) ⇒ Object
86 87 88 |
# File 'lib/rubytorrent/peer.rb', line 86 def bytes_until(new_rate) [(new_rate.to_f * (Time.now - @since)) - (@rate * (@last - @since)), 0].max end |
#rate ⇒ Object
82 83 84 |
# File 'lib/rubytorrent/peer.rb', line 82 def rate (@rate * (@last - @since)).to_f / (Time.now - @since) end |