Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
Split the last blocks between free streams, and stop the holder at the split (#154)
Arpit Khandelwale184f75d15f7
diff --git a/e2e/fixtures.ts b/e2e/fixtures.tsindex 1c164bb..f64fc5f 100644--- a/e2e/fixtures.ts+++ b/e2e/fixtures.ts@@ -473,8 +473,9 @@ export function checkEvents(sessions: DownloadState[][]): void {)} else if (state.status === 'downloading') {// A stream holds a block exactly while it is fetching it. A block has at most one stream- // fetching it for real and two racing it as hedges, and is in flight whenever the first- // is there. What the stream rows show is only as true as this.+ // fetching it for real from where it has got to, two racing that one as hedges, and any+ // number fetching parts split off further on; it is in flight whenever the first is+ // there. What the stream rows show is only as true as this.const primaries = new Map<number, number>()const hedges = new Map<number, number>()for (const stream of state.streams) {@@ -486,8 +487,14 @@ export function checkEvents(sessions: DownloadState[][]): void {)if (stream.currentBlockIndex === undefined) {assert.ok(!stream.hedge, `${label}: idle stream ${stream.id} is not racing`)+ assert.ok(!stream.split, `${label}: idle stream ${stream.id} is not splitting`)continue}+ assert.ok(+ !(stream.hedge && stream.split),+ `${label}: stream ${stream.id} is a hedge or a split, not both`+ )+ if (stream.split) continueconst tally = stream.hedge ? hedges : primariestally.set(stream.currentBlockIndex, (tally.get(stream.currentBlockIndex) ?? 0) + 1)}diff --git a/e2e/hedge.spec.ts b/e2e/hedge.spec.tsindex 6849b61..fe7b245 100644--- a/e2e/hedge.spec.ts+++ b/e2e/hedge.spec.ts@@ -1,5 +1,5 @@import { BLOCK, expect, LAN_ADDRESS, test } from './fixtures'-import type { Fault, OriginRequest } from './origin'+import type { Fault, LoggedRequest, Origin, OriginRequest } from './origin'// L. Racing a slow block. Near the end of a download, a free stream fetches the rest of a block// whose holder is too slow, and whichever finishes first wins. Every test here also gets the@@ -114,3 +114,100 @@ test.describe('racing a slow block', () => {expect(shared.length, 'one block was split between the networks').toBeGreaterThanOrEqual(1)})})++// M. Splitting a block at the end. Once no block is waiting, a free stream takes the back half of+// what a block's holder has left, and the holder stops where it begins: each byte is fetched+// once. What matters most is a server that caps each connection, where racing the holder would+// only fetch the same bytes as slowly.+test.describe('splitting a block at the end', () => {+ const MIB = 1024 * 1024+ const RATE = 128 * 1024+ // Five 1 MiB blocks, four streams, every response at 128 KB/s: the fifth block goes to+ // whichever stream frees first, about 8 s in, and alone would take 8 s more.+ const SIZE = 5 * MIB+ const LAST = 4 * MIB+ test.use({ appEnv: { PLEXO_E2E_BLOCK_BYTES: String(MIB), PLEXO_E2E_SILENT_MS: '1000' } })++ const lastBlock = (origin: Origin): LoggedRequest[] =>+ origin.chunkRequests().filter((r) => r.range && r.range.start >= LAST)+ /** Requests that began partway into the last block: splits. */+ const splits = (origin: Origin): LoggedRequest[] =>+ lastBlock(origin).filter((r) => r.range!.start > LAST)+ const sent = (requests: LoggedRequest[]): number =>+ requests.reduce((sum, r) => sum + r.bytesSent, 0)++ test('the free streams share the last block, and nothing is fetched twice', async ({+ plexo,+ serve+ }) => {+ const origin = await serve({ size: SIZE, bytesPerSecond: RATE })+ await plexo.start(origin.url(), origin.sha256, { connections: 4 })+ await plexo.waitForHttpStatus('completed', 30_000)+ const done = Date.now()++ expect(splits(origin).length, 'a request began partway into the last block').toBeGreaterThan(0)+ // Measured from when the last block was first asked for, so a slow start doesn't count.+ expect(done - lastBlock(origin)[0].at, 'well under the 8 s one connection takes').toBeLessThan(+ 0.8 * (MIB / RATE) * 1000+ )+ // A holder cut short leaves a little of its response unread, and nothing more.+ expect(sent(origin.chunkRequests()), 'bytes served beyond the file').toBeLessThan(+ SIZE + 256 * 1024+ )+ })++ for (const fault of ['drop', 'stall'] as const) {+ test(`a split whose connection ${fault === 'drop' ? 'drops' : 'goes silent'} partway: the rest of its part is fetched from where it stopped, and the file is exact`, async ({+ plexo,+ serve+ }) => {+ const origin = await serve({ size: SIZE, bytesPerSecond: RATE })+ let first: OriginRequest['range'] = null+ origin.setRule(({ range }) => {+ if (first || !range || range.start <= LAST) return undefined+ first = range+ return fault === 'drop' ? { cutAfter: 64 * 1024 } : 'stallBody'+ })+ await plexo.start(origin.url(), origin.sha256, { connections: 4 })+ await plexo.waitForHttpStatus('completed', 30_000)++ const resumedAt = first!.start + (fault === 'drop' ? 64 * 1024 : 0)+ expect(+ splits(origin).filter((r) => r.range!.start === resumedAt).length,+ 'what was left of the split was asked for from where it stopped'+ ).toBeGreaterThanOrEqual(fault === 'drop' ? 1 : 2)+ })+ }++ for (const stop of ['pause', 'kill'] as const) {+ test(`${stop === 'pause' ? 'paused' : 'killed'} while split, then resumed: what the split fetched ahead is fetched again, and the file is exact`, async ({+ plexo,+ serve+ }) => {+ const origin = await serve({ size: SIZE, bytesPerSecond: RATE })+ const id = await plexo.start(origin.url(), origin.sha256, { connections: 4 })+ await expect+ .poll(() => splits(origin).length, { message: 'a split started', timeout: 20_000 })+ .toBeGreaterThan(0)+ // Let it write some of its part.+ await new Promise((resolve) => setTimeout(resolve, 800))++ if (stop === 'pause') {+ await plexo.api.pauseDownload(id)+ const paused = await plexo.waitForHttpStatus('paused')+ expect(paused.streams.some((chunk) => chunk.hedge || chunk.split)).toBe(false)+ // Only what joins the block's first byte counts: a split's part ahead of it doesn't.+ const last = paused.blocks!.at(-1)!+ expect(last.status).not.toBe('completed')+ expect(last.bytesDownloaded).toBeLessThan(splits(origin)[0].range!.start - LAST)+ } else {+ await plexo.kill()+ await plexo.launch()+ expect((await plexo.currentHttp())?.status).toBe('paused')+ }++ await plexo.api.resumeDownload(id)+ await plexo.waitForHttpStatus('completed', 30_000)+ })+ }+})diff --git a/e2e/scheduler.spec.ts b/e2e/scheduler.spec.tsindex 1bf445d..6a14ec2 100644--- a/e2e/scheduler.spec.ts+++ b/e2e/scheduler.spec.ts@@ -1,6 +1,7 @@import { expect, test } from '@playwright/test'import fc from 'fast-check'import {+ partEnd,pickWork,type AttemptView,type SchedulerPolicy,@@ -373,3 +374,217 @@ test('no racing while the disk is behind, and racing resumes once it keeps up',expect(pick({ ...s, diskBehind: true }, 1, 'b')).toBeUndefined()expect(pick(s, 1, 'b')).toBe('hedge:0')})++test.describe('scheduler: splitting what is left', () => {+ // Parts of 1000-byte blocks, halved down to 100 bytes a side.+ const SPLIT: SchedulerPolicy = { ...POLICY, minSplitBytes: 100 }+ const part = (+ streamId: number,+ networkId: string,+ startOffset: number,+ end: number,+ received: number,+ kind: AttemptView['kind'] = 'primary'+ ): AttemptView => ({+ ...attempt(streamId, networkId, kind),+ startOffset,+ end,+ networkReceived: received+ })+ const split = (s: SchedulerState, id = 9, networkId = 'a'): string | undefined => {+ const work = pickWork(s, { id, networkId, speedBytesPerSec: 100 }, NOW, SPLIT)+ return work && `${work.kind}:${work.block.index}@${work.start ?? ''}-${work.end ?? ''}`+ }+ // Holders as fast as anyone free, as from a server that caps each connection: racing them+ // wouldn't pay.+ const capped = (+ attempts: [number, AttemptView[]][],+ blocks = [block(0, 'downloading'), block(1, 'downloading')],+ over: Partial<SchedulerState> = {}+ ): SchedulerState => ({+ ...state({+ blocks,+ streams: [+ stream(0, 'a', 'downloading', 100),+ stream(1, 'a', 'downloading', 100),+ stream(2, 'a', 'downloading', 100),+ stream(9, 'a', 'pending', 100)+ ],+ attempts+ }),+ ...over+ })++ test('a free stream takes the back half of the part with the most bytes left', () => {+ const s = capped([+ [0, [part(0, 'a', 0, 1000, 600)]],+ [1, [part(1, 'a', 0, 1000, 200)]]+ ])+ // Block 1 has 800 left, from byte 200: the free stream takes 600–1000.+ expect(split(s)).toBe('split:1@600-1000')+ })++ test('a part split off before can be split again', () => {+ const s = capped([[0, [part(0, 'a', 0, 500, 400), part(1, 'a', 500, 1000, 50, 'split')]]])+ expect(split(s)).toBe('split:0@775-1000')+ })++ test('no speeds come into it: a holder far faster than the free stream is still split', () => {+ const s = capped([[0, [part(0, 'a', 0, 1000, 0)]]])+ const fast = { ...s, streams: [stream(0, 'a', 'downloading', 1e9), stream(9, 'a', 'pending')] }+ expect(split(fast)).toBe('split:0@500-1000')+ })++ test('nothing is split smaller than the minimum on either side', () => {+ expect(split(capped([[0, [part(0, 'a', 0, 1000, 800)]]]))).toBe('split:0@900-1000')+ expect(split(capped([[0, [part(0, 'a', 0, 1000, 801)]]]))).toBeUndefined()+ })++ test('never while a block is still waiting: that comes first', () => {+ const s = capped([[0, [part(0, 'a', 0, 1000, 0)]]], [block(0, 'downloading'), block(1)])+ expect(split(s)).toBe('primary:1@-')+ })++ test('not while the disk is behind', () => {+ const s = capped([[0, [part(0, 'a', 0, 1000, 0)]]], undefined, { diskBehind: true })+ expect(split(s)).toBeUndefined()+ })++ test('not a block of unknown length', () => {+ const open = { ...block(0, 'downloading'), rangeEnd: null }+ expect(split(capped([[0, [part(0, 'a', 0, 1000, 0)]]], [open]))).toBeUndefined()+ })++ test('not a part being raced, nor one on its way out', () => {+ const raced = capped([+ [0, [part(0, 'a', 0, 1000, 0), part(1, 'a', 0, 1000, 0, 'hedge')]],+ [1, [{ ...part(2, 'a', 0, 1000, 0), abortReason: 'refresh' }]]+ ])+ expect(split(raced)).toBeUndefined()+ })++ test('not by a network that got nothing from the block', () => {+ const s = capped([[0, [part(0, 'a', 0, 1000, 0)]]], undefined, {+ avoid: new Map([[0, 'a']])+ })+ expect(split(s)).toBeUndefined()+ })++ test('racing still comes first when it would clearly pay', () => {+ const s = capped([[0, [part(0, 'a', 0, 1000, 0)]]])+ const crawling = {+ ...s,+ streams: [stream(0, 'a', 'downloading', 1), stream(9, 'a', 'pending')]+ }+ expect(+ pickWork(crawling, { id: 9, networkId: 'a', speedBytesPerSec: 100 }, NOW, SPLIT)?.kind+ ).toBe('hedge')+ })++ test('a race is over what is left of the part from the frontier, not the whole block', () => {+ // 100 bytes left before the split at 500: not worth racing at 10 B/s against a 5 s bar.+ const s = capped(+ [[0, [part(0, 'a', 0, 500, 400), part(1, 'a', 500, 1000, 0, 'split')]]],+ [block(0, 'downloading', 400)]+ )+ const slow = { ...s, streams: [stream(0, 'a', 'downloading', 30), stream(9, 'a', 'pending')] }+ expect(pickWork(slow, { id: 9, networkId: 'a' }, NOW, POLICY)).toBeUndefined()+ })++ test('what a split left unfetched when it stopped is taken whole, before anything else', () => {+ // A split of 500–1000 stopped at 700: 700–1000 is nobody's.+ const s = capped([[0, [part(0, 'a', 0, 500, 100)]]], [block(0, 'downloading', 100)], {+ segments: new Map([[0, [{ start: 500, end: 700 }]]]),+ diskBehind: true+ })+ expect(split(s)).toBe('split:0@700-1000')+ })++ test('a gap is only looked for behind someone fetching from the frontier', () => {+ const s = capped([[0, [part(1, 'a', 500, 1000, 0, 'split')]]], [block(0, 'downloading', 100)])+ expect(split(s)).not.toMatch(/@100-/)+ })++ test('partEnd stops a part where the next one begins', () => {+ const b = block(0, 'downloading', 100)+ const attempts = [part(1, 'a', 600, 1000, 0, 'split')]+ expect(partEnd(b, attempts, [{ start: 300, end: 400 }], 100)).toBe(300)+ expect(partEnd(b, attempts, [], 100)).toBe(600)+ expect(partEnd(b, [], [], 100)).toBe(1000)+ expect(partEnd({ ...b, rangeEnd: null }, [], [], 0)).toBeNull()+ })++ test('whatever is handed out is a stretch nobody else is fetching or has', () => {+ // Blocks cut into parts at random: each part is held, raced, or already in as a segment.+ fc.assert(+ fc.property(+ fc.array(+ fc.record({+ frontier: fc.integer({ min: 0, max: 400 }),+ cuts: fc.uniqueArray(fc.integer({ min: 401, max: 999 }), { maxLength: 4 }),+ fates: fc.array(fc.constantFrom('held', 'segment', 'gap'), {+ minLength: 5,+ maxLength: 5+ }),+ received: fc.array(fc.integer({ min: 0, max: 600 }), { minLength: 5, maxLength: 5 }),+ raced: fc.boolean()+ }),+ { minLength: 1, maxLength: 3 }+ ),+ (rows) => {+ const blocks = rows.map((r, i) => block(i, 'downloading', r.frontier))+ const attempts: [number, AttemptView[]][] = []+ const segments = new Map<number, { start: number; end: number }[]>()+ let id = 0+ rows.forEach((r, i) => {+ const bounds = [r.frontier, ...[...r.cuts].sort((a, b) => a - b), LENGTH]+ const held: AttemptView[] = []+ const done: { start: number; end: number }[] = []+ for (let k = 0; k + 1 < bounds.length; k++) {+ const [start, end] = [bounds[k], bounds[k + 1]]+ const fate = k === 0 ? 'held' : r.fates[k]+ const got = Math.min(r.received[k], end - start)+ if (fate === 'held') {+ held.push(part(id++, 'a', start, end, got, k === 0 ? 'primary' : 'split'))+ if (k === 0 && r.raced) held.push(part(id++, 'a', start, end, got, 'hedge'))+ } else if (fate === 'segment' && got > 0) {+ done.push({ start, end: start + got })+ }+ }+ attempts.push([i, held])+ segments.set(i, done)+ })+ const s: SchedulerState = {+ ...state({ blocks, streams: [stream(99, 'a', 'pending')], attempts }),+ segments+ }+ const work = pickWork(s, { id: 99, networkId: 'a' }, NOW, SPLIT)+ if (!work || work.kind !== 'split') return+ const start = work.start!+ const end = work.end!+ expect(start).toBeGreaterThan(work.block.bytesDownloaded)+ expect(end).toBeGreaterThan(start)+ expect(end).toBeLessThanOrEqual(LENGTH)+ // Nothing already in overlaps it.+ for (const segment of segments.get(work.block.index) ?? []) {+ expect(segment.end <= start || segment.start >= end).toBe(true)+ }+ // Whoever is fetching across its start has yet to reach it, by at least the minimum,+ // and isn't being raced; nobody else is fetching any of it.+ for (const other of s.attempts.get(work.block.index) ?? []) {+ const from = other.startOffset!+ const to = other.end!+ if (from < start && to > start) {+ expect(from + other.networkReceived! + 100).toBeLessThanOrEqual(start)+ expect(end - start).toBeGreaterThanOrEqual(100)+ expect(to).toBe(end)+ expect(other.kind === 'split' || !rows[work.block.index].raced).toBe(true)+ } else {+ expect(to <= start || from >= end).toBe(true)+ }+ }+ }+ )+ )+ })+})diff --git a/src/main/download/chunkDownloader.ts b/src/main/download/chunkDownloader.tsindex c60a581..2fe78a3 100644--- a/src/main/download/chunkDownloader.ts+++ b/src/main/download/chunkDownloader.ts@@ -33,6 +33,11 @@ export interface ChunkDownloadOptions {onResponse?: (info: { ttfbMs: number; reusedSocket: boolean }) => void/** Told of every body byte received; answers how many ms to stop reading for (see Limits). */throttle?: (bytes: number) => number+ /** How many bytes of the range are still wanted, from rangeStart: it can drop while the+ * request runs, when another stream takes over the rest of the range (see scheduler.ts). Once+ * that many are in, the rest of the body is left unread and the request ends as if the range+ * had ended there. */+ wanted?: () => number}/** A response whose version doesn't match the download's. Nothing from it was written; the@@ -152,7 +157,8 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {acceptedVersions,onResponse,throttle,- onWriteWait+ onWriteWait,+ wanted} = optionsreturn new Promise((resolve, reject) => {@@ -234,8 +240,12 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {return}currentReq = req- // The connection dropping mid-answer; one the server answered wrongly fails below.- const dropped = (error: Error): void => fail(asConnectionError(error))+ // The connection dropping mid-answer; one the server answered wrongly fails below. Once+ // the body has ended — or been cut short on purpose — only the writer can still fail.+ let bodyEnded = false+ const dropped = (error: Error): void => {+ if (!bodyEnded) fail(asConnectionError(error))+ }req.on('error', dropped)const status = res.statusCode ?? 0@@ -316,7 +326,6 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {// once neither holds it. Neither says anything about the network: the watchdog stops// until then.let holds = 0- let bodyEnded = falseconst hold = (): void => {if (holds++ === 0) {clearWatchdog()@@ -348,16 +357,30 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {else coalesceTimer = setTimeout(() => flushWrites(), WRITE_COALESCE_MS)}+ const endBody = (): void => {+ bodyEnded = true+ clearWatchdog()+ flushWrites()+ // Held until its last bytes are written; closing the file isn't the disk falling+ // behind.+ onWriteWait?.(true)+ fileStream.once('finish', () => onWriteWait?.(false))+ // Windows cannot reliably reopen/remove a file until its handle closes.+ fileStream.once('close', () => finish(resolve))+ fileStream.end()+ }+// Written by hand rather than piped so an overlong body can be cut off// at the range boundary: an overlong response could overwrite the next block.res.on('data', (chunk: Buffer) => {- if (settled) return+ if (settled || bodyEnded) returnresetWatchdog()const remaining =expectedBytes === null ? chunk.length : expectedBytes - bytesDownloaded- const usable =- chunk.length <= remaining ? chunk : chunk.subarray(0, Math.max(remaining, 0))+ // Bytes past what is still wanted aren't an overlong body: they are left unread.+ const keep = Math.min(remaining, (wanted?.() ?? Infinity) - bytesDownloaded)+ const usable = chunk.length <= keep ? chunk : chunk.subarray(0, Math.max(keep, 0))if (usable.length > 0) {bytesDownloaded += usable.length@@ -389,13 +412,20 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {}}- if (usable.length < chunk.length) {+ const rangeLeft = expectedBytes === null ? Infinity : expectedBytes - bytesDownloaded+ if (wanted && rangeLeft > 0 && bytesDownloaded >= wanted()) {+ // The rest of the range is another stream's now. The socket still has it coming,+ // so it can't carry another request: it goes, and the writer finishes as at the+ // end of a body.+ endBody()+ currentReq?.destroy()+ } else if (usable.length < chunk.length) {fail(new Error('Server sent more data than the requested range'))}})res.on('end', () => {- if (settled) return+ if (settled || bodyEnded) returnclearWatchdog()if (expectedBytes !== null && bytesDownloaded !== expectedBytes) {fail(@@ -405,15 +435,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {)return}- bodyEnded = true- flushWrites()- // Held until its last bytes are written; closing the file isn't the disk falling- // behind.- onWriteWait?.(true)- fileStream.once('finish', () => onWriteWait?.(false))- // Windows cannot reliably reopen/remove a file until its handle closes.- fileStream.once('close', () => finish(resolve))- fileStream.end()+ endBody()})}).catch(fail)diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 4d3b1c3..b2b4456 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -1091,6 +1091,7 @@ export class DownloadManager {}stream.currentBlockIndex = undefinedstream.hedge = undefined+ stream.split = undefined}}for (const unit of unitsOf(runtime)) {diff --git a/src/main/download/httpTransfer.ts b/src/main/download/httpTransfer.tsindex 3cc1697..b6b1bc7 100644--- a/src/main/download/httpTransfer.ts+++ b/src/main/download/httpTransfer.ts@@ -20,7 +20,7 @@ import {import { downloadChunk, fetchRange, HttpStatusError, RemoteChangedError } from './chunkDownloader'import { compareVersion, type FileVersion } from './fileVersion'import { interleave, MAX_STREAMS_PER_NETWORK, startingStreams } from './plan'-import { pickWork, type SchedulerPolicy, type Work } from './scheduler'+import { partEnd, pickWork, type SchedulerPolicy, type Segment, type Work } from './scheduler'import {delay,recomputeAggregates,@@ -36,19 +36,23 @@ type AbortReason = 'refresh' | 'lost'/*** One request for one block, by one stream. A block normally has a single (primary) attempt.* Near the end of a download a second (hedge) one may race it — see scheduler.ts — and then the- * block is finished by whichever gets there first.+ * block is finished by whichever gets there first. Or the block may be split: a split attempt+ * fetches the back part of what another has left, and that one stops where the split begins.** Both write straight into the staging file at the block's own offsets. Every response is checked* to be the download's version before any of it is written, so racing attempts write identical* bytes and it doesn't matter which lands last: whatever either has written stays secured.*/interface Attempt {- kind: 'primary' | 'hedge'+ kind: 'primary' | 'hedge' | 'split'block: HttpBlockStatestreamId: numbernetworkId: string/** Where the request begins, as an offset into the block. Null until it is known. */startOffset: number | null+ /** Where it stops, as an offset into the block, exclusive; null for a block of unknown length.+ * Brought forward while it runs when a split takes over the rest (see scheduler.ts). */+ end: number | null/** Bytes the destination writer has accepted; safe to resume from this prefix. */received: number/** Bytes the network has delivered, independently of disk backpressure. */@@ -155,7 +159,14 @@ const MAX_REFRESHES_PER_BLOCK = 2const SCHEDULER_POLICY: SchedulerPolicy = {hedgeAfterMs: testKnobs.hedgeAfterMs,maxHedgesPerBlock: 2,- startupMs: 1000+ startupMs: 1000,+ minSplitBytes: 256 * 1024+}++/** What a split fetched ahead of its block's frontier before it ended, and who fetched it. */+interface AheadSegment extends Segment {+ networkId: string+ streamId: number}/** Whether the file can be fetched in parts. Otherwise one request has to carry all of it: one@@ -299,6 +310,10 @@ export class HttpTransfer implements Transfer {private attempts = new Map<number, Attempt[]>()/** Hedges started per block index, capped at SCHEDULER_POLICY.maxHedgesPerBlock. */private hedgesByBlock = new Map<number, number>()+ /** By block index, what splits that have ended fetched ahead of the block's frontier. Counted+ * once the frontier reaches them (see join), so never saved as progress: after a pause or a+ * crash they are simply fetched again. Forgotten on reset. */+ private aheadByBlock = new Map<number, AheadSegment[]>()/** By network id: everything its streams have received, and when it last showed it was alive —* received something, or came into use. */private traffic = new Map<string, { received: number; aliveAt: number }>()@@ -418,6 +433,7 @@ export class HttpTransfer implements Transfer {reset(): void {this.avoidNetworkByBlock.clear()+ this.aheadByBlock.clear()this.reconciled.clear()this.disk.reset()}@@ -617,7 +633,9 @@ export class HttpTransfer implements Transfer {private otherAttemptsPosition(attempt: Attempt): number {let furthest = 0for (const other of this.attempts.get(attempt.block.index) ?? []) {- if (other !== attempt) furthest = Math.max(furthest, this.attemptPosition(other))+ // A split ahead of the frontier secures nothing until the frontier reaches it.+ if (other === attempt || (other.startOffset ?? 0) > attempt.block.bytesDownloaded) continue+ furthest = Math.max(furthest, this.attemptPosition(other))}return furthest}@@ -627,17 +645,29 @@ export class HttpTransfer implements Transfer {chunk.status = 'pending'chunk.currentBlockIndex = undefinedchunk.hedge = undefined+ chunk.split = undefinedchunk.speedBytesPerSec = 0}private beginAttempt(chunk: HttpStreamState, self: ChunkRuntime, work: Work): Attempt {const { block } = work+ const inFlight = this.attempts.get(block.index) ?? []+ const segments = this.aheadByBlock.get(block.index) ?? []+ // A primary or a hedge fetches from the frontier — from byte 0 if the server can't serve+ // ranges — up to where a split's part begins.+ const startOffset =+ work.kind === 'split'+ ? work.start!+ : work.kind === 'hedge' || this.runtime.requestPayload.supportsRanges+ ? block.bytesDownloaded+ : 0const attempt: Attempt = {kind: work.kind,block,streamId: chunk.id,networkId: chunk.interfaceId,- startOffset: null,+ startOffset,+ end: work.kind === 'split' ? work.end! : partEnd(block, inFlight, segments, startOffset),received: 0,networkReceived: 0,lastNetworkAt: 0,@@ -655,16 +685,23 @@ export class HttpTransfer implements Transfer {block.status = 'downloading'block.interfaceId = chunk.interfaceIdthis.avoidNetworkByBlock.delete(block.index)- } else {+ } else if (work.kind === 'hedge') {this.hedgesByBlock.set(block.index, (this.hedgesByBlock.get(block.index) ?? 0) + 1)+ } else {+ // Whoever is fetching across where the split begins stops there. Its response has yet to+ // reach that point (see nextSplit), and nothing can arrive between here and the cut.+ for (const other of inFlight) {+ if (other.end !== null && (other.startOffset ?? 0) < startOffset && other.end > startOffset)+ other.end = startOffset+ }}- const inFlight = this.attempts.get(block.index)- if (inFlight) inFlight.push(attempt)+ if (inFlight.length > 0) inFlight.push(attempt)else this.attempts.set(block.index, [attempt])self.attempt = attemptchunk.status = 'downloading'chunk.hedge = work.kind === 'hedge' ? true : undefined+ chunk.split = work.kind === 'split' ? true : undefinedchunk.rangeStart = block.rangeStartchunk.rangeEnd = block.rangeEndchunk.currentBlockIndex = block.index@@ -690,22 +727,19 @@ export class HttpTransfer implements Transfer {): Promise<AttemptOutcome> {const { block } = attempttry {+ // A failed range leaves its written prefix in the staging file, and the attempt starts+ // after it (see beginAttempt). Without range support, the server can only restart from+ // byte zero: what another attempt has already secured stays counted.+ const startOffset = attempt.startOffset ?? 0if (attempt.kind === 'primary') {- // A failed range leaves its written prefix in the staging file. Without range support,- // the server can only restart from byte zero.- attempt.startOffset = this.runtime.requestPayload.supportsRanges ? block.bytesDownloaded : 0- // What another attempt has already secured stays counted.- const keep = Math.max(attempt.startOffset, this.otherAttemptsPosition(attempt))+ const keep = Math.max(startOffset, this.otherAttemptsPosition(attempt))if (retractBlock(block, keep, attempt.previousWriter) > 0) {recomputeAggregates(this.runtime.state, this.runtime.blocks)}- } else {- attempt.startOffset = block.bytesDownloaded}- const length = block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1- if (length !== null && attempt.startOffset >= length) {- // The staging file already holds the whole block.+ if (attempt.end !== null && startOffset >= attempt.end) {+ // The staging file already holds its whole part.return attempt.kind === 'primary'? { type: 'completed' }: { type: 'aborted', reason: 'lost' }@@ -715,12 +749,13 @@ export class HttpTransfer implements Transfer {await downloadChunk({url: this.runtime.requestPayload.url,browser: this.runtime.requestPayload.browser,- rangeStart: block.rangeStart + attempt.startOffset,- rangeEnd: block.rangeEnd,+ rangeStart: block.rangeStart + startOffset,+ rangeEnd: attempt.end === null ? null : block.rangeStart + attempt.end - 1,+ // Its end can come forward while it runs (see beginAttempt).+ wanted: attempt.end === null ? undefined : () => attempt.end! - startOffset,wholeFile: !this.runtime.requestPayload.supportsRanges,connection: self.connection,- createDestination: () =>- this.runtime.file.writer(block.rangeStart + (attempt.startOffset ?? 0)),+ createDestination: () => this.runtime.file.writer(block.rangeStart + startOffset),signal: AbortSignal.any([self.controller.signal, attempt.abort.signal]),acceptedVersions: this.acceptedVersions,onResponse: (info) => (attempt.response = info),@@ -740,6 +775,7 @@ export class HttpTransfer implements Transfer {attempt.received = bytesThisRunthis.disk.landed(Date.now())this.onAttemptProgress(chunk, attempt)+ this.host.scheduleUpdate()}}})@@ -771,18 +807,130 @@ export class HttpTransfer implements Transfer {}private onAttemptProgress(chunk: HttpStreamState, attempt: Attempt): void {+ // A split's bytes count once the block's frontier reaches where it started.+ if ((attempt.startOffset ?? 0) > attempt.block.bytesDownloaded) return+ this.advance(attempt.block, attempt.networkId, this.attemptPosition(attempt), chunk)+ }++ /** Moves the block's frontier forward to `position`, crediting what it gains to the network+ * and stream that fetched it, then over whatever splits have fetched from there on. */+ private advance(+ block: HttpBlockState,+ networkId: string,+ position: number,+ stream: HttpStreamState | undefined+ ): void {+ if (this.credit(block, networkId, position, stream) === 0) return+ if (this.aheadByBlock.has(block.index) || (this.attempts.get(block.index)?.length ?? 0) > 1) {+ this.join(block)+ }+ }++ /** Moves the block's frontier forward to `position` and counts what it gains. */+ private credit(+ block: HttpBlockState,+ networkId: string,+ position: number,+ stream: HttpStreamState | undefined+ ): number {// Only what gets the block further than it already was counts as progress: a racing attempt// re-fetches bytes the other already has.- const gained = advanceBlock(attempt.block, attempt.networkId, this.attemptPosition(attempt))- chunk.bytesDownloaded += gained- this.network(attempt.networkId).bytesDownloaded += gained-+ const gained = advanceBlock(block, networkId, position)+ if (gained === 0) return 0+ if (stream) stream.bytesDownloaded += gained+ this.network(networkId).bytesDownloaded += gained// This runs on every completed writer callback, so it folds the delta in rather than re-summing// every block — that sum is O(blocks), and a large file has thousands of them. The other// callers of recomputeAggregates are rare enough to afford the full pass, and each one// re-derives the true total, so any drift here cannot accumulate.this.runtime.state.bytesDownloaded += gained- this.host.scheduleUpdate()+ return gained+ }++ /** Moves the block's frontier over what splits have fetched from where it now is: the parts+ * of attempts it has reached, and the segments of ones that have ended. */+ private join(block: HttpBlockState): void {+ const streamOf = (id: number): HttpStreamState | undefined =>+ this.runtime.state.streams.find((stream) => stream.id === id)+ for (let moved = true; moved;) {+ moved = false+ const before = block.bytesDownloaded+ for (const other of this.attempts.get(block.index) ?? []) {+ if ((other.startOffset ?? 0) > block.bytesDownloaded) continue+ if (this.attemptPosition(other) <= block.bytesDownloaded) continue+ this.credit(block, other.networkId, this.attemptPosition(other), streamOf(other.streamId))+ }+ const segments = this.aheadByBlock.get(block.index) ?? []+ for (const segment of segments) {+ if (segment.start > block.bytesDownloaded || segment.end <= block.bytesDownloaded) continue+ this.credit(block, segment.networkId, segment.end, streamOf(segment.streamId))+ }+ const left = segments.filter((segment) => segment.end > block.bytesDownloaded)+ if (left.length > 0) this.aheadByBlock.set(block.index, left)+ else this.aheadByBlock.delete(block.index)+ moved = block.bytesDownloaded > before+ }+ }++ /** Remembers what a split fetched ahead of its block's frontier, up to `end`. */+ private keepAhead(attempt: Attempt, end: number): void {+ const { block } = attempt+ const start = attempt.startOffset+ if (start === null || start <= block.bytesDownloaded || end <= start) return+ if (block.status === 'completed') return+ const segments = this.aheadByBlock.get(block.index) ?? []+ segments.push({ start, end, networkId: attempt.networkId, streamId: attempt.streamId })+ this.aheadByBlock.set(block.index, segments)+ }++ /** Squares the block up after one of its attempts ended. A block whose frontier has reached its+ * end is complete, and whoever is still fetching it has lost; so has whoever is fetching a part+ * the frontier has passed. A block left with no one fetching from its frontier hands that on to+ * the split that begins there, if there is one, or goes back to the queue. */+ private settle(block: HttpBlockState, networkId: string): void {+ if (block.status === 'completed') return+ if (block.rangeEnd !== null && block.bytesDownloaded >= block.rangeEnd - block.rangeStart + 1) {+ block.status = 'completed'+ block.interfaceId = networkId+ this.aheadByBlock.delete(block.index)+ this.loseRivals(block)+ return+ }+ this.loseRivals(block, (rival) => rival.end !== null && rival.end <= block.bytesDownloaded)+ // One called off for another reason still holds its part until it has let go, and then+ // settles the block itself.+ const holding = (this.attempts.get(block.index) ?? []).filter(+ (attempt) => attempt.abortReason !== 'lost'+ )+ if (holding.some((attempt) => attempt.kind === 'primary')) return+ const next = holding.find(+ (attempt) =>+ attempt.kind === 'split' &&+ !attempt.abortReason &&+ (attempt.startOffset ?? 0) <= block.bytesDownloaded+ )+ if (next) {+ next.kind = 'primary'+ block.status = 'downloading'+ block.interfaceId = next.networkId+ const stream = this.runtime.state.streams.find((chunk) => chunk.id === next.streamId)+ if (stream) stream.split = undefined+ } else if (block.status === 'downloading') {+ block.status = 'pending'+ }+ }++ /** Stops the attempts still fetching a block that `lost` picks — by default, all of them:+ * the block is done. Their streams show as free at once: their requests only end once the+ * abort reaches them, and until then they would show holding a block that is no longer in+ * flight. */+ private loseRivals(block: HttpBlockState, lost: (rival: Attempt) => boolean = () => true): void {+ for (const rival of this.attempts.get(block.index) ?? []) {+ if (!lost(rival)) continue+ this.abortAttempt(rival, 'lost')+ const stream = this.runtime.state.streams.find((chunk) => chunk.id === rival.streamId)+ if (stream) this.goIdle(stream)+ }}/** Applies what became of an attempt to the download. 'stop' ends the stream. */@@ -830,22 +978,35 @@ export class HttpTransfer implements Transfer {}this.endAttempt(self, attempt)- block.status = 'completed'- block.interfaceId = attempt.networkId- if (block.rangeEnd !== null) {- // Progress events can lag the final write, so square the block up to its exact size and- // credit the shortfall to the network that finished it.- const blockBytes = block.rangeEnd - block.rangeStart + 1- chunk.bytesDownloaded += advanceBlock(block, attempt.networkId, blockBytes)- self.lastBlockSpeed =- (attempt.networkReceived /- Math.max(1, (attempt.lastNetworkAt || Date.now()) - attempt.startedAt)) *- 1000- }self.failures = 0self.strikes = 0- // Whoever is still racing for the block has lost.- for (const rival of this.attempts.get(block.index) ?? []) this.abortAttempt(rival, 'lost')+ if (block.rangeEnd === null) {+ // A block of unknown length is the whole file, in one request: it is all in.+ block.status = 'completed'+ block.interfaceId = attempt.networkId+ this.loseRivals(block)+ recomputeAggregates(this.runtime.state, this.runtime.blocks)+ this.host.scheduleUpdate()+ return 'continue'+ }++ self.lastBlockSpeed =+ (attempt.networkReceived /+ Math.max(1, (attempt.lastNetworkAt || Date.now()) - attempt.startedAt)) *+ 1000+ const end = attempt.end ?? block.rangeEnd - block.rangeStart + 1+ if ((attempt.startOffset ?? 0) > block.bytesDownloaded) {+ // A split that got to the end of its part before the frontier got to it: its part waits+ // there to be joined, and the stream is free for more.+ this.keepAhead(attempt, end)+ } else {+ // Progress events can lag the final write, so square the block up to the end of this+ // attempt's part and credit the shortfall to the network that fetched it.+ this.advance(block, attempt.networkId, end, chunk)+ }+ this.settle(block, attempt.networkId)+ // Its part is in, but the block may not be: it no longer holds it either way.+ if ((block.status as HttpBlockState['status']) !== 'completed') this.goIdle(chunk)recomputeAggregates(this.runtime.state, this.runtime.blocks)this.host.scheduleUpdate()return 'continue'@@ -1012,8 +1173,14 @@ export class HttpTransfer implements Transfer {): void {this.endAttempt(self, attempt)const { block } = attempt- // A block another attempt has finished stays finished.- if (attempt.kind === 'primary' && block.status === 'downloading') block.status = 'pending'+ // What a split got before it stopped stays usable.+ this.keepAhead(attempt, this.attemptPosition(attempt))+ if ((this.runtime.state.status as DownloadStatus) === 'downloading') {+ this.settle(block, attempt.networkId)+ } else if (attempt.kind === 'primary' && block.status === 'downloading') {+ // Stopping: every block in flight goes back to the queue anyway.+ block.status = 'pending'+ }if (deliveredNothing) this.avoidNetworkByBlock.set(block.index, attempt.networkId)this.goIdle(chunk)}@@ -1143,7 +1310,8 @@ export class HttpTransfer implements Transfer {attempts: this.attempts,avoid: this.avoidNetworkByBlock,hedgesUsed: this.hedgesByBlock,- diskBehind: this.disk.reading === 'behind'+ diskBehind: this.disk.reading === 'behind',+ segments: this.aheadByBlock},{id: chunk.id,diff --git a/src/main/download/plan.ts b/src/main/download/plan.tsindex 6787f74..547e51c 100644--- a/src/main/download/plan.ts+++ b/src/main/download/plan.ts@@ -8,8 +8,9 @@ import type { HttpBlockState, TorrentPieceState } from '../../shared/types'const MIB = 1024 * 1024/** The largest block. However big the file, a block stays small enough for one stream to finish- * soon: a racing second attempt re-fetches a block rather than splitting it (see scheduler.ts),- * so the last block's size is how long the slowest connection can hold the download up. */+ * soon: a racing second attempt re-fetches what is left of a block rather than splitting it (see+ * scheduler.ts), so the last block's size is how long the slowest connection can hold the+ * download up. Splits share out only what free streams can take over at the very end. */export const DEFAULT_MAX_BLOCK_BYTES = 8 * MIB/** No block is planned smaller than this: each block is its own request, and below about adiff --git a/src/main/download/scheduler.ts b/src/main/download/scheduler.tsindex fda2924..492843b 100644--- a/src/main/download/scheduler.ts+++ b/src/main/download/scheduler.ts@@ -3,7 +3,7 @@ import type { HttpBlockState, HttpStreamState } from '../../shared/types'// Who should fetch what, decided from a snapshot of the download and nothing else — no I/O, no// clock of its own — so every rule here can be checked against exact situations.//-// A free stream is given one of two kinds of work:+// A free stream is given one of three kinds of work://// primary A block nobody is fetching. The normal case, and the only one until the queue runs dry.// hedge A second attempt at a block someone else is fetching too slowly. Only handed out once@@ -12,9 +12,20 @@ import type { HttpBlockState, HttpStreamState } from '../../shared/types'// that one slow connection can no longer hold the whole download back. Whichever attempt// finishes first wins; the others are dropped. It is handed out as soon as it would// clearly finish first, and a block whose hedge is stuck too can be raced again.+// split The back half of what an attempt has left, once no block is waiting and racing+// wouldn't pay. The attempt stops where the split begins, so nothing is fetched twice,+// and the free streams at the end of a download share its last blocks rather than+// watching one connection each crawl through them. No speeds come into it: the part+// with the most bytes left is halved, for as long as both halves stay over+// minSplitBytes.+//+// Splits live only in memory, as parts of a block. A block's progress is still its frontier: the+// bytes from its first one that are in (see blockProgress.ts). A split's bytes count once the+// frontier reaches them, so saved progress is unchanged, and a pause or a crash only means+// fetching them again.export interface AttemptView {- kind: 'primary' | 'hedge'+ kind: 'primary' | 'hedge' | 'split'streamId: numbernetworkId: string/** When its request was sent. */@@ -22,6 +33,21 @@ export interface AttemptView {/** The reader is paused for writes or its body ended and the writer is flushing: the disk, not* the connection, sets its pace. */writeWaiting?: boolean+ /** Its part of the block, as offsets into it: from startOffset up to end, exclusive. Null+ * when not known (a block of unknown length); without them, its part is never split. */+ startOffset?: number | null+ end?: number | null+ /** Bytes it has been sent from startOffset: past these, its part can still be cut short. */+ networkReceived?: number+ /** Called off and on its way out: its part is not split. */+ abortReason?: string | null+}++/** Bytes of a block fetched ahead of its frontier by a split that has finished or stopped, as+ * offsets into the block: [start, end). */+export interface Segment {+ start: number+ end: number}export interface SchedulerState {@@ -35,6 +61,8 @@ export interface SchedulerState {hedgesUsed: ReadonlyMap<number, number>/** The disk can't keep up (see DiskWatch in concurrency.ts): a hedge would only add writes. */diskBehind?: boolean+ /** By block index, what splits have fetched ahead of its frontier. */+ segments?: ReadonlyMap<number, readonly Segment[]>}export interface SchedulerPolicy {@@ -46,6 +74,9 @@ export interface SchedulerPolicy {/** What a new request costs before its first byte arrives (connecting, the server answering),* counted into how long a hedge would take. */startupMs: number+ /** The least either half of a split may be; absent, nothing is split. Each part is its own+ * request, so below this the round trip costs more than the split saves. */+ minSplitBytes?: number}export interface Requester {@@ -57,8 +88,58 @@ export interface Requester {}export interface Work {- kind: 'primary' | 'hedge'+ kind: 'primary' | 'hedge' | 'split'block: HttpBlockState+ /** For a split: its part of the block, as offsets into it, [start, end). Whoever is fetching+ * across `start` stops there. */+ start?: number+ end?: number+}++const lengthOf = (block: HttpBlockState): number | null =>+ block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1++/** Where an attempt starting `from` bytes into `block` stops: where the next part someone is+ * fetching, or has fetched, begins — or the end of the block. Null for a block of unknown length. */+export function partEnd(+ block: HttpBlockState,+ attempts: readonly AttemptView[],+ segments: readonly Segment[],+ from: number+): number | null {+ let end = lengthOf(block)+ if (end === null) return null+ for (const attempt of attempts) {+ if (attempt.startOffset != null && attempt.startOffset > from)+ end = Math.min(end, attempt.startOffset)+ }+ for (const segment of segments) if (segment.start > from) end = Math.min(end, segment.start)+ return end+}++/** The first stretch of `block`, past its frontier, that nobody is fetching and nobody has: what+ * a split left behind when it stopped early. */+function firstGap(+ block: HttpBlockState,+ attempts: readonly AttemptView[],+ segments: readonly Segment[]+): Segment | undefined {+ const length = lengthOf(block)+ if (length === null) return undefined+ const taken: Segment[] = [{ start: 0, end: block.bytesDownloaded }, ...segments]+ for (const attempt of attempts) {+ if (attempt.startOffset == null) return undefined+ taken.push({ start: attempt.startOffset, end: attempt.end ?? length })+ }+ taken.sort((a, b) => a.start - b.start)+ let reach = 0+ for (const part of taken) {+ if (part.start > reach) break+ reach = Math.max(reach, part.end)+ }+ if (reach >= length) return undefined+ const next = taken.find((part) => part.start > reach)+ return { start: reach, end: next ? next.start : length }}/** The first waiting block — except that one whose last attempt on this network delivered@@ -78,6 +159,63 @@ function nextWaitingBlock(state: SchedulerState, networkId: string): HttpBlockSt)}+/** A stretch of a block in flight that nobody is fetching, past its frontier. Taken like a+ * waiting block, and left to another network by one that got nothing from the block. */+function nextGap(state: SchedulerState, networkId: string): Work | undefined {+ const otherNetworkFree = state.streams.some(+ (stream) => stream.interfaceId !== networkId && stream.status === 'pending'+ )+ for (const [index, attempts] of state.attempts) {+ const block = state.blocks[index]+ if (block?.index !== index || block.status !== 'downloading') continue+ if (otherNetworkFree && state.avoid.get(index) === networkId) continue+ // Only behind someone fetching from the frontier: without one, the block goes back to the+ // queue and is taken from there.+ if (!attempts.some((attempt) => attempt.kind === 'primary')) continue+ const gap = firstGap(block, attempts, state.segments?.get(index) ?? [])+ if (gap && gap.start > block.bytesDownloaded) {+ return { kind: 'split', block, start: gap.start, end: gap.end }+ }+ }+ return undefined+}++/** The back half of the part with the most bytes left, if both halves are at least+ * minSplitBytes. Only once nothing is waiting, and while the disk keeps up. A part being raced+ * is left whole: both racers would have to be cut short. */+function nextSplit(+ state: SchedulerState,+ who: Requester,+ policy: SchedulerPolicy+): Work | undefined {+ const min = policy.minSplitBytes+ if (!min || state.diskBehind || state.blocks.some((block) => block.status === 'pending'))+ return undefined++ let target: Work | undefined+ let most = 2 * min - 1+ for (const [index, attempts] of state.attempts) {+ const block = state.blocks[index]+ if (block?.index !== index || block.rangeEnd === null || block.status !== 'downloading')+ continue+ // A network that got nothing from this block won't do better on a part of it.+ if (state.avoid.get(index) === who.networkId) continue+ const raced = attempts.some((attempt) => attempt.kind === 'hedge')+ for (const attempt of attempts) {+ if (attempt.abortReason || (raced && attempt.kind !== 'split')) continue+ const { startOffset, end, networkReceived } = attempt+ if (startOffset == null || end == null || networkReceived === undefined) continue+ const reached = startOffset + networkReceived+ const left = end - reached+ if (left > most) {+ most = left+ target = { kind: 'split', block, start: reached + Math.floor(left / 2), end }+ }+ }+ }+ return target+}+/** The block most worth another attempt: the one that will be longest yet, if the requester* would clearly beat it. A block is as slow as its fastest attempt. */function nextHedgeTarget(@@ -98,22 +236,26 @@ function nextHedgeTarget(const block = state.blocks[index]if (block?.index !== index || block.rangeEnd === null || block.status !== 'downloading')continue- // Nothing to race if every attempt let go: the block goes back to the queue instead.- if (attempts.length === 0 || !attempts.some((attempt) => attempt.kind === 'primary')) continueif (attempts.some((attempt) => attempt.streamId === who.id)) continue+ // What is raced is the part fetched from the block's frontier; any split ahead of it goes on+ // by itself.+ const racing = attempts.filter((attempt) => attempt.kind !== 'split')+ const primary = racing.find((attempt) => attempt.kind === 'primary')+ // Nothing to race if every attempt let go: the block goes back to the queue instead.+ if (!primary) continueif ((state.hedgesUsed.get(index) ?? 0) >= policy.maxHedgesPerBlock) continue// A network that got nothing from this block won't do better on a second try.if (state.avoid.get(index) === who.networkId) continue// Speeds only mean something once every attempt has been going a while.- if (attempts.some((attempt) => now - attempt.startedAt < policy.hedgeAfterMs)) continue+ if (racing.some((attempt) => now - attempt.startedAt < policy.hedgeAfterMs)) continue// A holder waiting on its writer is as slow as the disk, which another attempt can't beat.- if (attempts.some((attempt) => attempt.writeWaiting)) continue+ if (racing.some((attempt) => attempt.writeWaiting)) continue- const remaining = block.rangeEnd - block.rangeStart + 1 - block.bytesDownloaded+ const remaining = (primary.end ?? block.rangeEnd - block.rangeStart + 1) - block.bytesDownloadedif (remaining <= 0) continue// A holder that has gone quiet has no finish time at all.const eta = Math.min(- ...attempts.map((attempt) => {+ ...racing.map((attempt) => {const speed = speedOf(attempt.streamId)return speed ? (remaining / speed) * 1000 : Infinity})@@ -127,7 +269,7 @@ function nextHedgeTarget(// The holders' network may be what is slow, so a stream on another network gets the first// go — unless none of those would take it either.- const holders = new Set(attempts.map((attempt) => attempt.networkId))+ const holders = new Set(racing.map((attempt) => attempt.networkId))const otherNetworkFree = state.streams.some((stream) =>!holders.has(stream.interfaceId) &&@@ -152,6 +294,9 @@ export function pickWork(): Work | undefined {const waiting = nextWaitingBlock(state, who.networkId)if (waiting) return { kind: 'primary', block: waiting }+ const gap = nextGap(state, who.networkId)+ if (gap) return gapconst slow = nextHedgeTarget(state, who, now, policy)- return slow && { kind: 'hedge', block: slow }+ if (slow) return { kind: 'hedge', block: slow }+ return nextSplit(state, who, policy)}diff --git a/src/renderer/src/components/NetworkRow.tsx b/src/renderer/src/components/NetworkRow.tsxindex 502dd10..5d3a501 100644--- a/src/renderer/src/components/NetworkRow.tsx+++ b/src/renderer/src/components/NetworkRow.tsx@@ -145,7 +145,7 @@ function HttpStreamRows({</span>)}{/* A lit dot and a speed say it's fetching; only what isn't the usual gets a word. */}- {active && stream.hedge ? (+ {active && (stream.hedge || stream.split) ? (<Tooltip><TooltipTriggerrender={@@ -156,12 +156,14 @@ function HttpStreamRows({tabIndex={0}className="h-[13px] rounded-[3px] px-[5px] py-px text-[9px] font-semibold tracking-[0.04em] outline-none focus-visible:ring-2 focus-visible:ring-ring/50">- BACKUP+ {stream.hedge ? 'BACKUP' : 'SPLIT'}</ColorBadge>}/><TooltipContent>- Racing another stream for this chunk, which was running slowly+ {stream.hedge+ ? 'Racing another stream for this chunk, which was running slowly'+ : 'Fetching the far part of this chunk, so another stream doesn’t have to'}</TooltipContent></Tooltip>) : (diff --git a/src/shared/types.ts b/src/shared/types.tsindex 60580a2..d9b8117 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -101,6 +101,9 @@ export interface HttpStreamState {/** True while this stream is racing another stream for `currentBlockIndex`, because that one* was too slow — see main/download/scheduler.ts. Whichever finishes first wins. */hedge?: boolean+ /** True while this stream is fetching the back part of `currentBlockIndex`, split off from what+ * another stream had left of it — see main/download/scheduler.ts. Each fetches its own part. */+ split?: boolean}/** A live BitTorrent peer connection. A peer does not own a piece: it may contribute blocks to