Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async function run(argv) {
try {
await start(options);
if (options.localhost) {
console.log(`Started throttler on localhost RTT:${options.rtt}ms `);
console.log(`Started throttler on localhost RTT:${options.rtt}ms PL:${options.packetLoss}%`);
} else {
let message = 'Started throttler:';
if (typeof options.down !== 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function start(options = {}) {
switch (platform()) {
case 'darwin': {
if (options.localhost) {
return startPfctlLocalhost(options.rtt);
return startPfctlLocalhost(options.rtt, options.packetLoss);
}
return startPfctl(
options.up,
Expand All @@ -44,7 +44,7 @@ export async function start(options = {}) {

case 'linux': {
return options.localhost
? startTcLocalhost(options.rtt)
? startTcLocalhost(options.rtt, options.packetLoss)
: startTc(options.up, options.down, options.rtt, options.packetLoss);
}

Expand Down
13 changes: 8 additions & 5 deletions lib/localHostPfctl.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import sudo from './sudo.js';
import shell from './shell.js';

export async function start(rtt) {
export async function start(rtt, packetLoss=0) {
const halfWayRTT = rtt / 2;

await stop();

await sudo('dnctl', '-q', 'flush');
await sudo('dnctl', '-q', 'pipe', 'flush');

await sudo(
const parameters = [
'dnctl',
'pipe',
1,
'config',
'delay',
`${halfWayRTT}ms`,
'noerror'
);
`${halfWayRTT}ms`
];
if (packetLoss > 0) {
parameters.push('plr', packetLoss / 100, 'noerror');
}
await sudo.apply(this, parameters);

await shell(
'echo "dummynet out from any to 127.0.0.1 pipe 1" | sudo pfctl -f -'
Expand Down
12 changes: 9 additions & 3 deletions lib/localHostTc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sudo from './sudo.js';

export async function start(delay) {
export async function start(delay, packetLoss=0) {
const halfWayDelay = delay / 2;

try {
Expand All @@ -9,7 +9,7 @@ export async function start(delay) {
// ignore
}

await sudo(
const parameters = [
'tc',
'qdisc',
'add',
Expand All @@ -21,7 +21,13 @@ export async function start(delay) {
'netem',
'delay',
`${halfWayDelay}ms`
);
];

if (packetLoss) {
parameters.push('loss', `${packetLoss}%`);
}

await sudo.apply(this, parameters);
}
export async function stop() {
await sudo('tc', 'qdisc', 'del', 'dev', 'lo', 'root');
Expand Down