fix(protocol): improve upload recovery and time limits
Apply configurable idle and total transfer deadlines, cover supplied upload candidates, and retain recent endpoint outcomes per account and upload type. Preserve failed action responses and resource cleanup. Refs #637
This commit is contained in:
10 files changed
+760
-41
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { UploadNodeHistory } from '../../src/highway/upload-nodes';
|
||||
|
||||
const a = { host: '192.0.2.1', port: 14000 };
|
||||
const b = { host: '192.0.2.1', port: 80 };
|
||||
const c = { host: '192.0.2.2', port: 80 };
|
||||
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
describe('upload node observations', () => {
|
||||
it('orders current candidates by confirmed success, unknown, then recent failure', () => {
|
||||
const history = new UploadNodeHistory();
|
||||
history.record(1004, a, false);
|
||||
history.record(1004, b, true);
|
||||
const original = [a, c, b, a];
|
||||
expect(history.order(1004, original)).toEqual([b, c, a]);
|
||||
expect(original).toEqual([a, c, b, a]);
|
||||
expect(history.order(1003, [a, c, b])).toEqual([a, c, b]);
|
||||
expect(history.order(1004, [c])).toEqual([c]);
|
||||
});
|
||||
|
||||
it('expires failures and successes and still allows every failed candidate', () => {
|
||||
vi.useFakeTimers();
|
||||
const history = new UploadNodeHistory();
|
||||
history.record(1004, a, false);
|
||||
history.record(1004, b, false);
|
||||
expect(history.order(1004, [a, b])).toEqual([a, b]);
|
||||
expect(history.order(1004, [a, c])).toEqual([c, a]);
|
||||
vi.advanceTimersByTime(30_000);
|
||||
expect(history.order(1004, [a, c])).toEqual([a, c]);
|
||||
history.record(1004, b, true);
|
||||
expect(history.order(1004, [a, b])).toEqual([b, a]);
|
||||
vi.advanceTimersByTime(5 * 60_000);
|
||||
expect(history.order(1004, [a, b])).toEqual([a, b]);
|
||||
});
|
||||
|
||||
it('lets a new failure replace success and a subsequent success recover', () => {
|
||||
const history = new UploadNodeHistory();
|
||||
history.record(1004, b, true);
|
||||
history.record(1004, b, false);
|
||||
expect(history.order(1004, [b, a])).toEqual([a, b]);
|
||||
history.record(1004, b, true);
|
||||
expect(history.order(1004, [a, b])).toEqual([b, a]);
|
||||
});
|
||||
|
||||
it('evicts old observations when many distinct endpoints arrive', () => {
|
||||
const history = new UploadNodeHistory();
|
||||
history.record(1004, a, false);
|
||||
|
||||
for (let port = 10000; port < 10200; port++) {
|
||||
history.record(1004, { host: '192.0.2.3', port }, false);
|
||||
}
|
||||
|
||||
expect(history.order(1004, [a, c])).toEqual([a, c]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { highwayUploadTimeouts } from '../../src/highway/upload-settings';
|
||||
|
||||
describe('highway upload timeouts', () => {
|
||||
it('retains the idle default and supplies a finite whole-file deadline', () => {
|
||||
expect(highwayUploadTimeouts({})).toEqual({ idleMs: 30_000, totalMs: 1_800_000 });
|
||||
});
|
||||
|
||||
it('reads independent millisecond budgets without clamping', () => {
|
||||
expect(
|
||||
highwayUploadTimeouts({
|
||||
SNOWLUMA_HIGHWAY_UPLOAD_IDLE_TIMEOUT_MS: ' 5000 ',
|
||||
SNOWLUMA_HIGHWAY_UPLOAD_TIMEOUT_MS: '120000',
|
||||
}),
|
||||
).toEqual({ idleMs: 5000, totalMs: 120000 });
|
||||
expect(highwayUploadTimeouts({ SNOWLUMA_HIGHWAY_UPLOAD_TIMEOUT_MS: '1' }).totalMs).toBe(1);
|
||||
expect(
|
||||
highwayUploadTimeouts({ SNOWLUMA_HIGHWAY_UPLOAD_TIMEOUT_MS: '2147483647' }).totalMs,
|
||||
).toBe(2147483647);
|
||||
});
|
||||
|
||||
it.each(['', ' ', '0', '-1', '1.5', 'NaN', 'Infinity', '1e3', '200ms', '2147483648'])(
|
||||
'rejects invalid configuration %j',
|
||||
(value) => {
|
||||
for (const name of [
|
||||
'SNOWLUMA_HIGHWAY_UPLOAD_IDLE_TIMEOUT_MS',
|
||||
'SNOWLUMA_HIGHWAY_UPLOAD_TIMEOUT_MS',
|
||||
]) {
|
||||
expect(() => highwayUploadTimeouts({ [name]: value })).toThrow(name);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
import net from 'node:net';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { BridgeContext } from '../../src/bridge-context';
|
||||
import {
|
||||
BufferChunkSource,
|
||||
uploadHighwayHttp,
|
||||
type HighwayServer,
|
||||
} from '../../src/highway/highway-client';
|
||||
import { packHighwayFrame } from '../../src/highway/utils';
|
||||
|
||||
const servers: net.Server[] = [];
|
||||
const sockets = new Set<net.Socket>();
|
||||
|
||||
afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
|
||||
for (const socket of sockets) {
|
||||
socket.destroy();
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
servers.splice(0).map(
|
||||
(server) =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
server.close((error) => (error ? reject(error) : resolve()));
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
async function listen(
|
||||
reply: boolean,
|
||||
): Promise<{ address: HighwayServer; connections: () => number }> {
|
||||
let connections = 0;
|
||||
const server = net.createServer((socket) => {
|
||||
connections++;
|
||||
sockets.add(socket);
|
||||
socket.once('close', () => sockets.delete(socket));
|
||||
let received = Buffer.alloc(0);
|
||||
socket.on('data', (bytes: Buffer) => {
|
||||
received = Buffer.concat([received, bytes]);
|
||||
const end = received.indexOf('\r\n\r\n');
|
||||
|
||||
if (!reply || end < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const header = received.subarray(0, end).toString('ascii');
|
||||
const length = Number(header.match(/Content-Length: (\d+)/i)?.[1]);
|
||||
|
||||
if (received.length < end + 4 + length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const frame = packHighwayFrame(new Uint8Array(), new Uint8Array());
|
||||
socket.write(
|
||||
Buffer.concat([
|
||||
Buffer.from(`HTTP/1.1 200 OK\r\nContent-Length: ${frame.byteLength}\r\n\r\n`),
|
||||
frame,
|
||||
]),
|
||||
);
|
||||
received = received.subarray(end + 4 + length);
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once('error', reject);
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
server.off('error', reject);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
servers.push(server);
|
||||
const address = server.address();
|
||||
|
||||
if (!address || typeof address === 'string') {
|
||||
throw new Error('missing loopback address');
|
||||
}
|
||||
|
||||
return {
|
||||
address: { host: address.address, port: address.port },
|
||||
connections: () => connections,
|
||||
};
|
||||
}
|
||||
|
||||
function bridge(uin: string): BridgeContext {
|
||||
return { identity: { uin } } as unknown as BridgeContext;
|
||||
}
|
||||
|
||||
function send(
|
||||
owner: BridgeContext,
|
||||
candidates: HighwayServer[],
|
||||
source = new BufferChunkSource(new Uint8Array([1, 2, 3])),
|
||||
) {
|
||||
return uploadHighwayHttp(
|
||||
owner,
|
||||
{ sigSession: new Uint8Array([1]), sessionKey: new Uint8Array(), servers: candidates },
|
||||
1004,
|
||||
source,
|
||||
new Uint8Array(16),
|
||||
new Uint8Array(),
|
||||
);
|
||||
}
|
||||
|
||||
describe('highway upload over real loopback sockets', () => {
|
||||
it('switches idle ports and remembers only confirmed success for that account', async () => {
|
||||
vi.stubEnv('SNOWLUMA_HIGHWAY_UPLOAD_IDLE_TIMEOUT_MS', '100');
|
||||
vi.stubEnv('SNOWLUMA_HIGHWAY_UPLOAD_TIMEOUT_MS', '5000');
|
||||
const idle = await listen(false);
|
||||
const healthy = await listen(true);
|
||||
const owner = bridge('10001');
|
||||
const candidates = [idle.address, healthy.address];
|
||||
await send(owner, candidates);
|
||||
await send(owner, candidates);
|
||||
expect(idle.connections()).toBe(1);
|
||||
expect(healthy.connections()).toBe(2);
|
||||
await send(bridge('10002'), candidates);
|
||||
expect(idle.connections()).toBe(2);
|
||||
expect(healthy.connections()).toBe(3);
|
||||
});
|
||||
|
||||
it('rejects an unresponsive peer at the total deadline and closes its source once', async () => {
|
||||
vi.stubEnv('SNOWLUMA_HIGHWAY_UPLOAD_IDLE_TIMEOUT_MS', '10000');
|
||||
vi.stubEnv('SNOWLUMA_HIGHWAY_UPLOAD_TIMEOUT_MS', '100');
|
||||
const idle = await listen(false);
|
||||
const source = new BufferChunkSource(new Uint8Array([1, 2, 3]));
|
||||
const close = vi.spyOn(source, 'close');
|
||||
await expect(send(bridge('10003'), [idle.address], source)).rejects.toThrow(
|
||||
'timed out after 100ms',
|
||||
);
|
||||
expect(close).toHaveBeenCalledTimes(1);
|
||||
expect(idle.connections()).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user