mafintosh / torrent-stream

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

Pulse to prevent network flooding #122

Closed dcerisano closed 9 years ago

dcerisano commented 9 years ago

Here is an early swarm-level solution for network flooding - a one-liner in the selector:

if (swarm.downloaded > engine.flood && swarm.downloadSpeed() > engine.pulse) 
    return true;

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

A limited flood of the buffer can be set before pulsing begins.

image

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

For example in a request for a 720p video segment:

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

or

engine.setFlood(10*1024*1024);
engine.setPulse(312*1024);

or

engine.setFloodedPulse(10*1024*1024, 312*1024);

Reset defaults:

engine.flood();
mafintosh commented 9 years ago

could we make the pulse/flood properties lowercase since they aren't constants? also it would be cool if you pass them as constructor options as well

bsuh commented 9 years ago

:+1: This is awesome

mafintosh commented 9 years ago

@dcerisano :+1: i'll release this when i get to my laptop

mafintosh commented 9 years ago

Out in 0.21.0. Thanks!