Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

perf: keep one connection per stream instead of one per block

Anmol Kapil5a2358c11c10
diff --git a/e2e/download.spec.ts b/e2e/download.spec.ts
index 7bdaf87..634d288 100644
--- a/e2e/download.spec.ts
+++ b/e2e/download.spec.ts
@@ -43,6 +43,10 @@ test.describe('happy paths @smoke', () => {
await plexo.start(origin.url(), origin.sha256, { connections })
const state = await plexo.waitForStatus('completed')
expect(state.chunks).toHaveLength(connections)
+ // Each stream keeps its connection from one block to the next rather than reconnecting.
+ const requests = origin.chunkRequests()
+ expect(new Set(requests.map((request) => request.connection)).size).toBe(connections)
+ expect(requests.length).toBeGreaterThan(connections)
})
}
diff --git a/e2e/network.spec.ts b/e2e/network.spec.ts
index e56bb50..ab0ee1f 100644
--- a/e2e/network.spec.ts
+++ b/e2e/network.spec.ts
@@ -1,8 +1,8 @@
import { expect, test } from '@playwright/test'
-import { createServer } from 'node:http'
+import { createServer, type IncomingMessage } from 'node:http'
import type { AddressInfo } from 'node:net'
import type { NetworkInterfaceInfo } from '../src/shared/types'
-import { requestOnInterface } from '../src/main/network/routes'
+import { StreamConnection } from '../src/main/network/routes'
const iface: NetworkInterfaceInfo = {
id: 'wifi',
@@ -15,6 +15,17 @@ const iface: NetworkInterfaceInfo = {
]
}
+const loopback: NetworkInterfaceInfo = {
+ ...iface,
+ addresses: [{ address: '127.0.0.1', family: 4 }]
+}
+
+async function body(res: IncomingMessage): Promise<string> {
+ let text = ''
+ for await (const chunk of res) text += chunk
+ return text
+}
+
test('AAAA-only hostname connects over IPv6 and keeps its HTTP Host header @smoke', async () => {
let seenHost = ''
const server = createServer((req, res) => {
@@ -22,25 +33,18 @@ test('AAAA-only hostname connects over IPv6 and keeps its HTTP Host header @smok
res.end('ok')
})
await new Promise<void>((resolve) => server.listen(0, '::1', resolve))
+ const connection = new StreamConnection(
+ { ...iface, addresses: [{ address: '::1', family: 6 }] },
+ 2000,
+ async () => [{ address: '::1', family: 6 }]
+ )
try {
const port = (server.address() as AddressInfo).port
- const target = new URL(`http://ipv6.example.test:${port}/file`)
- const source: NetworkInterfaceInfo = {
- ...iface,
- addresses: [{ address: '::1', family: 6 }]
- }
- const { res } = await requestOnInterface({
- target,
- iface: source,
- headers: {},
- timeoutMs: 2000,
- resolveHost: async () => [{ address: '::1', family: 6 }]
- })
- let body = ''
- for await (const chunk of res) body += chunk
- expect(body).toBe('ok')
+ const { res } = await connection.request(new URL(`http://ipv6.example.test:${port}/file`), {})
+ expect(await body(res)).toBe('ok')
expect(seenHost).toBe(`ipv6.example.test:${port}`)
} finally {
+ connection.close()
await new Promise<void>((resolve) => server.close(() => resolve()))
}
})
@@ -50,41 +54,98 @@ test('a working route gets time to answer after an earlier route fails @smoke',
setTimeout(() => res.end('ipv6'), 1800)
})
await new Promise<void>((resolve) => server.listen(0, '::1', resolve))
- try {
- const port = (server.address() as AddressInfo).port
- 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: 3000,
- resolveHost: async () => [
+ const connection = new StreamConnection(
+ {
+ ...iface,
+ addresses: [
{ address: '127.0.0.1', family: 4 },
- { address: '::1', family: 6 },
- { address: '127.0.0.2', family: 4 }
+ { address: '::1', family: 6 }
]
- })
- let body = ''
- for await (const chunk of res) body += chunk
- expect(body).toBe('ipv6')
+ },
+ 3000,
+ async () => [
+ { address: '127.0.0.1', family: 4 },
+ { address: '::1', family: 6 },
+ { address: '127.0.0.2', family: 4 }
+ ]
+ )
+ try {
+ const port = (server.address() as AddressInfo).port
+ const { res } = await connection.request(new URL(`http://dual.example.test:${port}/`), {})
+ expect(await body(res)).toBe('ipv6')
} finally {
+ connection.close()
await new Promise<void>((resolve) => server.close(() => resolve()))
}
})
test('DNS resolution obeys the request deadline @smoke', async () => {
+ const connection = new StreamConnection(iface, 100, () => new Promise(() => {}))
await expect(
- requestOnInterface({
- target: new URL('http://unresolved.example.test/file'),
- iface,
- headers: {},
- timeoutMs: 100,
- resolveHost: () => new Promise(() => {})
- })
+ connection.request(new URL('http://unresolved.example.test/file'), {})
).rejects.toThrow('Could not resolve the download host in time')
+ connection.close()
+})
+
+test('one stream sends request after request down a single connection @smoke', async () => {
+ let connections = 0
+ const server = createServer((_req, res) => res.end('ok'))
+ server.on('connection', () => connections++)
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
+ const connection = new StreamConnection(loopback, 2000)
+ try {
+ const url = new URL(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`)
+ const reused: boolean[] = []
+ for (let i = 0; i < 3; i++) {
+ const { req, res } = await connection.request(url, {})
+ expect(await body(res)).toBe('ok')
+ reused.push(req.reusedSocket)
+ }
+ expect(reused).toEqual([false, true, true])
+ expect(connections).toBe(1)
+ } finally {
+ connection.close()
+ await new Promise<void>((resolve) => server.close(() => resolve()))
+ }
+})
+
+test('a kept-alive connection the server dropped is replaced without failing the request @smoke', async () => {
+ let requests = 0
+ let connections = 0
+ // The second request lands on the reused connection and is dropped unanswered, as happens when
+ // a server times out an idle connection just as the next request goes out.
+ const server = createServer((req, res) => {
+ if (++requests === 2) req.socket.destroy()
+ else res.end(`answer ${requests}`)
+ })
+ server.on('connection', () => connections++)
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
+ const connection = new StreamConnection(loopback, 2000)
+ try {
+ const url = new URL(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`)
+ expect(await body((await connection.request(url, {})).res)).toBe('answer 1')
+ expect(await body((await connection.request(url, {})).res)).toBe('answer 3')
+ expect(connections).toBe(2)
+ } finally {
+ connection.close()
+ await new Promise<void>((resolve) => server.close(() => resolve()))
+ }
+})
+
+test('a fresh connection that fails is not retried @smoke', async () => {
+ let requests = 0
+ const server = createServer((req) => {
+ requests++
+ req.socket.destroy()
+ })
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
+ const connection = new StreamConnection(loopback, 2000)
+ try {
+ const url = new URL(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`)
+ await expect(connection.request(url, {})).rejects.toThrow()
+ expect(requests).toBe(1)
+ } finally {
+ connection.close()
+ await new Promise<void>((resolve) => server.close(() => resolve()))
+ }
})
diff --git a/e2e/origin.ts b/e2e/origin.ts
index b9934cb..9aadd5e 100644
--- a/e2e/origin.ts
+++ b/e2e/origin.ts
@@ -39,6 +39,8 @@ export interface OriginRequest {
path: string
/** Source address — which "network" the request came in on. */
from: string
+ /** Which TCP connection it arrived on, numbered in the order they opened. */
+ connection: number
range: { start: number; end: number | null } | null
}
@@ -105,6 +107,8 @@ export class Origin {
private releasePromise: Promise<void> = Promise.resolve()
private reachedPromise: Promise<void> = Promise.resolve()
private sockets = new Set<Socket>()
+ private connectionIds = new WeakMap<Socket, number>()
+ private connections = 0
private server = createServer((req, res) => void this.handle(req, res))
private port = 0
@@ -113,6 +117,7 @@ export class Origin {
this.etag = options.etag === undefined ? '"v1"' : options.etag
this.lastModified = options.lastModified ?? null
this.server.on('connection', (socket) => {
+ this.connectionIds.set(socket, ++this.connections)
this.sockets.add(socket)
socket.on('close', () => this.sockets.delete(socket))
})
@@ -188,6 +193,7 @@ export class Origin {
n: this.log.length + 1,
path: req.url ?? '/',
from: (req.socket.remoteAddress ?? '').replace(/^::ffff:/, ''),
+ connection: this.connectionIds.get(req.socket) ?? 0,
range: parseRange(req.headers.range)
}
const fault = this.rule(request) ?? 'ok'
diff --git a/src/main/download/chunkDownloader.ts b/src/main/download/chunkDownloader.ts
index 7d61b83..cb1f103 100644
--- a/src/main/download/chunkDownloader.ts
+++ b/src/main/download/chunkDownloader.ts
@@ -1,8 +1,7 @@
import type { Writable } from 'node:stream'
import type { ClientRequest, IncomingMessage } from 'node:http'
import { URL } from 'node:url'
-import type { NetworkInterfaceInfo } from '../../shared/types'
-import { requestOnInterface } from '../network/routes'
+import type { StreamConnection } from '../network/routes'
import { testKnobs } from '../testKnobs'
import { compareVersion, type FileVersion, type VersionCheck } from './fileVersion'
@@ -11,7 +10,8 @@ export interface ChunkDownloadOptions {
rangeStart: number
/** null = open-ended range, download to end of file. */
rangeEnd: number | null
- interfaceInfo: NetworkInterfaceInfo
+ /** The stream's connection, through the network it's bound to. */
+ connection: StreamConnection
createDestination: () => Writable
/** Network bytes received, before destination backpressure or disk writes. */
onNetworkProgress: (bytesReceivedThisRun: number) => void
@@ -97,7 +97,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
url,
rangeStart,
rangeEnd,
- interfaceInfo,
+ connection,
createDestination,
onNetworkProgress,
onProgress,
@@ -171,13 +171,8 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
}
const attempt = (targetUrl: URL, redirectsLeft: number): void => {
- void requestOnInterface({
- target: targetUrl,
- iface: interfaceInfo,
- headers,
- signal,
- timeoutMs: STALL_TIMEOUT_MS
- })
+ void connection
+ .request(targetUrl, headers, signal)
.then(({ req, res, sentAt }) => {
if (settled) {
req.destroy()
@@ -323,16 +318,12 @@ export function fetchRange(
url: string,
start: number,
end: number,
- interfaceInfo: NetworkInterfaceInfo
+ connection: StreamConnection
): Promise<{ body: Buffer; version: FileVersion }> {
return new Promise((resolve, reject) => {
const attempt = (target: URL, redirectsLeft: number): void => {
- void requestOnInterface({
- target,
- iface: interfaceInfo,
- headers: { 'User-Agent': 'Plexo/1.0', Range: `bytes=${start}-${end}` },
- timeoutMs: STALL_TIMEOUT_MS
- })
+ void connection
+ .request(target, { 'User-Agent': 'Plexo/1.0', Range: `bytes=${start}-${end}` })
.then(({ req, res }) => {
req.on('error', reject)
const status = res.statusCode ?? 0
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index 24f9ca3..e2347b1 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -26,6 +26,7 @@ import {
compatibleInterfaces,
NoCompatibleRouteError,
resolveTargetWithin,
+ StreamConnection,
targetHost
} from '../network/routes'
import {
@@ -86,6 +87,8 @@ type AttemptOutcome =
interface ChunkRuntime {
/** Aborts the whole stream: pause and cancel. */
controller: AbortController
+ /** Its one connection to the server, reused from block to block. */
+ connection: StreamConnection
/** What the stream is fetching right now, if anything. */
attempt: Attempt | null
/** When this connection last (re)connected; it isn't judged until SLOW_WARMUP_MS after. */
@@ -498,7 +501,7 @@ export class DownloadManager {
const target = new URL(requestPayload.url)
const interfaces = compatibleInterfaces(
selected,
- await resolveTargetWithin(target, testKnobs.stallTimeoutMs)
+ await resolveTargetWithin(targetHost(target), testKnobs.stallTimeoutMs)
)
if (interfaces.length === 0) throw new NoCompatibleRouteError(targetHost(target))
@@ -741,7 +744,7 @@ export class DownloadManager {
try {
runtime.activeInterfaces = compatibleInterfaces(
runtime.activeInterfaces,
- await resolveTargetWithin(new URL(url), testKnobs.stallTimeoutMs)
+ await resolveTargetWithin(targetHost(new URL(url)), testKnobs.stallTimeoutMs)
)
} catch {
runtime.state.error = 'Could not resolve the download host. Try resuming again.'
@@ -1149,7 +1152,6 @@ export class DownloadManager {
runtime: DownloadRuntime,
chunk: ChunkState,
self: ChunkRuntime,
- iface: NetworkInterfaceInfo,
attempt: Attempt
): Promise<AttemptOutcome> {
const { block } = attempt
@@ -1181,7 +1183,7 @@ export class DownloadManager {
url: runtime.requestPayload.url,
rangeStart: block.rangeStart + attempt.startOffset,
rangeEnd: block.rangeEnd,
- interfaceInfo: iface,
+ connection: self.connection,
createDestination: () =>
attempt.kind === 'primary'
? runtime.file.writer(block.rangeStart + (attempt.startOffset ?? 0))
@@ -1282,7 +1284,7 @@ export class DownloadManager {
case 'aborted':
return this.finishAborted(runtime, chunk, self, attempt, outcome.reason)
case 'failed':
- return this.finishFailed(runtime, chunk, self, iface, attempt, outcome.error)
+ return this.finishFailed(runtime, chunk, self, attempt, outcome.error)
}
}
@@ -1410,7 +1412,6 @@ export class DownloadManager {
runtime: DownloadRuntime,
chunk: ChunkState,
self: ChunkRuntime,
- iface: NetworkInterfaceInfo,
attempt: Attempt,
error: unknown
): Promise<'continue' | 'stop'> {
@@ -1422,7 +1423,7 @@ export class DownloadManager {
const verdict =
error.check.kind === 'size'
? 'different'
- : await this.confirmSameBytes(runtime, iface, error.seen)
+ : await this.confirmSameBytes(runtime, self.connection, error.seen)
// The check takes a moment; the download may have been paused or stopped meanwhile.
if ((runtime.state.status as DownloadStatus) !== 'downloading') {
return this.finishStopped(runtime, chunk, self, attempt)
@@ -1561,6 +1562,7 @@ export class DownloadManager {
const controller = new AbortController()
const self: ChunkRuntime = {
controller,
+ connection: new StreamConnection(iface, testKnobs.stallTimeoutMs),
attempt: null,
warmSince: Date.now(),
slowSince: null,
@@ -1570,55 +1572,60 @@ export class DownloadManager {
}
runtime.chunkRuntimes.set(chunk.id, self)
- while (runtime.state.status === 'downloading') {
- if (controller.signal.aborted) break
-
- // Taken atomically: nothing between choosing the work and registering it can yield.
- const work = pickWork(
- {
- blocks: runtime.blocks,
- streams: runtime.state.chunks,
- attempts: runtime.attempts,
- avoid: runtime.avoidNetworkByBlock,
- hedgesUsed: runtime.hedgesByBlock
- },
- { id: chunk.id, networkId: iface.id },
- Date.now(),
- SCHEDULER_POLICY
- )
- if (
- work?.kind === 'hedge' &&
- [...runtime.attempts.values()].flat().filter((attempt) => attempt.kind === 'hedge')
- .length >= MAX_ACTIVE_HEDGES
- ) {
- this.goIdle(chunk)
- await delay(250, controller.signal)
- continue
- }
- if (!work) {
- this.goIdle(chunk)
- // While blocks are still in flight, stay available in case one fails and is handed back,
- // or turns out to be slow enough to be worth racing.
- if (runtime.blocks.some((b) => b.status === 'pending' || b.status === 'downloading')) {
- this.scheduleUpdate(runtime)
+ try {
+ while (runtime.state.status === 'downloading') {
+ if (controller.signal.aborted) break
+
+ // Taken atomically: nothing between choosing the work and registering it can yield.
+ const work = pickWork(
+ {
+ blocks: runtime.blocks,
+ streams: runtime.state.chunks,
+ attempts: runtime.attempts,
+ avoid: runtime.avoidNetworkByBlock,
+ hedgesUsed: runtime.hedgesByBlock
+ },
+ { id: chunk.id, networkId: iface.id },
+ Date.now(),
+ SCHEDULER_POLICY
+ )
+ if (
+ work?.kind === 'hedge' &&
+ [...runtime.attempts.values()].flat().filter((attempt) => attempt.kind === 'hedge')
+ .length >= MAX_ACTIVE_HEDGES
+ ) {
+ this.goIdle(chunk)
await delay(250, controller.signal)
continue
}
- chunk.status = 'completed'
- this.scheduleUpdate(runtime)
- break
- }
+ if (!work) {
+ this.goIdle(chunk)
+ // While blocks are still in flight, stay available in case one fails and is handed back,
+ // or turns out to be slow enough to be worth racing.
+ if (runtime.blocks.some((b) => b.status === 'pending' || b.status === 'downloading')) {
+ this.scheduleUpdate(runtime)
+ await delay(250, controller.signal)
+ continue
+ }
+ chunk.status = 'completed'
+ this.scheduleUpdate(runtime)
+ break
+ }
- const attempt = this.beginAttempt(runtime, chunk, self, iface, work)
- this.scheduleUpdate(runtime)
- const outcome = await this.executeAttempt(runtime, chunk, self, iface, attempt)
- let next: 'continue' | 'stop'
- try {
- next = await this.finishAttempt(runtime, chunk, self, iface, attempt, outcome)
- } finally {
- this.endAttempt(runtime, self, attempt)
+ const attempt = this.beginAttempt(runtime, chunk, self, iface, work)
+ this.scheduleUpdate(runtime)
+ const outcome = await this.executeAttempt(runtime, chunk, self, attempt)
+ let next: 'continue' | 'stop'
+ try {
+ next = await this.finishAttempt(runtime, chunk, self, iface, attempt, outcome)
+ } finally {
+ this.endAttempt(runtime, self, attempt)
+ }
+ if (next === 'stop') break
}
- if (next === 'stop') break
+ } finally {
+ // Its sockets would otherwise stay open, kept alive for requests that will never come.
+ self.connection.close()
}
this.scheduleUpdate(runtime)
@@ -1635,7 +1642,7 @@ export class DownloadManager {
*/
private async confirmSameBytes(
runtime: DownloadRuntime,
- iface: NetworkInterfaceInfo,
+ connection: StreamConnection,
seen: FileVersion
): Promise<'same' | 'different' | 'unknown'> {
if (compareVersion(runtime.acceptedVersions, seen).kind === 'same') return 'same'
@@ -1668,7 +1675,7 @@ export class DownloadManager {
runtime.requestPayload.url,
block.rangeStart,
block.rangeStart + local.length - 1,
- iface
+ connection
)
// A reply from a server still presenting an accepted label proves nothing here.
if (compareVersion([seen], remote.version).kind !== 'same') continue
diff --git a/src/main/download/simDownload.ts b/src/main/download/simDownload.ts
index a4d2812..b1a375f 100644
--- a/src/main/download/simDownload.ts
+++ b/src/main/download/simDownload.ts
@@ -76,7 +76,7 @@ export function downloadChunkSimulated(options: ChunkDownloadOptions): Promise<v
url,
rangeStart,
rangeEnd,
- interfaceInfo,
+ connection,
createDestination,
onNetworkProgress,
onProgress,
@@ -95,7 +95,7 @@ export function downloadChunkSimulated(options: ChunkDownloadOptions): Promise<v
return
}
- const network = session.networks.get(interfaceInfo.id)
+ const network = session.networks.get(connection.iface.id)
if (network && Math.random() * 100 < network.faultRatePercent) {
reject(new Error(`Simulated drop on ${network.label}`))
return
diff --git a/src/main/network/deviceBinding.ts b/src/main/network/deviceBinding.ts
index a1d671d..9c70983 100644
--- a/src/main/network/deviceBinding.ts
+++ b/src/main/network/deviceBinding.ts
@@ -1,6 +1,4 @@
-import type { ClientRequestArgs } from 'node:http'
-import { connect, isIP, Socket, type SocketConstructorOpts } from 'node:net'
-import { checkServerIdentity, connect as tlsConnect } from 'node:tls'
+import { connect, Socket, type SocketConstructorOpts } from 'node:net'
import type { NetworkRoute } from './routes'
// Linux picks a socket's outgoing interface from the routing table alone: binding to an
@@ -91,33 +89,3 @@ export function connectRoute(route: NetworkRoute, port: number): Socket {
family
})
}
-
-/** Convert an explicit route into request options without changing the URL's HTTP authority. */
-export function routeFrom(
- route: NetworkRoute,
- target: URL
-): Pick<
- ClientRequestArgs,
- 'localAddress' | 'family' | 'createConnection' | 'port' | 'defaultPort'
-> {
- const secure = target.protocol === 'https:'
- const defaultPort = secure ? 443 : 80
- const port = Number(target.port) || defaultPort
- const host = target.hostname.replace(/^\[|\]$/g, '')
- return {
- localAddress: route.localAddress,
- family: route.family,
- port,
- defaultPort,
- createConnection: () => {
- const socket = connectRoute(route, port)
- return secure
- ? tlsConnect({
- socket,
- servername: isIP(host) ? undefined : host,
- checkServerIdentity: (_name, cert) => checkServerIdentity(host, cert)
- })
- : socket
- }
- }
-}
diff --git a/src/main/network/routes.ts b/src/main/network/routes.ts
index 497ef96..2e31bac 100644
--- a/src/main/network/routes.ts
+++ b/src/main/network/routes.ts
@@ -1,9 +1,16 @@
import { lookup } from 'node:dns/promises'
-import { request as httpRequest, type ClientRequest, type IncomingMessage } from 'node:http'
-import { request as httpsRequest } from 'node:https'
-import { isIP } from 'node:net'
+import {
+ Agent as HttpAgent,
+ request as httpRequest,
+ type ClientRequest,
+ type ClientRequestArgs,
+ type IncomingMessage
+} from 'node:http'
+import { Agent as HttpsAgent, request as httpsRequest } from 'node:https'
+import { isIP, type Socket } from 'node:net'
+import type { Duplex } from 'node:stream'
import type { IpFamily, NetworkInterfaceInfo } from '../../shared/types'
-import { routeFrom } from './deviceBinding'
+import { connectRoute } from './deviceBinding'
export interface NetworkRoute {
device: string
@@ -14,17 +21,19 @@ export interface NetworkRoute {
export type RemoteAddress = { address: string; family: IpFamily }
+/** Test seam for an AAAA-only hostname without modifying system DNS. */
+type ResolveHost = (host: string) => Promise<RemoteAddress[]>
+
/** URL.hostname brackets IPv6 literals; sockets and isIP expect the unbracketed address. */
export function targetHost(target: URL): string {
return target.hostname.replace(/^\[|\]$/g, '')
}
async function resolveTarget(
- target: URL,
- resolveHost: (host: string) => Promise<RemoteAddress[]> = async (host) =>
- (await lookup(host, { all: true, order: 'verbatim' })) as RemoteAddress[]
+ host: string,
+ resolveHost: ResolveHost = async (name) =>
+ (await lookup(name, { all: true, order: 'verbatim' })) as RemoteAddress[]
): Promise<RemoteAddress[]> {
- const host = targetHost(target)
const literalFamily = isIP(host)
if (literalFamily) return [{ address: host, family: literalFamily as IpFamily }]
return resolveHost(host)
@@ -60,12 +69,12 @@ export class NoCompatibleRouteError extends Error {
}
}
-/** DNS is part of opening a request, so it must not outlive the request's deadline. */
+/** DNS is part of opening a connection, so it must not outlive the connection's deadline. */
export function resolveTargetWithin(
- target: URL,
+ host: string,
timeoutMs: number,
signal?: AbortSignal,
- resolveHost?: (host: string) => Promise<RemoteAddress[]>
+ resolveHost?: ResolveHost
): Promise<RemoteAddress[]> {
return new Promise((resolve, reject) => {
let done = false
@@ -76,107 +85,230 @@ export function resolveTargetWithin(
signal?.removeEventListener('abort', onAbort)
complete()
}
- const onAbort = (): void => finish(() => reject(new DOMException('Aborted', 'AbortError')))
+ const onAbort = (): void => finish(() => reject(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(
+ void resolveTarget(host, 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
- iface: NetworkInterfaceInfo
- headers: Record<string, string>
- signal?: AbortSignal
- timeoutMs: number
- /** Test seam for an AAAA-only hostname without modifying system DNS. */
- resolveHost?: (host: string) => Promise<RemoteAddress[]>
-}
+const abortError = (): DOMException => new DOMException('Aborted', 'AbortError')
-export async function requestOnInterface({
- target,
- iface,
- headers,
- signal,
- timeoutMs,
- resolveHost
-}: RequestOptions): Promise<{ req: ClientRequest; res: IncomingMessage; sentAt: number }> {
+/**
+ * A socket to `host` through `iface`, trying each compatible route in DNS order until one
+ * completes its handshake. `secure` wraps the TCP socket in TLS before that point, so a route
+ * whose TLS handshake never finishes is given up on just like one that never connects.
+ */
+async function connectOnInterface(
+ iface: NetworkInterfaceInfo,
+ host: string,
+ port: number,
+ secure: ((socket: Socket) => Socket) | null,
+ timeoutMs: number,
+ signal: AbortSignal,
+ resolveHost?: ResolveHost
+): Promise<Socket> {
const deadline = Date.now() + timeoutMs
- const routes = routesFor(iface, await resolveTargetWithin(target, timeoutMs, signal, resolveHost))
- if (routes.length === 0) throw new NoCompatibleRouteError(targetHost(target))
+ const routes = routesFor(iface, await resolveTargetWithin(host, timeoutMs, signal, resolveHost))
+ if (routes.length === 0) throw new NoCompatibleRouteError(host)
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 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 =
+ // A stale address gets a short window, so the next can still be tried; the last route gets
+ // all that remains.
+ const window =
index === routes.length - 1 ? remaining : Math.min(3_000, Math.max(1, remaining / 2))
-
+ const tcp = connectRoute(route, port)
+ const socket = secure ? secure(tcp) : tcp
try {
- return await new Promise((resolve, reject) => {
- const requester = target.protocol === 'https:' ? httpsRequest : httpRequest
- const sentAt = Date.now()
- const req = requester(
- {
- method: 'GET',
- hostname: targetHost(target),
- path: `${target.pathname}${target.search}`,
- headers,
- ...routeFrom(route, target)
- },
- (res) => {
- clearTimers()
- signal?.removeEventListener('abort', onAbort)
- resolve({ req, res, sentAt })
- }
- )
- const onAbort = (): void => {
- req.destroy(new DOMException('Aborted', 'AbortError'))
- }
- 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)
+ await new Promise<void>((resolve, reject) => {
+ const settle = (error?: Error): void => {
+ clearTimeout(timer)
+ signal.removeEventListener('abort', onAbort)
+ socket.off(secure ? 'secureConnect' : 'connect', onConnected)
+ socket.off('error', settle)
+ tcp.off('error', settle)
+ if (error) reject(error)
+ else resolve()
}
- 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')),
- remaining
+ const onConnected = (): void => settle()
+ const onAbort = (): void => settle(abortError())
+ const timer = setTimeout(
+ () => settle(new Error('Connection stalled: could not connect')),
+ window
)
- req.once('socket', (assignedSocket) => {
- socket = assignedSocket
- if (req.reusedSocket) connected()
- else socket.once(connectionEvent, connected)
- })
- req.once('error', (error) => {
- clearTimers()
- signal?.removeEventListener('abort', onAbort)
- reject(error)
- })
- signal?.addEventListener('abort', onAbort, { once: true })
- req.end()
+ socket.once(secure ? 'secureConnect' : 'connect', onConnected)
+ // A refused TCP connection is reported on the raw socket, not always on its TLS wrapper.
+ socket.once('error', settle)
+ tcp.once('error', settle)
+ if (signal.aborted) onAbort()
+ else signal.addEventListener('abort', onAbort, { once: true })
})
+ return socket
} catch (error) {
- if (signal?.aborted) throw new DOMException('Aborted', 'AbortError')
+ socket.destroy()
+ tcp.destroy()
+ if (signal.aborted) throw abortError()
lastError = error instanceof Error ? error : new Error(String(error))
}
}
throw lastError
}
+
+type Open = (
+ options: ClientRequestArgs,
+ secure: ((socket: Socket) => Socket) | null
+) => Promise<Socket>
+
+type OnCreate = (err: Error | null, stream: Duplex) => void
+
+// Node's agents keep sockets alive and pool them; these only change how a new one is opened.
+class RoutedHttpAgent extends HttpAgent {
+ constructor(private readonly open: Open) {
+ super({ keepAlive: true })
+ }
+
+ override createConnection(options: ClientRequestArgs, callback?: OnCreate): undefined {
+ this.open(options, null).then(
+ (socket) => callback?.(null, socket),
+ (error: Error) => callback?.(error, undefined as never)
+ )
+ return undefined
+ }
+}
+
+class RoutedHttpsAgent extends HttpsAgent {
+ constructor(private readonly open: Open) {
+ super({ keepAlive: true })
+ }
+
+ override createConnection(options: ClientRequestArgs, callback?: OnCreate): undefined {
+ // The agent's own TLS setup, around our routed socket: it verifies the certificate against
+ // the URL's host and caches the TLS session, so a reconnect resumes it instead of redoing
+ // the full handshake.
+ this.open(
+ options,
+ (socket) => super.createConnection({ ...options, socket } as ClientRequestArgs) as Socket
+ ).then(
+ (socket) => callback?.(null, socket),
+ (error: Error) => callback?.(error, undefined as never)
+ )
+ return undefined
+ }
+}
+
+export interface ResponseStart {
+ req: ClientRequest
+ res: IncomingMessage
+ /** When the request was sent. */
+ sentAt: number
+}
+
+/**
+ * One download stream's connection to the server, through one network. Requests reuse a single
+ * kept-alive socket, so a stream pays for DNS, the TCP and TLS handshakes and TCP slow start once
+ * rather than on every block. A new socket is only opened when there is none to reuse: the first
+ * request, after the server closes an idle one, or after an abort destroyed it, which is how a
+ * stuck connection gets swapped for a fresh one.
+ */
+export class StreamConnection {
+ private readonly lifetime = new AbortController()
+ private readonly http: HttpAgent
+ private readonly https: HttpsAgent
+
+ constructor(
+ readonly iface: NetworkInterfaceInfo,
+ /** How long a request may take to get its response headers, connecting included. */
+ private readonly timeoutMs: number,
+ resolveHost?: ResolveHost
+ ) {
+ const open: Open = (options, secure) =>
+ connectOnInterface(
+ iface,
+ options.host ?? '',
+ Number(options.port),
+ secure,
+ timeoutMs,
+ this.lifetime.signal,
+ resolveHost
+ )
+ this.http = new RoutedHttpAgent(open)
+ this.https = new RoutedHttpsAgent(open)
+ }
+
+ request(
+ target: URL,
+ headers: Record<string, string>,
+ signal?: AbortSignal
+ ): Promise<ResponseStart> {
+ return this.send(target, headers, signal, true)
+ }
+
+ /** Closes its sockets, and gives up on any still connecting. */
+ close(): void {
+ this.lifetime.abort()
+ this.http.destroy()
+ this.https.destroy()
+ }
+
+ private send(
+ target: URL,
+ headers: Record<string, string>,
+ signal: AbortSignal | undefined,
+ retryStale: boolean
+ ): Promise<ResponseStart> {
+ return new Promise((resolve, reject) => {
+ if (signal?.aborted) return reject(abortError())
+ const secure = target.protocol === 'https:'
+ const sentAt = Date.now()
+ let answered = false
+ let timedOut = false
+ const req = (secure ? httpsRequest : httpRequest)(
+ {
+ method: 'GET',
+ hostname: targetHost(target),
+ port: target.port || undefined,
+ path: `${target.pathname}${target.search}`,
+ headers,
+ agent: secure ? this.https : this.http
+ },
+ (res) => {
+ answered = true
+ settle()
+ resolve({ req, res, sentAt })
+ }
+ )
+ const onAbort = (): void => void req.destroy(abortError())
+ const timer = setTimeout(() => {
+ timedOut = true
+ req.destroy(new Error('Connection stalled: no response from server'))
+ }, this.timeoutMs)
+ const settle = (): void => {
+ clearTimeout(timer)
+ signal?.removeEventListener('abort', onAbort)
+ }
+ req.on('error', (error) => {
+ if (answered) return // The response's owner handles errors from here on.
+ settle()
+ // A kept-alive socket the server closed while it sat idle only fails once used, so the
+ // request gets one more go on a new socket (the retry Node documents for `reusedSocket`).
+ if (retryStale && req.reusedSocket && !timedOut && !signal?.aborted) {
+ resolve(this.send(target, headers, signal, false))
+ } else {
+ reject(error)
+ }
+ })
+ signal?.addEventListener('abort', onAbort, { once: true })
+ req.end()
+ })
+ }
+}