mafintosh / torrent-stream

The low level streaming torrent engine that peerflix uses
MIT License
1.94k stars 227 forks source link

Pulse to prevent flooding. #120

Closed dcerisano closed 7 years ago

dcerisano commented 9 years ago

Seem to have an early swarm-level solution for network flooding. Just a one-liner in the selector.

Requests to peers are prevented above a threshold swarm download rate (PULSE), and resumed below the threshold, producing a pulse in the network, rather than a flood (see fig 1).

FLOOD allows a limited flood before pulsing begins.

engine.PULSE = Number.MAX_SAFE_INTEGER;  // Defaults: do not pulse
engine.FLOOD = Number.MAX_SAFE_INTEGER;  

var select = function(wire, hotswap) {
   // Pulse
   if (swarm.downloaded > engine.FLOOD && swarm.downloadSpeed() > engine.PULSE) 
       return true;
   ...

Default is no pulsing (existing behavior). The flood limit and pulse threshold can be set dynamically by a torrent-stream consumer.

For example in a request for a video segment:

engine.FLOOD = 10  * 1024 * 1024 + engine.swarm.downloaded; //  10 MB flood
engine.PULSE = 312 * 1024;                                  // 312 KBps pulse (720p)

image

bsuh commented 9 years ago

Wow this is very nice! Thanks for this. :+1:

rstanislav commented 9 years ago

Is there any way to make this dynamic ? ie when starting download speed limited at ~ 50mbps, when user watching and not doing any seek it limit to file bitrate + 10% - so it uses only needed amount of BW, when user seek's to some point - it again gives full speed (50mbps) and then again after first few pieces downloaded it limits speed to file bitrate + 10% ?

dcerisano commented 9 years ago

Torrent-stream is for producing any torrent stream, not just video. Content specific requirements (what you described) should be implemented in a torrent-stream consumer.

Also, pulsing is dynamic, but designed to allow torrent-stream consumers to prevent flooding. It does not set hard limits on rates.