- def byterate
- return "#{@bytes_per}/#{@seconds_per} (limit hit #{@hit_limit} times)"
- end
-
- def byterate=(newrate)
- @qmutex.synchronize do
- setbyterate(newrate)
- end
- end
-
- def run_throttle(more=0)
- now = Time.new
- # Each time we reach the limit, we reduce the bitrate. We reset the bitrate only if the throttle
- # manages to reset twice. This way we have better flood control, although the really perfect way
- # would be to calculate our penalty the way it's done serverside.
- if @throttle_bytes > 0
- if @throttle_bytes >= @bytes_per
- @hit_limit += 1
- @hit_limit = 3 if @hit_limit > 3
- end
- delta = ((now - @last_throttle)*(0.5**@hit_limit.ceil)*@bytes_per/@seconds_per).floor
- if delta > 0
- @throttle_bytes -= delta
- @throttle_bytes = 0 if @throttle_bytes < 0
- @last_throttle = now
- end
- else
- @hit_limit -= 0.5 if @hit_limit > 0
- end
- @throttle_bytes += more
- end
-