This commit is contained in:
2026-03-17 15:36:23 +00:00
parent 236d6d16ee
commit 8b5df9a0b7
4 changed files with 100 additions and 55 deletions

View File

@@ -142,7 +142,7 @@ class ThrottleTransform extends stream.Transform {
this.bucket = 0;
const delayMs = Math.min((deficit / this.bytesPerSec) * 1000, 1000);
setTimeout(() => {
if (this.destroyed_) return;
if (this.destroyed_) { callback(); return; }
this.lastRefill = Date.now();
this.bucket = 0;
callback(null, chunk);
@@ -179,7 +179,16 @@ async function startThrottleProxy(
clientSock.pipe(throttleUp).pipe(upstream);
upstream.pipe(throttleDown).pipe(clientSock);
const cleanup = () => {
let cleaned = false;
const cleanup = (source: string, err?: Error) => {
if (cleaned) return;
cleaned = true;
if (err) {
console.error(`[ThrottleProxy] cleanup triggered by ${source}: ${err.message}`);
} else {
console.error(`[ThrottleProxy] cleanup triggered by ${source} (no error)`);
}
console.error(`[ThrottleProxy] stack:`, new Error().stack);
throttleUp.destroy();
throttleDown.destroy();
clientSock.destroy();
@@ -187,12 +196,12 @@ async function startThrottleProxy(
connections.delete(clientSock);
connections.delete(upstream);
};
clientSock.on('error', cleanup);
upstream.on('error', cleanup);
throttleUp.on('error', cleanup);
throttleDown.on('error', cleanup);
clientSock.on('close', cleanup);
upstream.on('close', cleanup);
clientSock.on('error', (e) => cleanup('clientSock.error', e));
upstream.on('error', (e) => cleanup('upstream.error', e));
throttleUp.on('error', (e) => cleanup('throttleUp.error', e));
throttleDown.on('error', (e) => cleanup('throttleDown.error', e));
clientSock.on('close', () => cleanup('clientSock.close'));
upstream.on('close', () => cleanup('upstream.close'));
});
await new Promise<void>((resolve) => server.listen(listenPort, '127.0.0.1', resolve));
@@ -222,13 +231,13 @@ let edgePort: number;
// Tests
// ---------------------------------------------------------------------------
tap.test('setup: start throttled tunnel (20 Mbit/s)', async () => {
tap.test('setup: start throttled tunnel (100 Mbit/s)', async () => {
[hubPort, proxyPort, edgePort] = await findFreePorts(3);
echoServer = await startEchoServer(edgePort, '127.0.0.2');
// Throttle proxy: edge → proxy → hub at 20 Mbit/s (2.5 MB/s)
throttle = await startThrottleProxy(proxyPort, '127.0.0.1', hubPort, 2.5 * 1024 * 1024);
// Throttle proxy: edge → proxy → hub at 100 Mbit/s (12.5 MB/s)
throttle = await startThrottleProxy(proxyPort, '127.0.0.1', hubPort, 12.5 * 1024 * 1024);
hub = new RemoteIngressHub();
edge = new RemoteIngressEdge();
@@ -246,7 +255,7 @@ tap.test('setup: start throttled tunnel (20 Mbit/s)', async () => {
});
});
// Edge connects to proxy, not hub directly
// Edge connects through throttle proxy
await edge.start({
hubHost: '127.0.0.1',
hubPort: proxyPort,
@@ -262,12 +271,12 @@ tap.test('setup: start throttled tunnel (20 Mbit/s)', async () => {
expect(status.connected).toBeTrue();
});
tap.test('throttled: 10 streams x 50MB each through 10MB/s tunnel', async () => {
const streamCount = 10;
const payloadSize = 50 * 1024 * 1024; // 50MB per stream = 500MB total round-trip
tap.test('throttled: 5 streams x 20MB each through 100Mbit tunnel', async () => {
const streamCount = 5;
const payloadSize = 20 * 1024 * 1024; // 20MB per stream = 100MB total round-trip
const promises = Array.from({ length: streamCount }, () => {
const data = crypto.randomBytes(payloadSize);
const payloads = Array.from({ length: streamCount }, () => crypto.randomBytes(payloadSize));
const promises = payloads.map((data) => {
const hash = sha256(data);
return sendAndReceive(edgePort, data, 300000).then((received) => ({
sent: hash,
@@ -284,23 +293,23 @@ tap.test('throttled: 10 streams x 50MB each through 10MB/s tunnel', async () =>
expect(status.connected).toBeTrue();
});
tap.test('throttled: slow consumer with 50MB does not kill other streams', async () => {
// Open a connection that creates massive download-direction backpressure:
// send 50MB but DON'T read the response — client TCP receive buffer fills
tap.test('throttled: slow consumer with 20MB does not kill other streams', async () => {
// Open a connection that creates download-direction backpressure:
// send 20MB but DON'T read the response — client TCP receive buffer fills
const slowSock = net.createConnection({ host: '127.0.0.1', port: edgePort });
await new Promise<void>((resolve) => slowSock.on('connect', resolve));
const slowData = crypto.randomBytes(50 * 1024 * 1024);
const slowData = crypto.randomBytes(20 * 1024 * 1024);
slowSock.write(slowData);
slowSock.end();
// Don't read — backpressure builds on the download path
// Wait for backpressure to develop
await new Promise((r) => setTimeout(r, 3000));
await new Promise((r) => setTimeout(r, 2000));
// Meanwhile, 10 normal echo streams with 50MB each must complete
const payload = crypto.randomBytes(50 * 1024 * 1024);
// Meanwhile, 5 normal echo streams with 20MB each must complete
const payload = crypto.randomBytes(20 * 1024 * 1024);
const hash = sha256(payload);
const promises = Array.from({ length: 10 }, () =>
const promises = Array.from({ length: 5 }, () =>
sendAndReceive(edgePort, payload, 300000).then((r) => ({
hash: sha256(r),
sizeOk: r.length === payload.length,
@@ -317,11 +326,11 @@ tap.test('throttled: slow consumer with 50MB does not kill other streams', async
slowSock.destroy();
});
tap.test('throttled: rapid churn — 5 x 50MB long + 200 x 1MB short streams', async () => {
// 5 long streams (50MB each) running alongside 200 short streams (1MB each)
const longPayload = crypto.randomBytes(50 * 1024 * 1024);
tap.test('throttled: rapid churn — 3 x 20MB long + 50 x 1MB short streams', async () => {
// 3 long streams (20MB each) running alongside 50 short streams (1MB each)
const longPayload = crypto.randomBytes(20 * 1024 * 1024);
const longHash = sha256(longPayload);
const longPromises = Array.from({ length: 5 }, () =>
const longPromises = Array.from({ length: 3 }, () =>
sendAndReceive(edgePort, longPayload, 300000).then((r) => ({
hash: sha256(r),
sizeOk: r.length === longPayload.length,
@@ -330,7 +339,7 @@ tap.test('throttled: rapid churn — 5 x 50MB long + 200 x 1MB short streams', a
const shortPayload = crypto.randomBytes(1024 * 1024);
const shortHash = sha256(shortPayload);
const shortPromises = Array.from({ length: 200 }, () =>
const shortPromises = Array.from({ length: 50 }, () =>
sendAndReceive(edgePort, shortPayload, 300000).then((r) => ({
hash: sha256(r),
sizeOk: r.length === shortPayload.length,
@@ -351,10 +360,10 @@ tap.test('throttled: rapid churn — 5 x 50MB long + 200 x 1MB short streams', a
expect(status.connected).toBeTrue();
});
tap.test('throttled: 5 burst waves of 20 streams x 50MB each', async () => {
for (let wave = 0; wave < 5; wave++) {
const streamCount = 20;
const payloadSize = 50 * 1024 * 1024; // 50MB per stream = 1GB per wave
tap.test('throttled: 3 burst waves of 5 streams x 20MB each', async () => {
for (let wave = 0; wave < 3; wave++) {
const streamCount = 5;
const payloadSize = 20 * 1024 * 1024; // 20MB per stream = 100MB per wave
const promises = Array.from({ length: streamCount }, () => {
const data = crypto.randomBytes(payloadSize);