Class: RubyTorrent::RateMeter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytorrent/peer.rb

Overview

estimate a rate. basically copied from bram’s code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window = 20) ⇒ RateMeter

Returns a new instance of RateMeter.



64
65
66
67
68
69
70
# File 'lib/rubytorrent/peer.rb', line 64

def initialize(window=20)
  @window = window.to_f
  @amt = 0
  @rate = 0
  @last = @since = Time.now - 1
  @m = Mutex.new
end

Instance Attribute Details

#amtObject (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

#rateObject



82
83
84
# File 'lib/rubytorrent/peer.rb', line 82

def rate
  (@rate * (@last - @since)).to_f / (Time.now - @since)
end