rg3/youtube-dl — pull request #3407 by SyxbEaEQ2 (2014-07-31 02:21:28+00:00)
Title: [downloader/(common/http)] Changes calculation of the rate-limit. (Fix #...

Precondition: "-r, --rate-limit" is set to a small value.

What was the problem:
In /downloader/http.py (main download loop):
The calculation of the new block_size (resize-buffer) took place before the slow_down() method was called and the time measurement only observed the actual download and not the time spent in slow_down(). So the best_block_size() method thought, that the block size was far too small and doubled the block size in every iteration of the main loop.

Effect on other functions:
To compensate the exponentially growing block size, the slow_down() method doubled the sleep time in every iteration.

What was the result:
- Bursty download (while not downloading most of the time)
- Download timeouts / aborts
- Inresponsive interface
- Wrong calculation of current speed

Fix:
- Changed execution order of most of the functions in the main loop. (especially slow_down() and best_block_size())
- Measuring time over the whole loop body

Additional changes:
- Synchronized all methods (slow_down(), best_block_size(), calc_speed(), calc_percent()) in the main loop by using the same time measurement variables, to get a more precise result.
- Changed signature of slow_down() to be able to pass a "now" variable. (used for sync) -^
- Simplyfied the calculation of the sleep time in slow_down()
- Added more comments
- Fixed some pep8 warnings

After all, this schould resolve the following issues:
(fix #2297, fix #2140, fix #595, fix #2370)