Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
fix: bound IPv6 route setup and simplify coverage
Anmol Kapil22de4b16923f
diff --git a/e2e/network.spec.ts b/e2e/network.spec.tsindex 0b28210..e56bb50 100644--- a/e2e/network.spec.ts+++ b/e2e/network.spec.ts@@ -2,7 +2,7 @@ import { expect, test } from '@playwright/test'import { createServer } from 'node:http'import type { AddressInfo } from 'node:net'import type { NetworkInterfaceInfo } from '../src/shared/types'-import { requestOnInterface, resolveTarget, routesFor } from '../src/main/network/routes'+import { requestOnInterface } from '../src/main/network/routes'const iface: NetworkInterfaceInfo = {id: 'wifi',@@ -15,29 +15,7 @@ const iface: NetworkInterfaceInfo = {]}-test('AAAA-only host uses the matching IPv6 address', async () => {- const remote = await resolveTarget(new URL('https://ipv6.example.test/file'), async (host) => {- expect(host).toBe('ipv6.example.test')- return [{ address: '2001:db8::10', family: 6 }]- })- expect(routesFor(iface, remote)).toEqual([- { device: 'wifi', localAddress: '2001:db8::2', remoteAddress: '2001:db8::10', family: 6 }- ])-})--test('IPv4 and bracketed IPv6 literals bypass DNS', async () => {- const failLookup = async (): Promise<never> => {- throw new Error('DNS should not run for a literal')- }- expect(await resolveTarget(new URL('http://127.0.0.1/'), failLookup)).toEqual([- { address: '127.0.0.1', family: 4 }- ])- expect(await resolveTarget(new URL('http://[::1]/'), failLookup)).toEqual([- { address: '::1', family: 6 }- ])-})--test('AAAA-only hostname connects over IPv6 and keeps its HTTP Host header', async () => {+test('AAAA-only hostname connects over IPv6 and keeps its HTTP Host header @smoke', async () => {let seenHost = ''const server = createServer((req, res) => {seenHost = req.headers.host ?? ''@@ -67,8 +45,10 @@ test('AAAA-only hostname connects over IPv6 and keeps its HTTP Host header', asy}})-test('falls back to IPv6 when an IPv4 connection fails before headers', async () => {- const server = createServer((_req, res) => res.end('ipv6'))+test('a working route gets time to answer after an earlier route fails @smoke', async () => {+ const server = createServer((_req, res) => {+ setTimeout(() => res.end('ipv6'), 1800)+ })await new Promise<void>((resolve) => server.listen(0, '::1', resolve))try {const port = (server.address() as AddressInfo).port@@ -82,10 +62,11 @@ test('falls back to IPv6 when an IPv4 connection fails before headers', async ()]},headers: {},- timeoutMs: 2000,+ timeoutMs: 3000,resolveHost: async () => [{ address: '127.0.0.1', family: 4 },- { address: '::1', family: 6 }+ { address: '::1', family: 6 },+ { address: '127.0.0.2', family: 4 }]})let body = ''@@ -96,36 +77,14 @@ test('falls back to IPv6 when an IPv4 connection fails before headers', async ()}})-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 }- ]- },+test('DNS resolution obeys the request deadline @smoke', async () => {+ await expect(+ requestOnInterface({+ target: new URL('http://unresolved.example.test/file'),+ iface,headers: {},- timeoutMs: 2000,- resolveHost: async () => [- { address: '127.0.0.1', family: 4 },- { address: '::1', family: 6 }- ]+ timeoutMs: 100,+ resolveHost: () => new Promise(() => {})})- 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()))- ])- }+ ).rejects.toThrow('Could not resolve the download host in time')})diff --git a/e2e/recovery.spec.ts b/e2e/recovery.spec.tsindex d7b52b8..c5ff8ae 100644--- a/e2e/recovery.spec.ts+++ b/e2e/recovery.spec.ts@@ -11,22 +11,6 @@ import { seededBytes, sha256 } from './origin'const SIZE = 32 * BLOCKtest.describe('restart @smoke', () => {- test('IPv6 download survives quit and relaunch', async ({ plexo, serve }) => {- await plexo.relaunch({ PLEXO_E2E_INTERFACES: 'a=::1' })- const origin = await serve({ size: SIZE, host: '::1' })- const reached = origin.hold(8 * BLOCK)- const id = await plexo.start(origin.url(), origin.sha256)- await reached-- await plexo.quit()- origin.release()- await plexo.launch()- expect((await plexo.current())?.status).toBe('paused')-- await plexo.api.resumeDownload(id)- await plexo.waitForStatus('completed')- })-test('normal quit mid-download → relaunch paused → resume', async ({ plexo, serve }) => {const origin = await serve({ size: SIZE })const reached = origin.hold(10 * BLOCK + 500)diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex ff3a131..692b54a 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -34,7 +34,7 @@ import { pickWork, type SchedulerPolicy, type Work } from './scheduler'import {compatibleInterfaces,NoCompatibleRouteError,- resolveTarget,+ resolveTargetWithin,targetHost} from '../network/routes'import {@@ -462,7 +462,10 @@ export class DownloadManager {throw new Error('Select at least one network interface')}const target = new URL(requestPayload.url)- const interfaces = compatibleInterfaces(selected, await resolveTarget(target))+ const interfaces = compatibleInterfaces(+ selected,+ await resolveTargetWithin(target, testKnobs.stallTimeoutMs)+ )if (interfaces.length === 0) throw new NoCompatibleRouteError(targetHost(target))return this.startWithInterfaces(requestPayload, interfaces)@@ -709,7 +712,7 @@ export class DownloadManager {try {runtime.activeInterfaces = compatibleInterfaces(runtime.activeInterfaces,- await resolveTarget(new URL(url))+ await resolveTargetWithin(new URL(url), testKnobs.stallTimeoutMs))} catch {runtime.state.error = 'Could not resolve the download host. Try resuming again.'diff --git a/src/main/download/probe.ts b/src/main/download/probe.tsindex 7d9f8cf..e4e6f94 100644--- a/src/main/download/probe.ts+++ b/src/main/download/probe.ts@@ -36,14 +36,19 @@ function requestOneByte(url: URL): Promise<ProbeResponse> {headers: { 'User-Agent': USER_AGENT, Range: 'bytes=0-0' }},(res) => {+ clearTimeout(timer)res.destroy()resolve({ statusCode: res.statusCode ?? 0, headers: res.headers as Headers })})- req.on('error', reject)- req.setTimeout(PROBE_TIMEOUT_MS, () =>- req.destroy(new Error('The server did not respond — check the link and try again'))+ const timer = setTimeout(+ () => req.destroy(new Error('The server did not respond — check the link and try again')),+ PROBE_TIMEOUT_MS)+ req.on('error', (error) => {+ clearTimeout(timer)+ reject(error)+ })req.end()})}diff --git a/src/main/network/latency.ts b/src/main/network/latency.tsindex ce266bc..22e9468 100644--- a/src/main/network/latency.ts+++ b/src/main/network/latency.ts@@ -1,19 +1,31 @@-import type { NetworkInterfaceInfo } from '../../shared/types'+import type { IpFamily, NetworkInterfaceInfo } from '../../shared/types'import { connectRoute } from './deviceBinding'-const PROBE_HOSTS = ['1.1.1.1', '8.8.8.8']+const PROBE_HOSTS: { address: string; family: IpFamily }[][] = [+ [+ { address: '1.1.1.1', family: 4 },+ { address: '2606:4700:4700::1111', family: 6 }+ ],+ [+ { address: '8.8.8.8', family: 4 },+ { address: '2001:4860:4860::8888', family: 6 }+ ]+]const PROBE_PORT = 443const TIMEOUT_MS = 2000/** Rough per-interface latency: time to open a TCP connection to a reliable* host, sourced from that interface's local address. null means unreachable. */-function measureLatencyToHost(iface: NetworkInterfaceInfo, host: string): Promise<number | null> {+function measureLatencyToHost(+ iface: NetworkInterfaceInfo,+ localAddress: string,+ remoteAddress: string,+ family: IpFamily+): Promise<number | null> {return new Promise((resolve) => {const start = Date.now()- const localAddress = iface.addresses.find((address) => address.family === 4)?.address- if (!localAddress) return resolve(null)const socket = connectRoute(- { device: iface.device, localAddress, remoteAddress: host, family: 4 },+ { device: iface.device, localAddress, remoteAddress, family },PROBE_PORT)let settled = false@@ -21,21 +33,28 @@ function measureLatencyToHost(iface: NetworkInterfaceInfo, host: string): Promisconst finish = (result: number | null): void => {if (settled) returnsettled = true+ clearTimeout(timer)socket.destroy()resolve(result)}- socket.setTimeout(TIMEOUT_MS)+ const timer = setTimeout(() => finish(null), TIMEOUT_MS)socket.once('connect', () => finish(Date.now() - start))- socket.once('timeout', () => finish(null))socket.once('error', () => finish(null))})}async function measureLatency(iface: NetworkInterfaceInfo): Promise<number | null> {- for (const host of PROBE_HOSTS) {- const latency = await measureLatencyToHost(iface, host)- if (latency !== null) return latency+ for (const hosts of PROBE_HOSTS) {+ const attempts = hosts.flatMap((host) =>+ iface.addresses+ .filter((local) => local.family === host.family)+ .map((local) => measureLatencyToHost(iface, local.address, host.address, host.family))+ )+ const latencies = (await Promise.all(attempts)).filter(+ (latency): latency is number => latency !== null+ )+ if (latencies.length > 0) return Math.min(...latencies)}return null}diff --git a/src/main/network/routes.ts b/src/main/network/routes.tsindex bf535e7..497ef96 100644--- a/src/main/network/routes.ts+++ b/src/main/network/routes.ts@@ -19,7 +19,7 @@ export function targetHost(target: URL): string {return target.hostname.replace(/^\[|\]$/g, '')}-export async function resolveTarget(+async function resolveTarget(target: URL,resolveHost: (host: string) => Promise<RemoteAddress[]> = async (host) =>(await lookup(host, { all: true, order: 'verbatim' })) as RemoteAddress[]@@ -60,6 +60,36 @@ export class NoCompatibleRouteError extends Error {}}+/** DNS is part of opening a request, so it must not outlive the request's deadline. */+export function resolveTargetWithin(+ target: URL,+ timeoutMs: number,+ signal?: AbortSignal,+ resolveHost?: (host: string) => Promise<RemoteAddress[]>+): Promise<RemoteAddress[]> {+ return new Promise((resolve, reject) => {+ let done = false+ const finish = (complete: () => void): void => {+ if (done) return+ done = true+ clearTimeout(timer)+ signal?.removeEventListener('abort', onAbort)+ complete()+ }+ const onAbort = (): void => finish(() => reject(new DOMException('Aborted', 'AbortError')))+ const timer = setTimeout(+ () => finish(() => reject(new Error('Could not resolve the download host in time'))),+ timeoutMs+ )+ if (signal?.aborted) return onAbort()+ signal?.addEventListener('abort', onAbort, { once: true })+ void resolveTarget(target, resolveHost).then(+ (addresses) => finish(() => resolve(addresses)),+ (error) => finish(() => reject(error))+ )+ })+}+/** Try another compatible route only if no response headers have arrived yet. */interface RequestOptions {target: URL@@ -79,18 +109,19 @@ export async function requestOnInterface({timeoutMs,resolveHost}: RequestOptions): Promise<{ req: ClientRequest; res: IncomingMessage; sentAt: number }> {- const routes = routesFor(iface, await resolveTarget(target, resolveHost))- if (routes.length === 0) throw new NoCompatibleRouteError(targetHost(target))const deadline = Date.now() + timeoutMs+ const routes = routesFor(iface, await resolveTargetWithin(target, timeoutMs, signal, resolveHost))+ if (routes.length === 0) throw new NoCompatibleRouteError(targetHost(target))let lastError: Error = new Error('Connection failed')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)))+ // A stale address gets a short connection window, but a connected server keeps+ // the full remaining time to send headers. The final route gets all that remains.+ const connectTimeout =+ index === routes.length - 1 ? remaining : Math.min(3_000, Math.max(1, remaining / 2))try {return await new Promise((resolve, reject) => {@@ -105,7 +136,7 @@ export async function requestOnInterface({...routeFrom(route, target)},(res) => {- clearTimeout(timer)+ clearTimers()signal?.removeEventListener('abort', onAbort)resolve({ req, res, sentAt })}@@ -113,12 +144,29 @@ export async function requestOnInterface({const onAbort = (): void => {req.destroy(new DOMException('Aborted', 'AbortError'))}- const timer = setTimeout(+ const connected = (): void => clearTimeout(connectTimer)+ const connectionEvent = target.protocol === 'https:' ? 'secureConnect' : 'connect'+ let socket: ClientRequest['socket']+ const clearTimers = (): void => {+ clearTimeout(connectTimer)+ clearTimeout(responseTimer)+ socket?.removeListener(connectionEvent, connected)+ }+ const connectTimer = setTimeout(+ () => req.destroy(new Error('Connection stalled: could not connect')),+ connectTimeout+ )+ const responseTimer = setTimeout(() => req.destroy(new Error('Connection stalled: no response from server')),- routeTimeout+ remaining)+ req.once('socket', (assignedSocket) => {+ socket = assignedSocket+ if (req.reusedSocket) connected()+ else socket.once(connectionEvent, connected)+ })req.once('error', (error) => {- clearTimeout(timer)+ clearTimers()signal?.removeEventListener('abort', onAbort)reject(error)})diff --git a/src/renderer/src/components/NetworkCard.tsx b/src/renderer/src/components/NetworkCard.tsxindex 3cb9937..2c27f92 100644--- a/src/renderer/src/components/NetworkCard.tsx+++ b/src/renderer/src/components/NetworkCard.tsx@@ -41,7 +41,7 @@ export function NetworkCard({onToggle}: NetworkCardProps): React.JSX.Element {const visual = useNetworkVisuals()(iface.id, iface.kind, iface.displayName)- const online = latencyMs != null+ const hasLatency = latencyMs != nullreturn (<div@@ -96,14 +96,15 @@ export function NetworkCard({</div><divclassName="font-mono text-[13px] leading-none font-medium"- style={{ color: online && selected ? visual.text : 'var(--text-secondary)' }}+ style={{ color: hasLatency && selected ? visual.text : 'var(--text-secondary)' }}>- {online ? `${latencyMs} ms` : '—'}+ {hasLatency ? `${latencyMs} ms` : '—'}</div></div><div+ title={hasLatency ? 'Latency measured' : 'Latency unavailable; downloads may still work'}className={`size-[7px] shrink-0 rounded-full ${- online ? 'bg-[var(--color-success)]' : 'bg-[var(--icon-muted)]'+ hasLatency ? 'bg-[var(--color-success)]' : 'bg-[var(--icon-muted)]'}`}/></div>