Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

fix: try alternate IPv6 addresses after stalled route

Anmol Kapil1eb9883b932b
diff --git a/e2e/network.spec.ts b/e2e/network.spec.ts
index 245fbdc..0b28210 100644
--- a/e2e/network.spec.ts
+++ b/e2e/network.spec.ts
@@ -95,3 +95,37 @@ test('falls back to IPv6 when an IPv4 connection fails before headers', async ()
await new Promise<void>((resolve) => server.close(() => resolve()))
}
})
+
+test('a silent route leaves time to try another address', async () => {
+ const silent = createServer(() => {})
+ await new Promise<void>((resolve) => silent.listen(0, '127.0.0.1', resolve))
+ const port = (silent.address() as AddressInfo).port
+ const working = createServer((_req, res) => res.end('ok'))
+ await new Promise<void>((resolve) => working.listen(port, '::1', resolve))
+ try {
+ const { res } = await requestOnInterface({
+ target: new URL(`http://dual.example.test:${port}/`),
+ iface: {
+ ...iface,
+ addresses: [
+ { address: '127.0.0.1', family: 4 },
+ { address: '::1', family: 6 }
+ ]
+ },
+ headers: {},
+ timeoutMs: 2000,
+ resolveHost: async () => [
+ { address: '127.0.0.1', family: 4 },
+ { address: '::1', family: 6 }
+ ]
+ })
+ let body = ''
+ for await (const chunk of res) body += chunk
+ expect(body).toBe('ok')
+ } finally {
+ await Promise.all([
+ new Promise<void>((resolve) => silent.close(() => resolve())),
+ new Promise<void>((resolve) => working.close(() => resolve()))
+ ])
+ }
+})
diff --git a/src/main/network/routes.ts b/src/main/network/routes.ts
index 960f243..bf535e7 100644
--- a/src/main/network/routes.ts
+++ b/src/main/network/routes.ts
@@ -84,10 +84,13 @@ export async function requestOnInterface({
const deadline = Date.now() + timeoutMs
let lastError: Error = new Error('Connection failed')
- for (const route of routes) {
+ for (const [index, route] of routes.entries()) {
if (signal?.aborted) throw new DOMException('Aborted', 'AbortError')
const remaining = deadline - Date.now()
if (remaining <= 0) break
+ // A stale local address can hang until timeout. Leave time for the other addresses
+ // on this interface instead of spending the entire budget on the first one.
+ const routeTimeout = Math.max(1, Math.floor(remaining / (routes.length - index)))
try {
return await new Promise((resolve, reject) => {
@@ -112,7 +115,7 @@ export async function requestOnInterface({
}
const timer = setTimeout(
() => req.destroy(new Error('Connection stalled: no response from server')),
- remaining
+ routeTimeout
)
req.once('error', (error) => {
clearTimeout(timer)