stream-throttle
A simple Node.js stream throttler.
Installation
Using yarn:
$ yarn add @kldzj/stream-throttle
Using npm:
$ npm i -S @kldzj/stream-throttle
Usage
Options
rate
: The rate in bytes per second at which to emit data.burst
: The burst rate in bytes per second. Must be less than or equal to rate
. Defaults to rate
.chunkSize
: The size of chunks to split the received data into. Must be less than or equal to rate
. Defaults to rate / 10
.
Throttle
Used to throttle the output rate of a single stream.
import { Throttle } from '@kldzj/stream-throttle';
const throttle = new Throttle({ rate: 10 });
process.stdin.pipe(throttle).pipe(process.stdout);
ThrottleGroup
Used to throttle the output rate across multiple streams.
import { createConnection } from 'net';
import { ThrottleGroup } from '@kldzj/stream-throttle';
const group = new ThrottleGroup({ rate: 1024 * 1024 });
const addr = { host: 'www.google.com', port: 80 };
const throttled1 = createConnection(addr).pipe(group.createThrottle());
const throttled2 = createConnection(addr).pipe(group.createThrottle());
Changing the rate on-the-fly
const group = new ThrottleGroup({ rate: 1024 * 1024 });
group.setRate(4 * 1024 * 1024);
group.setChunkSize(group.rate / 6);
group.setBurst(group.rate * 1.5);
Special thanks