Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
fix: address review of the download-speed changes
Anmol Kapilbb6a7112356b
diff --git a/README.md b/README.mdindex e86531f..b9e3953 100644--- a/README.md+++ b/README.md@@ -141,7 +141,7 @@ Instead, Plexo uses a **dynamic work-stealing queue**:1. The file is split into **chunks of up to 8 MB** (smaller for small files, so every network gets a share).2. All chunks enter a centralized pending queue.3. A pool of worker connections continuously lease the next chunk from the queue as soon as they become free. Each keeps its one connection to the server from chunk to chunk, so it pays for the handshake and TCP's slow start once, not per chunk. Connections start interleaved across networks, so each network is served before any is served twice.- - **How many.** Each network starts with 4. More help only when something limits each connection on its own (a server capping per-connection speed, or a long, lossy route), not once the network itself is full, so Plexo measures instead of guessing: it doubles one network's connections and keeps them only if the whole download got at least 15% of that network's speed faster. Otherwise they finish their chunk and close. So do extra connections the server turns away. Up to 16 per network, and never more than there are chunks for.+ - **How many.** Each network starts with 4. More help only when something limits each connection on its own (a server capping per-connection speed, or a long, lossy route), not once the network itself is full, so Plexo measures instead of guessing. Once speeds have settled, it doubles one network's connections, waits for them to settle again, and keeps them only if that network got at least 15% faster, net of anything the other networks lost beyond their usual ups and downs (two networks behind one router share its uplink). Otherwise the new connections close, and their chunks go back to the queue to carry on from where they got to. So do extra connections the server turns away. Up to 16 per network, and never more than there are chunks for.4. Faster interfaces finish chunks quicker and immediately pick up new ones; slower interfaces pull fewer chunks.5. **Racing the tail.** Once no chunk is left waiting, a free connection can start a second attempt at a chunk another connection is fetching too slowly (one that still needs as long again as it has already taken, at least 5 seconds), picking up from where the first had got to. Whichever finishes first wins and the other is dropped. It costs a few bytes fetched twice at the very end, and it means one slow connection — or one slow network — can no longer hold the whole download back. A stream doing this is marked **BACKUP** in the streams table.diff --git a/e2e/chaos.spec.ts b/e2e/chaos.spec.tsindex 8db3474..65b275d 100644--- a/e2e/chaos.spec.ts+++ b/e2e/chaos.spec.ts@@ -138,7 +138,9 @@ test('any sequence of pauses, crashes and faults still ends in the exact file @cawait fc.assert(fc.asyncProperty(fc.integer({ min: 1, max: 2 ** 31 - 1 }),- fc.integer({ min: 1, max: 4 }),+ // A fixed count per network, or the app's own choice, which adds and retires streams as it+ // goes — through every pause, crash and fault the sequence throws at it.+ fc.oneof(fc.integer({ min: 1, max: 4 }), fc.constant('auto' as const)),commands,async (fileSeed, connections, cmds) => {const { dirs, dispose } = await makeDirs()@@ -147,7 +149,12 @@ test('any sequence of pauses, crashes and faults still ends in the exact file @cseed: fileSeed,bytesPerSecond: 512 * 1024}).start()- const app = new PlexoApp(dirs)+ // Short measuring windows, so streams come and go within the run; kept automatic across+ // the relaunches a crash or a quit brings.+ const app = new PlexoApp(+ dirs,+ connections === 'auto' ? { PLEXO_E2E_STREAMS: '', PLEXO_E2E_PROBE_MS: '500' } : {}+ )const real: Real = { app, origin, id: '', faults: [] }origin.setRule(({ range }) =>range && !(range.start === 0 && range.end === 0) ? real.faults.shift() : undefineddiff --git a/e2e/concurrency.spec.ts b/e2e/concurrency.spec.tsindex b39e950..fcb92ee 100644--- a/e2e/concurrency.spec.ts+++ b/e2e/concurrency.spec.ts@@ -2,76 +2,124 @@ import { expect, test } from '@playwright/test'import {ConcurrencyController,type Action,- type ConcurrencyPolicy+ type ConcurrencyPolicy,+ type Snapshot} from '../src/main/download/concurrency'// M. How many streams each network runs. The controller is pure, so it's driven here against-// models of what limits a download — the server, the link, an uplink two networks share — and-// judged by where it settles.+// models of what limits a download — the server, the link, an uplink two networks share — with+// the noise and ramp-up of real connections, and judged by where it settles.const MB = 1e6const POLICY: ConcurrencyPolicy = {maxPerNetwork: 16,windowMs: 2000,warmupMs: 2000,- minGain: 0.15+ minGain: 0.15,+ maxWindows: 4}const TICK_MS = 500+const SEEDS = 200-/** Bytes per second each network delivers, given how many streams each runs. */+/** Bytes per second each network settles at, given how many streams each runs. */type Model = (streams: Map<string, number>) => Map<string, number>+interface Options {+ seconds?: number+ spareWork?: number+ /** Each tick's delivery is off by up to this share, either way. */+ noise?: number+ seed?: number+ /** Seconds a network takes to get most of the way to a new speed (TCP ramping up). */+ ramp?: number+ /** Streams past this many on a network are turned away by the server. */+ accepts?: number+ /** Ticks a retired stream takes to leave. */+ retireTicks?: number+}+interface Run {streams: Map<string, number>/** Most streams each network had at once. */peak: Map<string, number>actions: Action[]+ /** What the controller saw whenever it acted. */+ seen: Snapshot[]+}++/** A seeded generator, so a failing case can be run again exactly. */+function random(seed: number): () => number {+ return () => {+ seed = (seed + 0x6d2b79f5) | 0+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed)+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296+ }}-function simulate(- model: Model,- networks: string[],- options: {- seconds?: number- spareWork?: number- failing?: (id: string, streams: number) => boolean- } = {}-): Run {- const { seconds = 120, spareWork = 1000, failing = () => false } = options+function simulate(model: Model, networks: string[], options: Options = {}): Run {+ const { seconds = 120, spareWork = 1000, noise = 0, seed = 1, ramp = 0 } = options+ const { accepts = Infinity, retireTicks = 1 } = options+ const next = random(seed)const controller = new ConcurrencyController(POLICY)const streams = new Map(networks.map((id) => [id, 4]))const peak = new Map(streams)const received = new Map(networks.map((id) => [id, 0]))- const actions: Action[] = []- let retiring = 0+ let rates = new Map(networks.map((id) => [id, 0]))+ const leaving: { networkId: string; count: number; at: number }[] = []+ const run: Run = { streams, peak, actions: [], seen: [] }+for (let now = 0; now <= seconds * 1000; now += TICK_MS) {- const rates = model(streams)- for (const id of networks)- received.set(id, received.get(id)! + (rates.get(id)! * TICK_MS) / 1000)- const action = controller.tick({+ const target = model(streams)+ const lag = ramp ? Math.exp(-TICK_MS / 1000 / ramp) : 0+ rates = new Map(+ networks.map((id) => [id, target.get(id)! + (rates.get(id)! - target.get(id)!) * lag])+ )+ for (const id of networks) {+ const jitter = 1 + noise * (next() * 2 - 1)+ received.set(id, received.get(id)! + (rates.get(id)! * jitter * TICK_MS) / 1000)+ }+ while (leaving.length > 0 && leaving[0].at <= now) {+ const gone = leaving.shift()!+ streams.set(gone.networkId, streams.get(gone.networkId)! - gone.count)+ }+ const snapshot: Snapshot = {now,networks: networks.map((id) => ({id,- streams: streams.get(id)!,+ streams:+ streams.get(id)! -+ leaving+ .filter((entry) => entry.networkId === id)+ .reduce((sum, entry) => sum + entry.count, 0),received: received.get(id)!,- failing: failing(id, streams.get(id)!)+ rejected: Math.max(0, streams.get(id)! - accepts)})),spareWork,- retiring- })- // Retired streams finish their block and are gone by the next look.- retiring = 0+ retiring: leaving.reduce((sum, entry) => sum + entry.count, 0)+ }+ const action = controller.tick(snapshot)if (!action) continue- actions.push(action)- const change = action.kind === 'add' ? action.count : -action.count- streams.set(action.networkId, streams.get(action.networkId)! + change)- peak.set(- action.networkId,- Math.max(peak.get(action.networkId)!, streams.get(action.networkId)!)- )- if (action.kind === 'retire') retiring = action.count+ run.actions.push(action)+ run.seen.push(snapshot)+ if (action.kind === 'add') {+ streams.set(action.networkId, streams.get(action.networkId)! + action.count)+ peak.set(+ action.networkId,+ Math.max(peak.get(action.networkId)!, streams.get(action.networkId)!)+ )+ } else {+ leaving.push({ ...action, at: now + retireTicks * TICK_MS })+ }}- return { streams, peak, actions }+ return run+}++/** The share of seeds for which `holds` is true. */+function share(holds: (seed: number) => boolean): number {+ let count = 0+ for (let seed = 1; seed <= SEEDS; seed++) if (holds(seed)) count+++ return count / SEEDS}/** Each connection gets at most `perStream`, and the link at most `link`. */@@ -80,6 +128,24 @@ const capped =(streams) =>new Map([...streams].map(([id, count]) => [id, Math.min(count * perStream, link)]))+/** Networks behind one uplink of `link`, each connection good for `perStream`. */+const sharedUplink =+ (perStream: number, link: number): Model =>+ (streams) => {+ const total = [...streams.values()].reduce((sum, count) => sum + count, 0)+ const delivered = Math.min(total * perStream, link)+ return new Map([...streams].map(([id, count]) => [id, (delivered * count) / total]))+ }++/** Ethernet full at 100 MB/s beside a cellular link, which more streams speed up or don't. */+const besideEthernet =+ (cellularGains: boolean): Model =>+ (streams) =>+ new Map([+ ['ethernet', Math.min(streams.get('ethernet')! * 50 * MB, 100 * MB)],+ ['cellular', cellularGains ? streams.get('cellular')! * 0.25 * MB : 1 * MB]+ ])+test.describe('stream count', () => {test('a server that caps each connection gets more of them, up to the limit', () => {const { streams, actions } = simulate(capped(1 * MB, 1000 * MB), ['a'])@@ -89,12 +155,24 @@ test.describe('stream count', () => {{ kind: 'add', networkId: 'a', count: 4 },{ kind: 'add', networkId: 'a', count: 8 }])+ expect(+ share(+ (seed) =>+ simulate(capped(1 * MB, 1000 * MB), ['a'], { noise: 0.1, seed }).streams.get('a') === 16+ )+ ).toBe(1)})test('a full link keeps what it started with: the extra streams are tried, then retired', () => {const { streams, peak } = simulate(capped(10 * MB, 20 * MB), ['a'])expect(peak.get('a')).toBe(8)expect(streams.get('a')).toBe(4)+ expect(+ share(+ (seed) =>+ simulate(capped(10 * MB, 20 * MB), ['a'], { noise: 0.1, seed }).streams.get('a') === 4+ )+ ).toBe(1)})test('grows while it pays and stops where it stops paying', () => {@@ -104,14 +182,26 @@ test.describe('stream count', () => {expect(simulate(capped(1 * MB, 9 * MB), ['a']).streams.get('a')).toBe(8)})- test('streams that only take speed from a network sharing the same uplink are retired', () => {- // Two networks behind one 8 MB/s uplink, each connection good for 1 MB/s.- const sharedUplink: Model = (streams) => {- const total = [...streams.values()].reduce((sum, count) => sum + count, 0)- const delivered = Math.min(total * MB, 8 * MB)- return new Map([...streams].map(([id, count]) => [id, (delivered * count) / total]))- }- const { streams, peak } = simulate(sharedUplink, ['wifi', 'ethernet'])+ test('a slow network is judged by its own gain, not lost in a fast one’s noise', () => {+ // Cellular's doubling is worth 1 MB/s; Ethernet's everyday ±10% is worth ±10 MB/s.+ const grows = share(+ (seed) =>+ simulate(besideEthernet(true), ['ethernet', 'cellular'], { noise: 0.1, seed }).streams.get(+ 'cellular'+ ) === 16+ )+ const staysPut = share(+ (seed) =>+ simulate(besideEthernet(false), ['ethernet', 'cellular'], { noise: 0.1, seed }).streams.get(+ 'cellular'+ ) === 4+ )+ expect(grows).toBeGreaterThanOrEqual(0.95)+ expect(staysPut).toBeGreaterThanOrEqual(0.95)+ })++ test('streams that only take speed from a network behind the same uplink are retired', () => {+ const { streams, peak } = simulate(sharedUplink(1 * MB, 8 * MB), ['wifi', 'ethernet'])expect(peak.get('wifi')).toBe(8)expect(peak.get('ethernet')).toBe(8)expect(streams).toEqual(@@ -120,18 +210,25 @@ test.describe('stream count', () => {['ethernet', 4]]))+ const bothStay = share((seed) => {+ const run = simulate(sharedUplink(1 * MB, 8 * MB), ['wifi', 'ethernet'], { noise: 0.1, seed })+ return run.streams.get('wifi') === 4 && run.streams.get('ethernet') === 4+ })+ expect(bothStay).toBeGreaterThanOrEqual(0.95)})- test("a slow network's real gain counts, however fast the network beside it", () => {- const model: Model = (streams) =>- new Map([- ['ethernet', Math.min(streams.get('ethernet')! * 50 * MB, 100 * MB)],- ['cellular', streams.get('cellular')! * 0.25 * MB]- ])- const { streams } = simulate(model, ['ethernet', 'cellular'])- // Cellular's doubling is a 1 MB/s gain against 100 MB/s in total, and it is kept all the same.- expect(streams.get('cellular')).toBe(16)- expect(streams.get('ethernet')).toBe(4)+ test('speeds still ramping up are waited out, not mistaken for a step’s gain', () => {+ // A full link whose connections take seconds to reach speed: measuring them halfway up would+ // make the first doubling look like it paid.+ for (const ramp of [1, 2, 4]) {+ const staysPut = share(+ (seed) =>+ simulate(capped(10 * MB, 20 * MB), ['a'], { ramp, noise: 0.05, seed }).streams.get(+ 'a'+ ) === 4+ )+ expect(staysPut, `ramping over ${ramp} s`).toBeGreaterThanOrEqual(0.95)+ }})test('networks take turns', () => {@@ -139,11 +236,8 @@ test.describe('stream count', () => {expect(actions.map((action) => action.networkId)).toEqual(['a', 'b', 'a', 'b'])})- test('new streams that fail are retired at once, and the network grows no further', () => {- // The server turns away anything past the first four connections.- const { streams, actions } = simulate(capped(1 * MB, 1000 * MB), ['a'], {- failing: (_id, count) => count > 4- })+ test('new streams the server turns away are retired, and the network grows no further', () => {+ const { streams, actions } = simulate(capped(1 * MB, 1000 * MB), ['a'], { accepts: 4 })expect(streams.get('a')).toBe(4)expect(actions).toEqual([{ kind: 'add', networkId: 'a', count: 4 },@@ -151,6 +245,16 @@ test.describe('stream count', () => {])})+ test('nothing is measured while retired streams are still leaving', () => {+ // Two networks behind one uplink: each step is undone, and the next network's is only taken+ // once the last one's streams have gone.+ const run = simulate(sharedUplink(1 * MB, 8 * MB), ['wifi', 'ethernet'], { retireTicks: 6 })+ expect(run.actions.map((action) => action.kind)).toEqual(['add', 'retire', 'add', 'retire'])+ for (const [index, action] of run.actions.entries()) {+ if (action.kind === 'add') expect(run.seen[index].retiring).toBe(0)+ }+ })+test('no growth without waiting blocks for the new streams to take', () => {expect(simulate(capped(1 * MB, 1000 * MB), ['a'], { spareWork: 0 }).actions).toEqual([])// Room for two more: the step is cut to fit.@@ -162,7 +266,47 @@ test.describe('stream count', () => {})test('a network delivering nothing is not grown', () => {- const { actions } = simulate(() => new Map([['a', 0]]), ['a'])- expect(actions).toEqual([])+ expect(simulate(() => new Map([['a', 0]]), ['a']).actions).toEqual([])+ })++ test('a pause mid-step gives back the untried streams; what was settled stays settled', () => {+ const controller = new ConcurrencyController(POLICY)+ const tick = (now: number, streams: number, received: number): Action | undefined =>+ controller.tick({+ now,+ networks: [{ id: 'a', streams, received, rejected: 0 }],+ spareWork: 1000,+ retiring: 0+ })+ // 1 MB/s whatever the stream count: a full link.+ let received = 0+ let now = 0+ let streams = 4+ const until = (done: (action: Action) => boolean): Action => {+ for (; now < 60_000; now += TICK_MS) {+ received += 0.5 * MB+ const action = tick(now, streams, received)+ if (action && done(action)) return action+ }+ throw new Error('never happened')+ }+ until((action) => action.kind === 'add')+ streams = 8+ now += TICK_MS+ // Paused partway through judging the step.+ expect(controller.interrupt()).toEqual({ kind: 'retire', networkId: 'a', count: 4 })+ streams = 4+ // Resumed: the step is tried again from scratch, found not to pay, and not tried a third time.+ expect(until((action) => action.kind === 'retire')).toEqual({+ kind: 'retire',+ networkId: 'a',+ count: 4+ })+ streams = 4+ expect(controller.interrupt()).toBeUndefined()+ for (let end = now + 30_000; now < end; now += TICK_MS) {+ received += 0.5 * MB+ expect(tick(now, streams, received)).toBeUndefined()+ }})})diff --git a/e2e/lifecycle.spec.ts b/e2e/lifecycle.spec.tsindex 865213b..7596c26 100644--- a/e2e/lifecycle.spec.ts+++ b/e2e/lifecycle.spec.ts@@ -46,6 +46,44 @@ test.describe('pause and resume @smoke', () => {await plexo.waitForStatus('completed')})+ test('resuming while a paused stream is still winding down waits for it, not runs beside it', async ({+ plexo,+ serve+ }) => {+ const origin = await serve({ size: SIZE, bytesPerSecond: 256 * 1024 })+ // One request a few blocks in is answered under a new label, so its stream checks whether+ // that's still the same file — and the sample it fetches for that never comes back. Pausing+ // can't cut the check short: the stream is still winding down when resume is pressed.+ let relabelled = false+ let stalled = false+ origin.setVersionRule(({ range }) => {+ if (relabelled || !range || range.start < 4 * BLOCK) return undefined+ relabelled = true+ return { content: origin.content, etag: '"v2"' }+ })+ origin.setRule(({ range }) => {+ const sample =+ range &&+ range.start > 0 &&+ range.start % BLOCK === 0 &&+ range.end !== null &&+ range.end - range.start < BLOCK / 4+ if (!sample || !relabelled || stalled) return undefined+ stalled = true+ return 'stallHeaders'+ })++ const id = await plexo.start(origin.url(), origin.sha256)+ await expect.poll(() => stalled, { message: 'the sample request hung' }).toBe(true)+ const pausing = plexo.api.pauseDownload(id)+ await plexo.waitForStatus('paused')+ await plexo.api.resumeDownload(id)+ await pausing+ // Two runs side by side would each try to finish the download, and the one left behind would+ // call it failed.+ await plexo.waitForStatus('completed', 30_000)+ })+test('pause and resume several times over the whole file', async ({ plexo, serve }) => {const origin = await serve({ size: SIZE, seed: 5 })let reached = origin.hold(2 * BLOCK + 5)diff --git a/e2e/network.spec.ts b/e2e/network.spec.tsindex ab0ee1f..aa16413 100644--- a/e2e/network.spec.ts+++ b/e2e/network.spec.ts@@ -149,3 +149,17 @@ test('a fresh connection that fails is not retried @smoke', async () => {await new Promise<void>((resolve) => server.close(() => resolve()))}})++test('a request abandoned while still connecting gives up the connection with it @smoke', async () => {+ // DNS that never answers stands in for any connect that hangs: the abort has to end it, not the+ // deadline.+ const connection = new StreamConnection(iface, 5000, () => new Promise(() => {}))+ const abort = new AbortController()+ const started = Date.now()+ setTimeout(() => abort.abort(), 100)+ await expect(+ connection.request(new URL('http://slow.example.test/'), {}, abort.signal)+ ).rejects.toMatchObject({ name: 'AbortError' })+ expect(Date.now() - started).toBeLessThan(1000)+ connection.close()+})diff --git a/e2e/savedProgress.spec.ts b/e2e/savedProgress.spec.tsnew file mode 100644index 0000000..cac0ecd--- /dev/null+++ b/e2e/savedProgress.spec.ts@@ -0,0 +1,89 @@+import { expect, test } from '@playwright/test'+import { planBlocks } from '../src/main/download/plan'+import { restoreBlocks, saveBlocks } from '../src/main/download/savedProgress'+import type { BlockState } from '../src/shared/types'++// P. What a download's saved progress says, and what it's trusted to say when read back.++const MIB = 1024 * 1024+const plan = { totalBytes: 10 * MIB, blockSizeBytes: 4 * MIB, totalBlocks: 3 }++/** A download of `plan` with some progress: block 0 done by two networks, block 1 partway. */+function inProgress(): BlockState[] {+ const blocks = planBlocks(plan.totalBytes, plan.blockSizeBytes)+ Object.assign(blocks[0], {+ status: 'completed',+ bytesDownloaded: 4 * MIB,+ bytesByInterface: { wifi: 3 * MIB, ethernet: MIB }+ })+ Object.assign(blocks[1], {+ status: 'downloading',+ bytesDownloaded: 1000,+ bytesByInterface: { wifi: 1000 }+ })+ return blocks+}++test.describe('saved progress', () => {+ test('what is saved comes back: progress, attribution, and which blocks are done', () => {+ const restored = restoreBlocks(plan, saveBlocks(inProgress()))!+ expect(restored.map((block) => [block.status, block.bytesDownloaded])).toEqual([+ ['completed', 4 * MIB],+ ['pending', 1000],+ ['pending', 0]+ ])+ expect(restored[0].bytesByInterface).toEqual({ wifi: 3 * MIB, ethernet: MIB })+ expect(restored.map((block) => [block.rangeStart, block.rangeEnd])).toEqual([+ [0, 4 * MIB - 1],+ [4 * MIB, 8 * MIB - 1],+ [8 * MIB, 10 * MIB - 1]+ ])+ })++ test('a finished file of unknown size comes back finished', () => {+ // One open-ended block: its bytes can't say it's done, so the save has to.+ const blocks = planBlocks(0, 0)+ Object.assign(blocks[0], {+ status: 'completed',+ bytesDownloaded: 5000,+ bytesByInterface: { wifi: 5000 }+ })+ const unknownSize = { totalBytes: 0, blockSizeBytes: 0, totalBlocks: 1 }+ expect(restoreBlocks(unknownSize, saveBlocks(blocks))?.[0].status).toBe('completed')+ // And one still going doesn't.+ blocks[0].status = 'downloading'+ expect(restoreBlocks(unknownSize, saveBlocks(blocks))?.[0].status).toBe('pending')+ })++ test('a plan that doesn’t add up is refused, before anything is allocated for it', () => {+ const saved = saveBlocks(inProgress())+ for (const blockSizeBytes of [0, -1, NaN, 1.5, undefined]) {+ expect(+ restoreBlocks({ ...plan, blockSizeBytes }, saved),+ String(blockSizeBytes)+ ).toBeUndefined()+ }+ // A block size of 1 byte would be ten million blocks; the saved count says 3.+ expect(restoreBlocks({ ...plan, blockSizeBytes: 1 }, saved)).toBeUndefined()+ expect(restoreBlocks({ ...plan, totalBlocks: 4 }, saved)).toBeUndefined()+ })++ test('progress that doesn’t fit the plan is dropped, and the blocks start over', () => {+ const good = saveBlocks(inProgress())+ const broken = [+ { wifi: [1, 2] }, // too few blocks+ { wifi: [0.5, 0, 0] }, // part of a byte+ { wifi: [-1, 0, 0] },+ { wifi: [5 * MIB, 0, 0] }, // more than the block holds+ { wifi: [3 * MIB, 0, 0], ethernet: [2 * MIB, 0, 0] }, // together, more than it holds+ { wifi: 'lots' }+ ]+ for (const progress of broken) {+ const restored = restoreBlocks(plan, { ...good, progress } as never)!+ expect(restored, JSON.stringify(progress)).toHaveLength(3)+ expect(+ restored.every((block) => block.bytesDownloaded === 0 && block.status === 'pending')+ ).toBe(true)+ }+ })+})diff --git a/e2e/streams.spec.ts b/e2e/streams.spec.tsindex 4a3a210..2a88304 100644--- a/e2e/streams.spec.ts+++ b/e2e/streams.spec.ts@@ -4,7 +4,8 @@ import { BLOCK, expect, test } from './fixtures'// server that limits it in each of the ways that matter.test.describe('automatic stream count', () => {- test.use({ appEnv: { PLEXO_E2E_PROBE_MS: '400' } })+ // Two ticks to a measuring window, so no one tick decides anything.+ test.use({ appEnv: { PLEXO_E2E_PROBE_MS: '1000' } })const peakStreams = (plexo: { sessions: { chunks: unknown[] }[][] }): number =>Math.max(...plexo.sessions.at(-1)!.map((state) => state.chunks.length))@@ -13,19 +14,20 @@ test.describe('automatic stream count', () => {plexo,serve}) => {- const origin = await serve({ size: 192 * BLOCK, bytesPerSecond: 256 * 1024 })+ const origin = await serve({ size: 384 * BLOCK, bytesPerSecond: 256 * 1024 })await plexo.start(origin.url(), origin.sha256, { connections: 'auto' })- const state = await plexo.waitForStatus('completed', 30_000)+ const state = await plexo.waitForStatus('completed', 40_000)expect(state.chunks).toHaveLength(16)+ expect(state.peakStreams).toBe(16)})test('a full link keeps the streams it started with: more are tried, then retired', async ({plexo,serve}) => {- const origin = await serve({ size: 64 * BLOCK, sharedBytesPerSecond: 1_000_000 })+ const origin = await serve({ size: 160 * BLOCK, sharedBytesPerSecond: 1_000_000 })await plexo.start(origin.url(), origin.sha256, { connections: 'auto' })- const state = await plexo.waitForStatus('completed', 30_000)+ const state = await plexo.waitForStatus('completed', 40_000)expect(peakStreams(plexo), 'more streams were tried').toBe(8)expect(state.chunks).toHaveLength(4)})@@ -34,7 +36,7 @@ test.describe('automatic stream count', () => {plexo,serve}) => {- const origin = await serve({ size: 64 * BLOCK, bytesPerSecond: 256 * 1024 })+ const origin = await serve({ size: 96 * BLOCK, bytesPerSecond: 256 * 1024 })const accepted = new Set<number>()origin.setRule(({ connection, range }) => {if (range?.start === 0 && range.end === 0) return undefined // the probe@@ -46,9 +48,26 @@ test.describe('automatic stream count', () => {})await plexo.start(origin.url(), origin.sha256, { connections: 'auto' })- const state = await plexo.waitForStatus('completed', 30_000)+ const state = await plexo.waitForStatus('completed', 40_000)expect(peakStreams(plexo), 'more streams were tried').toBe(8)expect(state.chunks).toHaveLength(4)expect(origin.log.some((request) => request.status === 503)).toBe(true)})++ test('paused while trying more streams: the untried ones go, and the rest carry on', async ({+ plexo,+ serve+ }) => {+ const origin = await serve({ size: 192 * BLOCK, bytesPerSecond: 256 * 1024 })+ const id = await plexo.start(origin.url(), origin.sha256, { connections: 'auto' })+ await plexo.waitUntil((state) => state.chunks.length === 8)++ await plexo.api.pauseDownload(id)+ const paused = await plexo.waitForStatus('paused')+ expect(paused.chunks, 'streams whose step was never judged').toHaveLength(4)++ await plexo.api.resumeDownload(id)+ const state = await plexo.waitForStatus('completed', 40_000)+ expect(state.chunks.length).toBeLessThanOrEqual(16)+ })})diff --git a/src/main/download/concurrency.ts b/src/main/download/concurrency.tsindex 7256cfa..908d788 100644--- a/src/main/download/concurrency.ts+++ b/src/main/download/concurrency.ts@@ -5,14 +5,18 @@// itself is full. Nothing outside says which, so the controller finds out the one way it can:// it adds streams to a network and keeps them if the download got faster.//+// - Speeds are only compared once they have settled: a measurement is taken window after window+// until no network is still speeding up. New connections ramp up, and a speed caught halfway+// would make whatever comes next look like a gain.// - One network at a time, doubling its streams (4 → 8 → 16): the answer in a couple of steps.-// - A step is kept when total speed rose by at least `minGain` of what that network was doing-// before it. Total, because two networks can share one uplink, and streams that only take-// speed from the other network gain nothing. The network's own speed as the yardstick,-// because a slow cellular link's real gain would otherwise vanish in a fast Ethernet's noise.-// - A step that doesn't pay is undone — its streams retire once their block is done — and that-// network grows no further. Nor does one whose new streams fail: the server's way of saying-// it wants fewer connections.+// - A step is kept when it made the download faster by at least `minGain` of that network's+// speed before it: the network's own gain, less what the other networks lost. Their loss only+// counts once it is more than `minGain` of their own speed — that much is beyond their+// everyday noise — so a slow link's real gain isn't lost in a fast one's ups and downs, while+// streams that only take speed from a network behind the same uplink still gain nothing.+// - A step that doesn't pay is undone and that network grows no further. So is one whose new+// streams are turned away: failing before they receive anything is how a server says it wants+// fewer connections.//// No I/O and no clock of its own: it reads a snapshot and says what to do, so every rule can be// checked against exact situations.@@ -23,8 +27,8 @@ export interface NetworkSnapshot {streams: number/** Everything its streams have received, ever. */received: number- /** One of its streams is failing: retrying, or given up. */- failing: boolean+ /** Its streams that have failed without ever receiving anything. */+ rejected: number}export interface Snapshot {@@ -32,7 +36,7 @@ export interface Snapshot {networks: readonly NetworkSnapshot[]/** How many more streams the waiting blocks could keep busy. */spareWork: number- /** Streams still winding down after a step that didn't pay. */+ /** Streams on their way out after a step that didn't pay. */retiring: number}@@ -40,52 +44,49 @@ export type Action = { kind: 'add' | 'retire'; networkId: string; count: numberexport interface ConcurrencyPolicy {maxPerNetwork: number- /** How long each measurement runs. */+ /** How long each measurement window runs. */windowMs: number- /** How long streams get to connect and reach speed before they're measured. */+ /** How long streams get to connect before they're measured at all. */warmupMs: number- /** The least a step must add to total speed, as a share of its network's speed before it. */+ /** The least a step must gain, as a share of its network's speed before it. */minGain: number+ /** Windows a measurement may take to settle before it is taken as it stands. */+ maxWindows: number}-/** Bytes per second, overall and per network. */-interface Rates {- total: number- byNetwork: Map<string, number>-}--interface Mark {- at: number- received: Map<string, number>+/** Bytes per second, by network id. */+type Rates = Map<string, number>++/** Measuring window after window until the speeds settle. */+interface Measurement {+ /** Nothing is measured before this. */+ startAt: number+ /** Where the current window began, once it has. */+ from: { at: number; received: Map<string, number> } | null+ /** The last window's speeds. */+ last: Rates | null+ windows: number}type Phase =- /** Letting streams reach speed before anything is measured. */- | { kind: 'wait'; until: number }/** Measuring the streams as they are. */- | { kind: 'measure'; from: Mark }- /** Streams were just added to `networkId`; letting them connect and reach speed. */- | { kind: 'warmup'; networkId: string; added: number; before: Rates; until: number }- /** Measuring whether they paid. */- | { kind: 'trial'; networkId: string; added: number; before: Rates; from: Mark }+ | { kind: 'measure'; measurement: Measurement }+ /** Streams were just added to `networkId`; measuring whether they paid. */+ | {+ kind: 'trial'+ networkId: string+ added: number+ before: Rates+ rejectedBefore: number+ measurement: Measurement+ }/** No network can grow any more. */| { kind: 'done' }-const mark = ({ now, networks }: Snapshot): Mark => ({- at: now,- received: new Map(networks.map((network) => [network.id, network.received]))-})--function ratesSince(from: Mark, { now, networks }: Snapshot): Rates {- const seconds = Math.max(now - from.at, 1) / 1000- const byNetwork = new Map<string, number>()+const sum = (rates: Rates, include: (id: string) => boolean): number => {let total = 0- for (const network of networks) {- const rate = (network.received - (from.received.get(network.id) ?? 0)) / seconds- byNetwork.set(network.id, rate)- total += rate- }- return { total, byNetwork }+ for (const [id, rate] of rates) if (include(id)) total += rate+ return total}export class ConcurrencyController {@@ -98,39 +99,33 @@ export class ConcurrencyController {constructor(private readonly policy: ConcurrencyPolicy) {}tick(snapshot: Snapshot): Action | undefined {- const { policy } = this- const phase = (this.phase ??= { kind: 'wait', until: snapshot.now + policy.warmupMs })+ const phase = (this.phase ??= { kind: 'measure', measurement: this.measurement(snapshot) })switch (phase.kind) {- case 'wait':- if (snapshot.now >= phase.until) this.phase = { kind: 'measure', from: mark(snapshot) }- return undefined-- case 'measure':+ case 'measure': {// Streams on their way out would count towards a speed that's about to drop.if (snapshot.retiring > 0) {- this.phase = { kind: 'measure', from: mark(snapshot) }+ phase.measurement = this.measurement(snapshot)return undefined}- if (snapshot.now - phase.from.at < policy.windowMs) return undefined- return this.grow(snapshot, ratesSince(phase.from, snapshot))-- case 'warmup':- if (this.failing(snapshot, phase.networkId)) return this.undo(snapshot, phase)- if (snapshot.now >= phase.until) {- const { networkId, added, before } = phase- this.phase = { kind: 'trial', networkId, added, before, from: mark(snapshot) }- }- return undefined+ const rates = this.measure(phase.measurement, snapshot)+ return rates && this.grow(snapshot, rates)+ }case 'trial': {- if (this.failing(snapshot, phase.networkId)) return this.undo(snapshot, phase)- if (snapshot.now - phase.from.at < policy.windowMs) return undefined- const after = ratesSince(phase.from, snapshot)- const networkBefore = phase.before.byNetwork.get(phase.networkId) ?? 0- if (after.total - phase.before.total < policy.minGain * networkBefore) {+ const network = snapshot.networks.find((entry) => entry.id === phase.networkId)+ if (!network || network.rejected > phase.rejectedBefore) return this.undo(snapshot, phase)+ const after = this.measure(phase.measurement, snapshot)+ if (!after) return undefined+ const { minGain } = this.policy+ const own = phase.networkId+ const ownBefore = phase.before.get(own) ?? 0+ const othersBefore = sum(phase.before, (id) => id !== own)+ const othersLoss = othersBefore - sum(after, (id) => id !== own)+ const loss = othersLoss > minGain * othersBefore ? othersLoss : 0+ if ((after.get(own) ?? 0) - ownBefore - loss < minGain * ownBefore) {return this.undo(snapshot, phase)}- // Kept: what was just measured is where the next step starts from.+ // Kept, and measured settled: that is where the next step starts from.return this.grow(snapshot, after)}@@ -139,6 +134,51 @@ export class ConcurrencyController {}}+ /** The download stopped (a pause): streams whose step was never judged are to go, and+ * measuring starts over when it runs again. What was settled stays settled. */+ interrupt(): Action | undefined {+ const phase = this.phase+ this.phase = phase?.kind === 'done' ? phase : null+ return phase?.kind === 'trial'+ ? { kind: 'retire', networkId: phase.networkId, count: phase.added }+ : undefined+ }++ private measurement(snapshot: Snapshot): Measurement {+ return { startAt: snapshot.now + this.policy.warmupMs, from: null, last: null, windows: 0 }+ }++ /** Advances a measurement; its speeds once they have settled, or once it has run out of+ * windows to settle in. */+ private measure(measurement: Measurement, { now, networks }: Snapshot): Rates | undefined {+ if (now < measurement.startAt) return undefined+ const received = new Map(networks.map((network) => [network.id, network.received]))+ const { from } = measurement+ if (!from) {+ measurement.from = { at: now, received }+ return undefined+ }+ if (now - from.at < this.policy.windowMs) return undefined++ const seconds = (now - from.at) / 1000+ const rates: Rates = new Map(+ networks.map((network) => [+ network.id,+ (network.received - (from.received.get(network.id) ?? 0)) / seconds+ ])+ )+ const { last } = measurement+ measurement.from = { at: now, received }+ measurement.last = rates+ measurement.windows+++ // Settled once no network is faster than in the last window by half what a step must gain:+ // any more, and what's still to come of a ramp could pass for a step's gain.+ const settled =+ last !== null &&+ [...rates].every(([id, rate]) => rate <= (last.get(id) ?? 0) * (1 + this.policy.minGain / 2))+ return settled || measurement.windows >= this.policy.maxWindows ? rates : undefined+ }+/** Adds streams to the next network that can take them, or measures again if none can yet. */private grow(snapshot: Snapshot, before: Rates): Action | undefined {const { networks, spareWork } = snapshot@@ -153,30 +193,30 @@ export class ConcurrencyController {this.policy.maxPerNetwork - network.streams,spareWork)- // A network delivering nothing has no speed to improve on.- if (network.failing || count < 1 || !((before.byNetwork.get(network.id) ?? 0) > 0)) continue+ // A network delivering nothing has no speed to improve on, and one already turning+ // streams away wants no more of them.+ if (count < 1 || network.rejected > 0 || !((before.get(network.id) ?? 0) > 0)) continuethis.next = (index + 1) % networks.lengththis.phase = {- kind: 'warmup',+ kind: 'trial',networkId: network.id,added: count,before,- until: snapshot.now + this.policy.warmupMs+ rejectedBefore: network.rejected,+ measurement: this.measurement(snapshot)}return { kind: 'add', networkId: network.id, count }}- this.phase = canGrowLater ? { kind: 'measure', from: mark(snapshot) } : { kind: 'done' }+ this.phase = canGrowLater+ ? { kind: 'measure', measurement: this.measurement(snapshot) }+ : { kind: 'done' }return undefined}- private undo(snapshot: Snapshot, phase: Extract<Phase, { kind: 'warmup' | 'trial' }>): Action {+ private undo(snapshot: Snapshot, phase: Extract<Phase, { kind: 'trial' }>): Action {this.settled.add(phase.networkId)- this.phase = { kind: 'measure', from: mark(snapshot) }+ this.phase = { kind: 'measure', measurement: this.measurement(snapshot) }return { kind: 'retire', networkId: phase.networkId, count: phase.added }}-- private failing(snapshot: Snapshot, networkId: string): boolean {- return snapshot.networks.find((network) => network.id === networkId)?.failing ?? true- }}diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 6c6c28a..4d0d5b4 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -15,12 +15,18 @@ import type {} from '../../shared/types'import { testKnobs, testStreamsPerNetwork } from '../testKnobs'import { advanceBlock, retractBlock } from './blockProgress'-import { ConcurrencyController, type ConcurrencyPolicy, type Snapshot } from './concurrency'+import {+ ConcurrencyController,+ type Action,+ type ConcurrencyPolicy,+ type Snapshot+} from './concurrency'import { downloadChunk, fetchRange, RemoteChangedError } from './chunkDownloader'import { DownloadFile } from './downloadFile'import { compareVersion, type FileVersion } from './fileVersion'import { ensureDirectory, reserveDestinationPath } from './paths'import { interleave, MAX_STREAMS_PER_NETWORK, planBlocks, planDownload } from './plan'+import { restoreBlocks, saveBlocks, type SavedBlocks } from './savedProgress'import { pickWork, type SchedulerPolicy, type Work } from './scheduler'import {compatibleInterfaces,@@ -91,7 +97,7 @@ interface ChunkRuntime {receivedBytes: number/** Failed attempts in a row, for backoff. */failures: number- /** Stops for good after the block it's on, and leaves the list (see retireStreams). */+ /** Stopped for good, and to leave the list once its worker has (see retireStreams). */retiring: boolean}@@ -133,6 +139,9 @@ interface DownloadRuntime {/** Everything each network's streams have received, by network id: what the stream count is* judged by (see concurrency.ts). */receivedByNetwork: Map<string, number>+ /** Decides how many streams each network runs; kept for the whole download, so a pause and+ * resume doesn't forget what it has found out. */+ concurrency: ConcurrencyController | null/** Updates sent to the window so far (see DownloadUpdate). */sentUpdates: number/** Each block as the window was last sent it, by index — what tells a changed block apart. */@@ -150,51 +159,12 @@ interface PersistedDownloadBase {type PersistedDownload = PersistedDownloadBase &(- | {- version: 5- state: Omit<DownloadState, 'blocks'>- /** Per network id, the bytes it delivered of each block, by block index. Everything else- * about a block follows from the plan: its range from `blockSizeBytes`, and that it is- * complete from those bytes adding up to its length. */- progress: Record<string, number[]>- }+ | ({ version: 5; state: Omit<DownloadState, 'blocks'> } & SavedBlocks)/** Written before version 5, with every block saved whole; still read, so an update doesn't* lose the progress of a download it finds paused. */| { version: 4; state: DownloadState })-/** Per network, the bytes it delivered of each block (see PersistedDownload). */-function progressByNetwork(blocks: readonly BlockState[]): Record<string, number[]> {- const progress: Record<string, number[]> = {}- for (const block of blocks) {- for (const [networkId, bytes] of Object.entries(block.bytesByInterface)) {- ;(progress[networkId] ??= new Array<number>(blocks.length).fill(0))[block.index] = bytes- }- }- return progress-}--/** The blocks a version 5 manifest describes. */-function restoreBlocks(- state: Omit<DownloadState, 'blocks'>,- progress: Record<string, number[]>-): BlockState[] {- const blocks = planBlocks(state.totalBytes, state.blockSizeBytes ?? state.totalBytes)- for (const [networkId, column] of Object.entries(progress)) {- column.forEach((bytes, index) => {- const block = blocks[index]- if (!block || !(bytes > 0)) return- block.bytesByInterface[networkId] = bytes- block.bytesDownloaded += bytes- })- }- for (const block of blocks) {- const length = block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1- if (state.status === 'completed' || block.bytesDownloaded === length) block.status = 'completed'- }- return blocks-}-// Set PLEXO_DEBUG=1 to log every request's outcome, how long the server took to answer and// whether it reused a warm connection — what it takes to tell one slow connection from a slow path.const debug: (...args: unknown[]) => void = process.env['PLEXO_DEBUG']@@ -270,7 +240,16 @@ const CONCURRENCY_POLICY: ConcurrencyPolicy = {maxPerNetwork: MAX_STREAMS_PER_NETWORK,windowMs: testKnobs.probeWindowMs,warmupMs: testKnobs.probeWindowMs,- minGain: 0.15+ minGain: 0.15,+ maxWindows: 4+}++/** How many streams a download runs is only for it to work out when there can be more than one+ * — a download without ranges or a known size is a single request — and unless a test fixes it. */+function concurrencyFor(request: StartDownloadRequest): ConcurrencyController | null {+ return request.supportsRanges && request.totalBytes > 0 && testStreamsPerNetwork() === null+ ? new ConcurrencyController(CONCURRENCY_POLICY)+ : null}// Idle connections look for work every 250 ms, so waiting a bit longer hands a refreshed block// to a connection that is already proven fast, if there is one.@@ -401,7 +380,7 @@ export class DownloadManager {if (persisted.state.id !== id) returnconst blocks =persisted.version === 5- ? restoreBlocks(persisted.state, persisted.progress)+ ? restoreBlocks(persisted.state, persisted): persisted.version === 4? persisted.state.blocks: undefined@@ -497,6 +476,7 @@ export class DownloadManager {attempts: new Map(),hedgesByBlock: new Map(),receivedByNetwork: new Map(),+ concurrency: concurrencyFor(persisted.requestPayload),sentUpdates: 0,sentBlocks: []})@@ -627,6 +607,7 @@ export class DownloadManager {speedBytesPerSec: 0,status: 'downloading',chunks,+ peakStreams: chunks.length,blocks,totalBlocks: blocks.length,blockSizeBytes: plan.blockSizeBytes,@@ -652,6 +633,7 @@ export class DownloadManager {attempts: new Map(),hedgesByBlock: new Map(),receivedByNetwork: new Map(),+ concurrency: concurrencyFor(requestPayload),sentUpdates: 0,sentBlocks: []}@@ -705,6 +687,9 @@ export class DownloadManager {// started on (see runWorker), which can tell a real change from a relabelled server.private async resumeAfterVerifying(runtime: DownloadRuntime): Promise<void> {const { url } = runtime.requestPayload+ // The paused run can still be winding down: a writer closing, a sample check in flight. A new+ // one must not start beside it — both would go on to publish, and act on each other's streams.+ await runtime.runPromiselet availableInterfaces: NetworkInterfaceInfo[]try {@@ -857,19 +842,20 @@ export class DownloadManager {}for (const chunk of chunks) startWorker(chunk)- // Streams are only added where there is more than one to have: a download without ranges or- // a known size is a single request.- const concurrency =- runtime.requestPayload.supportsRanges &&- runtime.state.totalBytes > 0 &&- testStreamsPerNetwork() === null- ? new ConcurrencyController(CONCURRENCY_POLICY)- : null+ const adjustStreams = (action: Action | undefined): void => {+ if (action?.kind === 'add') {+ for (const chunk of this.addStreams(runtime, action.networkId, action.count)) {+ startWorker(chunk)+ }+ } else if (action?.kind === 'retire') {+ for (const chunk of this.retireStreams(runtime, action.networkId, action.count)) {+ // One that already stopped has no worker left to remove it.+ if (!active.has(chunk.id)) this.removeStream(runtime, chunk)+ }+ }+ }const speedTicker = setInterval(() => {- // Only the `finally` below stops this ticker — self-clearing on status here would let a- // fast pause/resume start a second runChunksToCompletion (and ticker) while this one is- // still winding down, and it would never see 'downloading' flip back on its own.if (runtime.state.status !== 'downloading') returnconst now = Date.now()const prevSpeed = runtime.state.speedBytesPerSec@@ -879,18 +865,7 @@ export class DownloadManager {this.scheduleUpdate(runtime)}this.refreshStuckConnections(runtime, now)-- const action = concurrency?.tick(this.concurrencySnapshot(runtime, now))- if (action?.kind === 'add') {- for (const chunk of this.addStreams(runtime, action.networkId, action.count)) {- startWorker(chunk)- }- } else if (action?.kind === 'retire') {- for (const chunk of this.retireStreams(runtime, action.networkId, action.count)) {- // One that already stopped (it failed for good) has no worker left to remove it.- if (!active.has(chunk.id)) this.removeStream(runtime, chunk)- }- }+ adjustStreams(runtime.concurrency?.tick(this.concurrencySnapshot(runtime, now)))}, 500)speedTicker.unref()@@ -899,6 +874,8 @@ export class DownloadManager {} finally {clearInterval(speedTicker)}+ // Stopped partway through a step: streams it added were never judged, so they don't stay.+ if (runtime.state.status !== 'downloading') adjustStreams(runtime.concurrency?.interrupt())if (runtime.state.status === 'downloading' &&@@ -1305,24 +1282,17 @@ export class DownloadManager {return 'continue'}- /** The download was paused or cancelled while the request was in flight. */+ /** The stream was stopped while its request was in flight. */private finishStopped(runtime: DownloadRuntime,chunk: ChunkState,self: ChunkRuntime,attempt: Attempt): 'stop' {- const status = runtime.state.status as DownloadStatus- if (attempt.kind === 'primary') {- this.endAttempt(runtime, self, attempt)- if (status === 'paused') attempt.block.status = 'pending'- } else {- this.letGo(runtime, chunk, self, attempt, false)- }- chunk.status = status === 'paused' ? 'paused' : 'cancelled'- chunk.currentBlockIndex = undefined- chunk.hedge = undefined- chunk.speedBytesPerSec = 0+ // Whatever stopped it — a pause, a cancel, retirement — the block goes back as any other+ // attempt's would, and one another attempt has finished stays finished.+ this.letGo(runtime, chunk, self, attempt, false)+ chunk.status = runtime.state.status === 'paused' ? 'paused' : 'cancelled'return 'stop'}@@ -1466,17 +1436,17 @@ export class DownloadManager {private concurrencySnapshot(runtime: DownloadRuntime, now: number): Snapshot {const networks = runtime.activeInterfaces.map((iface) => {let streams = 0- let failing = false+ let rejected = 0for (const chunk of runtime.state.chunks) {- if (chunk.interfaceId !== iface.id || runtime.chunkRuntimes.get(chunk.id)?.retiring)- continue+ const self = runtime.chunkRuntimes.get(chunk.id)+ if (chunk.interfaceId !== iface.id || self?.retiring) continuestreams++- if (chunk.status === 'retrying' || chunk.status === 'error') failing = true+ if (self && self.failures > 0 && self.receivedBytes === 0) rejected++}return {id: iface.id,streams,- failing,+ rejected,received: runtime.receivedByNetwork.get(iface.id) ?? 0}})@@ -1494,13 +1464,17 @@ export class DownloadManager {let id = Math.max(-1, ...runtime.state.chunks.map((chunk) => chunk.id))const added = Array.from({ length: count }, () => newStream(++id, iface))runtime.state.chunks.push(...added)+ runtime.state.peakStreams = Math.max(+ runtime.state.peakStreams ?? 0,+ runtime.state.chunks.length+ )debug('streams', { network: iface.displayName, added: count })this.scheduleUpdate(runtime)return added}- /** Winds down the newest `count` streams on `networkId`. One between blocks — idle, or waiting- * out a retry — stops now; one partway through a block finishes it first. */+ /** Stops the newest `count` streams on `networkId` for good. Stopping partway through a block+ * costs nothing: what it wrote stays, and whoever takes the block next resumes from there. */private retireStreams(runtime: DownloadRuntime, networkId: string, count: number): ChunkState[] {const live = runtime.state.chunks.filter((chunk) => chunk.interfaceId === networkId && !runtime.chunkRuntimes.get(chunk.id)?.retiring@@ -1510,7 +1484,7 @@ export class DownloadManager {const self = runtime.chunkRuntimes.get(chunk.id)if (!self) continueself.retiring = true- if (!self.attempt) self.controller.abort()+ self.controller.abort()}debug('streams', { network: networkId, retired: count })return retired@@ -1750,7 +1724,7 @@ export class DownloadManager {version: 5,savedAt: Date.now(),state: structuredClone(state),- progress: progressByNetwork(blocks ?? runtime.blocks),+ ...saveBlocks(blocks ?? runtime.blocks),partialPath: runtime.file.path,publicationPath: runtime.publicationPath,publicationIdentity: runtime.publicationIdentity,diff --git a/src/main/download/savedProgress.ts b/src/main/download/savedProgress.tsnew file mode 100644index 0000000..61aa6a5--- /dev/null+++ b/src/main/download/savedProgress.ts@@ -0,0 +1,63 @@+import type { BlockState, DownloadState } from '../../shared/types'+import { planBlocks } from './plan'++/** How a download's blocks are saved (manifest version 5). Everything about a block but its+ * progress follows from the plan — its range from the block size — so only the progress is. */+export interface SavedBlocks {+ /** Per network id, the bytes it delivered of each block, by block index. A block is complete+ * when they add up to its length. */+ progress: Record<string, number[]>+ /** Every block is complete. Needed where a block's length is unknown — a file of unknown size+ * is one open-ended block — and so can't tell by its bytes. */+ complete: boolean+}++export function saveBlocks(blocks: readonly BlockState[]): SavedBlocks {+ const progress: Record<string, number[]> = {}+ for (const block of blocks) {+ for (const [networkId, bytes] of Object.entries(block.bytesByInterface)) {+ ;(progress[networkId] ??= new Array<number>(blocks.length).fill(0))[block.index] = bytes+ }+ }+ return { progress, complete: blocks.every((block) => block.status === 'completed') }+}++const isCount = (value: unknown): value is number =>+ Number.isSafeInteger(value) && (value as number) >= 0++/** The blocks saved as `saved` for a download planned as `state`. The file is read back from+ * disk, so it is checked like any input: a plan that doesn't add up is refused before anything+ * is allocated for it, and progress that doesn't fit the plan is dropped — the download starts+ * its blocks over rather than trusting bytes it can't account for. */+export function restoreBlocks(+ state: Pick<DownloadState, 'totalBytes' | 'blockSizeBytes' | 'totalBlocks'>,+ saved: SavedBlocks+): BlockState[] | undefined {+ const { totalBytes, blockSizeBytes, totalBlocks } = state+ if (!isCount(totalBytes) || !isCount(blockSizeBytes) || !isCount(totalBlocks)) return undefined+ if ((totalBytes > 0 ? Math.ceil(totalBytes / blockSizeBytes) : 1) !== totalBlocks)+ return undefined++ const fresh = (): BlockState[] => planBlocks(totalBytes, blockSizeBytes)+ const blocks = fresh()+ const columns =+ typeof saved.progress === 'object' && saved.progress !== null+ ? Object.entries(saved.progress)+ : []+ for (const [networkId, column] of columns) {+ if (!Array.isArray(column) || column.length !== blocks.length || !column.every(isCount)) {+ return fresh()+ }+ column.forEach((bytes, index) => {+ if (bytes === 0) return+ blocks[index].bytesByInterface[networkId] = bytes+ blocks[index].bytesDownloaded += bytes+ })+ }+ for (const block of blocks) {+ const length = block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1+ if (length !== null && block.bytesDownloaded > length) return fresh()+ if (saved.complete === true || block.bytesDownloaded === length) block.status = 'completed'+ }+ return blocks+}diff --git a/src/main/ipc/handlers.ts b/src/main/ipc/handlers.tsindex 4143c4b..668c804 100644--- a/src/main/ipc/handlers.ts+++ b/src/main/ipc/handlers.ts@@ -145,14 +145,6 @@ export function registerIpcHandlers(getWindow: () => BrowserWindow | null): Downreturn result.filePaths[0]})- handle('chooseSourceFile', async () => {- const window = getWindow()- if (!window) return null- const result = await dialog.showOpenDialog(window, { properties: ['openFile'] })- if (result.canceled || result.filePaths.length === 0) return null- return result.filePaths[0]- })-handle('readClipboardText', async () => clipboard.readText())handle('revealInFolder', async (_event, filePath) => {diff --git a/src/main/network/routes.ts b/src/main/network/routes.tsindex 2e31bac..45b11fe 100644--- a/src/main/network/routes.ts+++ b/src/main/network/routes.ts@@ -164,8 +164,12 @@ async function connectOnInterface(throw lastError}+/** What a request passes its agent: Node hands a request's options to the agent's+ * createConnection, so a connection opened for a request can be dropped with it. */+type ConnectOptions = ClientRequestArgs & { connectSignal?: AbortSignal }+type Open = (- options: ClientRequestArgs,+ options: ConnectOptions,secure: ((socket: Socket) => Socket) | null) => Promise<Socket>@@ -177,7 +181,7 @@ class RoutedHttpAgent extends HttpAgent {super({ keepAlive: true })}- override createConnection(options: ClientRequestArgs, callback?: OnCreate): undefined {+ override createConnection(options: ConnectOptions, callback?: OnCreate): undefined {this.open(options, null).then((socket) => callback?.(null, socket),(error: Error) => callback?.(error, undefined as never)@@ -191,7 +195,7 @@ class RoutedHttpsAgent extends HttpsAgent {super({ keepAlive: true })}- override createConnection(options: ClientRequestArgs, callback?: OnCreate): undefined {+ override createConnection(options: ConnectOptions, 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.@@ -238,7 +242,11 @@ export class StreamConnection {Number(options.port),secure,timeoutMs,- this.lifetime.signal,+ // Given up on when its request is, not only when the stream closes: a request abandoned+ // mid-connect (a stuck connection being replaced) shouldn't leave a handshake running.+ options.connectSignal+ ? AbortSignal.any([this.lifetime.signal, options.connectSignal])+ : this.lifetime.signal,resolveHost)this.http = new RoutedHttpAgent(open)@@ -272,21 +280,20 @@ export class StreamConnection {const sentAt = Date.now()let answered = falselet 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 options: ConnectOptions = {+ method: 'GET',+ hostname: targetHost(target),+ port: target.port || undefined,+ path: `${target.pathname}${target.search}`,+ headers,+ agent: secure ? this.https : this.http,+ connectSignal: signal+ }+ const req = (secure ? httpsRequest : httpRequest)(options, (res) => {+ answered = true+ settle()+ resolve({ req, res, sentAt })+ })const onAbort = (): void => void req.destroy(abortError())const timer = setTimeout(() => {timedOut = truediff --git a/src/preload/index.ts b/src/preload/index.tsindex 155cb40..28e1d05 100644--- a/src/preload/index.ts+++ b/src/preload/index.ts@@ -25,7 +25,6 @@ const plexoApi = {updateSettings: (patch: AppSettings) => invoke('updateSettings', patch),probeUrl: (url: string) => invoke('probeUrl', url),chooseDestinationFolder: (defaultPath: string) => invoke('chooseDestinationFolder', defaultPath),- chooseSourceFile: () => invoke('chooseSourceFile'),readClipboardText: () => invoke('readClipboardText'),revealInFolder: (filePath: string) => invoke('revealInFolder', filePath),startDownload: (request: IpcContract['startDownload']['args'][0]) =>diff --git a/src/renderer/src/components/ui/toggle-group.tsx b/src/renderer/src/components/ui/toggle-group.tsxdeleted file mode 100644index 7ab2456..0000000--- a/src/renderer/src/components/ui/toggle-group.tsx+++ /dev/null@@ -1,85 +0,0 @@-import * as React from 'react'-import { Toggle as TogglePrimitive } from '@base-ui/react/toggle'-import { ToggleGroup as ToggleGroupPrimitive } from '@base-ui/react/toggle-group'-import { type VariantProps } from 'class-variance-authority'-import { cn } from 'cn'--import { toggleVariants } from '@renderer/components/ui/toggle'--const ToggleGroupContext = React.createContext<- VariantProps<typeof toggleVariants> & {- spacing?: number- orientation?: 'horizontal' | 'vertical'- }->({- size: 'default',- variant: 'default',- spacing: 2,- orientation: 'horizontal'-})--function ToggleGroup({- className,- variant,- size,- spacing = 2,- orientation = 'horizontal',- children,- ...props-}: ToggleGroupPrimitive.Props &- VariantProps<typeof toggleVariants> & {- spacing?: number- orientation?: 'horizontal' | 'vertical'- }): React.JSX.Element {- return (- <ToggleGroupPrimitive- data-slot="toggle-group"- data-variant={variant}- data-size={size}- data-spacing={spacing}- data-orientation={orientation}- style={{ '--gap': spacing } as React.CSSProperties}- className={cn(- 'group/toggle-group flex w-fit flex-row items-center gap-[--spacing(var(--gap))] rounded-lg data-[size=sm]:rounded-[min(var(--radius-md),10px)] data-vertical:flex-col data-vertical:items-stretch',- className- )}- {...props}- >- <ToggleGroupContext.Provider value={{ variant, size, spacing, orientation }}>- {children}- </ToggleGroupContext.Provider>- </ToggleGroupPrimitive>- )-}--function ToggleGroupItem({- className,- children,- variant = 'default',- size = 'default',- ...props-}: TogglePrimitive.Props & VariantProps<typeof toggleVariants>): React.JSX.Element {- const context = React.useContext(ToggleGroupContext)-- return (- <TogglePrimitive- data-slot="toggle-group-item"- data-variant={context.variant || variant}- data-size={context.size || size}- data-spacing={context.spacing}- className={cn(- 'shrink-0 group-data-[spacing=0]/toggle-group:rounded-none group-data-[spacing=0]/toggle-group:px-2 focus:z-10 focus-visible:z-10 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-end]:pr-1.5 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-start]:pl-1.5 group-data-horizontal/toggle-group:data-[spacing=0]:first:rounded-l-lg group-data-vertical/toggle-group:data-[spacing=0]:first:rounded-t-lg group-data-horizontal/toggle-group:data-[spacing=0]:last:rounded-r-lg group-data-vertical/toggle-group:data-[spacing=0]:last:rounded-b-lg group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:border-l-0 group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:border-t-0 group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-l group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-t',- toggleVariants({- variant: context.variant || variant,- size: context.size || size- }),- className- )}- {...props}- >- {children}- </TogglePrimitive>- )-}--export { ToggleGroup, ToggleGroupItem }diff --git a/src/renderer/src/components/ui/toggle.tsx b/src/renderer/src/components/ui/toggle.tsxdeleted file mode 100644index 7d43025..0000000--- a/src/renderer/src/components/ui/toggle.tsx+++ /dev/null@@ -1,48 +0,0 @@-import { Toggle as TogglePrimitive } from '@base-ui/react/toggle'-import { cva, type VariantProps } from 'class-variance-authority'-import { cn } from 'cn'--const toggleVariants = cva(- "group/toggle inline-flex items-center justify-center gap-1 rounded-lg text-sm font-medium whitespace-nowrap transition-all outline-none hover:bg-muted hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-pressed:bg-muted data-[state=on]:bg-muted dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",- {- variants: {- variant: {- default: 'bg-transparent',- outline: 'border border-input bg-transparent hover:bg-muted',- // A selectable preset pill (e.g. "1×"/"2×"/"4×"/"8×" stream-count pickers) — muted/outlined- // at rest, switches to the primary/accent color once picked.- pill: 'border border-border bg-muted font-mono text-[var(--text-secondary)] hover:bg-muted hover:text-[var(--text-secondary)] aria-pressed:border-primary aria-pressed:bg-primary aria-pressed:text-primary-foreground data-[state=on]:border-primary data-[state=on]:bg-primary data-[state=on]:text-primary-foreground'- },- size: {- default:- 'h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2',- xs: "h-5 min-w-5 gap-1 rounded-[min(var(--radius-md),8px)] px-1.5 text-[10px] font-semibold [&_svg:not([class*='size-'])]:size-3",- sm: "h-7 min-w-7 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",- lg: 'h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2'- }- },- defaultVariants: {- variant: 'default',- size: 'default'- }- }-)--function Toggle({- className,- variant = 'default',- size = 'default',- ...props-}: TogglePrimitive.Props & VariantProps<typeof toggleVariants>): React.JSX.Element {- return (- <TogglePrimitive- data-slot="toggle"- className={cn(toggleVariants({ variant, size, className }))}- {...props}- />- )-}--export { Toggle }-// eslint-disable-next-line react-refresh/only-export-components -- shadcn convention: variants ship alongside the component-export { toggleVariants }diff --git a/src/renderer/src/screens/CompleteScreen.tsx b/src/renderer/src/screens/CompleteScreen.tsxindex 929015f..d492156 100644--- a/src/renderer/src/screens/CompleteScreen.tsx+++ b/src/renderer/src/screens/CompleteScreen.tsx@@ -101,7 +101,8 @@ export function CompleteScreen({{ label: 'Time', value: formatDuration(elapsedSeconds) },{ label: 'Peak', value: formatSpeed(peakSpeedBytesPerSec) },{ label: 'Networks', value: String(groups.length) },- { label: 'Streams', value: String(download.chunks.length) }+ // The most it ran at once: streams that didn't make it faster were closed along the way.+ { label: 'Streams', value: String(download.peakStreams ?? download.chunks.length) }].map((stat, index) => (<divkey={stat.label}diff --git a/src/renderer/src/screens/IdleScreen.tsx b/src/renderer/src/screens/IdleScreen.tsxindex 014df84..f1d1893 100644--- a/src/renderer/src/screens/IdleScreen.tsx+++ b/src/renderer/src/screens/IdleScreen.tsx@@ -104,6 +104,9 @@ export function IdleScreen(): React.JSX.Element {const ready = probe.status === 'ready' ? probe.result : nullconst multiChunkAllowed = ready !== null && ready.supportsRanges && ready.totalBytes !== nullconst isSingleStreamOnly = ready !== null && !multiChunkAllowed+ // One request has to carry the whole file: either the server can't serve parts of it, or it+ // didn't say how big it is, so there's no telling where the parts would be.+ const sizeUnknown = isSingleStreamOnly && ready.supportsRangesconst detectedIds = interfaces.map((iface) => iface.id)const enabledIds = detectedIds.filter((id) => !deselectedInterfaceIds.includes(id))@@ -120,7 +123,13 @@ export function IdleScreen(): React.JSX.Element {]// How many streams each network gets is worked out during the download; the one case worth// saying up front is a server that can't split the file at all.- if (isSingleStreamOnly) footerParts.push('1 stream: the server can’t split this file')+ if (isSingleStreamOnly) {+ footerParts.push(+ sizeUnknown+ ? '1 stream: the file’s size isn’t known'+ : '1 stream: the server can’t split this file'+ )+ }if (ready && ready.totalBytes !== null) footerParts.push(formatBytes(ready.totalBytes))let subnetConflict: { subnet: string; names: string[] } | null = null@@ -245,7 +254,11 @@ export function IdleScreen(): React.JSX.Element {{isSingleStreamOnly && (<InfoAlerttitle="Single-connection mode"- message="This server does not support parallel range requests (206 Partial Content). The download will run as a single stream through whichever network you choose below."+ message={+ sizeUnknown+ ? 'The server didn’t say how big this file is, so it can’t be split into parts. The download will run as a single stream through whichever network you choose below.'+ : 'This server does not support parallel range requests (206 Partial Content). The download will run as a single stream through whichever network you choose below.'+ }/>)}diff --git a/src/shared/downloadUpdate.ts b/src/shared/downloadUpdate.tsindex b7a63d5..049f7b1 100644--- a/src/shared/downloadUpdate.ts+++ b/src/shared/downloadUpdate.ts@@ -9,8 +9,12 @@ export function applyDownloadUpdate(if (current?.id === state.id) {// A snapshot taken after this was sent already has it.if ((current.seq ?? 0) >= seq) return current- const blocks = [...(current.blocks ?? [])]- for (const block of changed) blocks[block.index] = block+ // A new array only when a block changed, so what shows the blocks can tell when they did.+ let blocks = current.blocks+ if (changed.length > 0) {+ blocks = [...(blocks ?? [])]+ for (const block of changed) blocks[block.index] = block+ }return { ...state, blocks, seq }}// Another download: only an update with every block in it stands on its own — the first onediff --git a/src/shared/ipc-channels.ts b/src/shared/ipc-channels.tsindex 1390ad9..5adb8fa 100644--- a/src/shared/ipc-channels.ts+++ b/src/shared/ipc-channels.ts@@ -7,7 +7,6 @@ export const IpcChannels = {getInitialState: 'app:get-initial-state',updateSettings: 'app:update-settings',chooseDestinationFolder: 'dialog:choose-destination-folder',- chooseSourceFile: 'dialog:choose-source-file',readClipboardText: 'clipboard:read-text',revealInFolder: 'shell:reveal-in-folder',startDownload: 'download:start',diff --git a/src/shared/ipc-contract.ts b/src/shared/ipc-contract.tsindex 8f25c88..646c143 100644--- a/src/shared/ipc-contract.ts+++ b/src/shared/ipc-contract.ts@@ -19,7 +19,6 @@ export interface IpcContract {updateSettings: { args: [patch: AppSettings]; result: void }probeUrl: { args: [url: string]; result: ProbeResult }chooseDestinationFolder: { args: [defaultPath: string]; result: string | null }- chooseSourceFile: { args: []; result: string | null }readClipboardText: { args: []; result: string }revealInFolder: { args: [filePath: string]; result: void }startDownload: { args: [request: StartDownloadRequest]; result: string }diff --git a/src/shared/types.ts b/src/shared/types.tsindex baf379a..5a0eefd 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -94,6 +94,8 @@ export interface DownloadState {speedBytesPerSec: numberstatus: DownloadStatuschunks: ChunkState[]+ /** The most streams it has run at once. */+ peakStreams?: numberblocks?: BlockState[]totalBlocks?: numberblockSizeBytes?: number