Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Let Auto back off when the disk can't keep up (#89)

Anmol Kapile8b84b6fa5d1
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 1fdf4d1..1edf8af 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -91,3 +91,8 @@ jobs:
run: npx playwright test e2e/torrentNative.spec.ts
env:
PLEXO_E2E_SKIP_BUILD: '1'
+
+ - name: Check sparse staging files
+ run: npx playwright test e2e/sparseFile.spec.ts
+ env:
+ PLEXO_E2E_SKIP_BUILD: '1'
diff --git a/e2e/concurrency.spec.ts b/e2e/concurrency.spec.ts
index b4fc2c5..837aac0 100644
--- a/e2e/concurrency.spec.ts
+++ b/e2e/concurrency.spec.ts
@@ -1,6 +1,10 @@
import { expect, test } from '@playwright/test'
import {
ConcurrencyController,
+ DISK_PATIENCE_MS,
+ DISK_RECOVER_MS,
+ DISK_STALLED_MS,
+ DiskWatch,
RECOVER_MS,
type Action,
type NetworkSnapshot
@@ -214,4 +218,171 @@ test.describe('stream count', () => {
controller.tick({ now: 2 * RECOVER_MS, networks: [network('a', 4)], spareWork: 1000 })
).toEqual([])
})
+
+ test('the disk is judged by the clock: looks close together are not patience', () => {
+ const controller = new ConcurrencyController(MAX)
+ const tick = (now: number, streams: number): Action[] =>
+ controller.tick({
+ now,
+ networks: [network('a', streams)],
+ spareWork: 1000,
+ disk: { reading: 'behind', since: 0 }
+ })
+ // Not before it has lasted, and no growth meanwhile.
+ expect(tick(0, 16)).toEqual([])
+ expect(tick(1, 16)).toEqual([])
+ expect(tick(DISK_PATIENCE_MS - 1, 16)).toEqual([])
+ expect(tick(DISK_PATIENCE_MS, 16)).toEqual([{ kind: 'retire', networkId: 'a', count: 8 }])
+ // The next cut needs as long again at the new count.
+ expect(tick(DISK_PATIENCE_MS + 1, 8)).toEqual([])
+ expect(tick(2 * DISK_PATIENCE_MS, 8)).toEqual([{ kind: 'retire', networkId: 'a', count: 4 }])
+ })
+
+ test('a disk behind at every count is left with one stream, and keeps it', () => {
+ const counts = run(new ConcurrencyController(MAX), 32, 30_000, () => true)
+ expect(counts.indexOf(1) * LOOK_MS).toBeLessThanOrEqual(6 * DISK_PATIENCE_MS)
+ expect(counts.slice(counts.indexOf(1))).toEqual(counts.slice(counts.indexOf(1)).map(() => 1))
+ })
+
+ test('a disk that was only busy for a moment soon gives the streams back', () => {
+ const counts = run(new ConcurrencyController(MAX), 32, 30_000, (_, now) => now < 1_000)
+ expect(Math.min(...counts)).toBe(16)
+ expect(counts.indexOf(32, 1) * LOOK_MS).toBeLessThanOrEqual(
+ 2 * DISK_PATIENCE_MS + DISK_RECOVER_MS
+ )
+ expect(counts.at(-1)).toBe(32)
+ })
+
+ test('keeping up below the cap says nothing about it, so the cap stays', () => {
+ const controller = new ConcurrencyController(MAX)
+ const tick = (now: number, streams: number, spareWork: number): Action[] =>
+ controller.tick({
+ now,
+ networks: [network('a', streams)],
+ spareWork,
+ disk:
+ now <= DISK_PATIENCE_MS
+ ? { reading: 'behind', since: 0 }
+ : { reading: 'keeping-up', since: DISK_PATIENCE_MS }
+ })
+ expect(tick(DISK_PATIENCE_MS, 32, 1000)).toEqual([
+ { kind: 'retire', networkId: 'a', count: 16 }
+ ])
+ // Few blocks left to fetch keep it at 3 while the disk keeps up; then more are waiting.
+ expect(tick(DISK_PATIENCE_MS + DISK_RECOVER_MS, 3, 0)).toEqual([])
+ expect(tick(DISK_PATIENCE_MS + DISK_RECOVER_MS + 1, 6, 1000)).toEqual([
+ { kind: 'add', networkId: 'a', count: 6 }
+ ])
+ // Back at the cap, it has to keep up there before the cap rises.
+ const back = DISK_PATIENCE_MS + DISK_RECOVER_MS + 2
+ expect(tick(back, 16, 1000)).toEqual([])
+ expect(tick(back + DISK_RECOVER_MS, 16, 1000)).toEqual([
+ { kind: 'add', networkId: 'a', count: 16 }
+ ])
+ })
+
+ test('a disk cut leaves a server ceiling, and its pace, alone', () => {
+ const counts = run(
+ new ConcurrencyController(MAX),
+ 32,
+ RECOVER_MS - LOOK_MS,
+ (_, now) => now >= 500 && now < 2_000,
+ (now) => (now === 0 ? 2 : 0)
+ )
+ // Back at the 30 the server allowed at the disk's pace, not a stream a minute; never past it.
+ expect(Math.min(...counts)).toBe(8)
+ expect(counts.at(-1)).toBe(30)
+ expect(Math.max(...counts.slice(1))).toBe(30)
+ })
+
+ test('a refusal at the moment the disk cuts still counts', () => {
+ const counts = run(
+ new ConcurrencyController(MAX),
+ 32,
+ RECOVER_MS,
+ (_, now) => now < 2_000,
+ (now) => (now === DISK_PATIENCE_MS ? 5 : 0)
+ )
+ expect(Math.min(...counts)).toBe(4)
+ expect(Math.max(...counts.slice(1))).toBe(27)
+ })
+
+ test('a disk behind only now and then neither cuts nor raises', () => {
+ // Behind long enough for one cut, then every other look.
+ const counts = run(new ConcurrencyController(MAX), 32, 60_000, (_, now) =>
+ now <= DISK_PATIENCE_MS ? true : (now / LOOK_MS) % 2 === 1
+ )
+ expect(counts.slice(DISK_PATIENCE_MS / LOOK_MS)).toEqual(
+ counts.slice(DISK_PATIENCE_MS / LOOK_MS).map(() => 16)
+ )
+ })
+
+ test('the disk cuts every network together', () => {
+ const controller = new ConcurrencyController(MAX)
+ expect(
+ controller.tick({
+ now: DISK_PATIENCE_MS,
+ networks: [network('a', 8), network('b', 8)],
+ spareWork: 0,
+ disk: { reading: 'behind', since: 0 }
+ })
+ ).toEqual([
+ { kind: 'retire', networkId: 'a', count: 4 },
+ { kind: 'retire', networkId: 'b', count: 4 }
+ ])
+ })
+
+ test('the disk leaves a count the user picked alone', () => {
+ const counts = run(new ConcurrencyController(4, false), 4, 30_000, () => true)
+ expect(counts).toEqual(counts.map(() => 4))
+ })
+})
+
+test.describe('disk watch', () => {
+ test('reads the download as a whole, by the clock', () => {
+ const watch = new DiskWatch()
+ watch.landed(0)
+ watch.sample(0, 8, 5)
+ expect(watch.reading).toBe('behind')
+ watch.sample(400, 8, 6)
+ expect(watch.lasted('behind', 400)).toBe(400)
+ // Held, but nothing has landed for a while: a stalled disk isn't judged.
+ watch.sample(DISK_STALLED_MS, 8, 8)
+ expect(watch.reading).toBe('unknown')
+ // Half held is not most.
+ watch.landed(2_000)
+ watch.sample(2_000, 8, 4)
+ expect(watch.reading).toBe('keeping-up')
+ expect(watch.lasted('behind', 2_000)).toBe(0)
+ })
})
+
+const LOOK_MS = 500
+
+/** One network, looked at every LOOK_MS from 0 to `ms` as HttpTransfer does: all its streams are
+ * held while `behind`, and the server refuses `refused` of them. The streams it ran after each
+ * look. */
+function run(
+ controller: ConcurrencyController,
+ streams: number,
+ ms: number,
+ behind: (streams: number, now: number) => boolean,
+ refused: (now: number) => number = () => 0
+): number[] {
+ const watch = new DiskWatch()
+ const counts: number[] = []
+ for (let now = 0; now <= ms; now += LOOK_MS) {
+ watch.landed(now)
+ watch.sample(now, streams, behind(streams, now) ? streams : 0)
+ const refusedNow = refused(now)
+ const actions = controller.tick({
+ now,
+ networks: [network('a', streams, { refused: refusedNow, served: streams - refusedNow })],
+ spareWork: 1000,
+ disk: { reading: watch.reading, since: watch.since }
+ })
+ for (const action of actions) streams += action.kind === 'add' ? action.count : -action.count
+ counts.push(streams)
+ }
+ return counts
+}
diff --git a/e2e/scheduler.spec.ts b/e2e/scheduler.spec.ts
index d143346..1bf445d 100644
--- a/e2e/scheduler.spec.ts
+++ b/e2e/scheduler.spec.ts
@@ -167,6 +167,11 @@ test.describe('scheduler: racing a slow block', () => {
expect(pick(laggard(0), 1, 'a')).toBe('hedge:0')
})
+ test('…but one quiet because its writer is full is as slow as the disk, so it is left alone', () => {
+ const held = { ...attempt(0, 'a'), writeWaiting: true }
+ expect(pick(laggard(0, { attempts: [[0, [held]]] }), 1, 'a')).toBeUndefined()
+ })
+
test('a holder about to finish is left alone', () => {
// 800 bytes left at 1000 B/s: 0.8 s.
expect(pick(laggard(1000), 1, 'a')).toBeUndefined()
@@ -358,3 +363,13 @@ test.describe('scheduler: racing a slow block', () => {
)
})
})
+
+test('no racing while the disk is behind, and racing resumes once it keeps up', () => {
+ const s = state({
+ blocks: [block(0, 'downloading')],
+ streams: [stream(0, 'a', 'downloading'), stream(1, 'b', 'pending')],
+ attempts: [[0, [attempt(0, 'a')]]]
+ })
+ expect(pick({ ...s, diskBehind: true }, 1, 'b')).toBeUndefined()
+ expect(pick(s, 1, 'b')).toBe('hedge:0')
+})
diff --git a/e2e/sparseFile.spec.ts b/e2e/sparseFile.spec.ts
new file mode 100644
index 0000000..8369e7f
--- /dev/null
+++ b/e2e/sparseFile.spec.ts
@@ -0,0 +1,22 @@
+import { execFileSync } from 'node:child_process'
+import { mkdtemp, open, rm } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { expect, test } from '@playwright/test'
+import { markSparse } from '../src/main/download/sparseFile'
+
+test('a staging file on NTFS is sparse, so a write far into it zeroes nothing first', async () => {
+ test.skip(process.platform !== 'win32', 'NTFS only')
+ const dir = await mkdtemp(join(tmpdir(), 'plexo-sparse-'))
+ try {
+ const sparse = join(dir, 'sparse.plexo')
+ await (await open(sparse, 'wx+')).close()
+
+ expect(await markSparse(sparse)).toBe(true)
+ expect(execFileSync('fsutil', ['sparse', 'queryflag', sparse], { encoding: 'utf8' })).toMatch(
+ /is set as sparse/i
+ )
+ } finally {
+ await rm(dir, { recursive: true, force: true })
+ }
+})
diff --git a/e2e/writeWait.spec.ts b/e2e/writeWait.spec.ts
new file mode 100644
index 0000000..0bd2f12
--- /dev/null
+++ b/e2e/writeWait.spec.ts
@@ -0,0 +1,37 @@
+import { expect, test } from './fixtures'
+
+test.use({
+ appEnv: {
+ PLEXO_E2E_BLOCK_BYTES: String(1024 * 1024),
+ PLEXO_E2E_HEDGE_MS: '2000',
+ PLEXO_E2E_SLOW_WARMUP_MS: '5000',
+ PLEXO_E2E_SLOW_FOR_MS: '10000'
+ }
+})
+
+test('a disk-held range is not fetched again while other streams finish @smoke', async ({
+ plexo,
+ serve
+}) => {
+ await plexo.evaluateMain(() => {
+ const fs = process.getBuiltinModule('node:fs') as typeof import('node:fs')
+ const prototype = fs.WriteStream.prototype as unknown as {
+ _write: (chunk: Buffer, encoding: string, callback: (error?: Error | null) => void) => void
+ }
+ const original = prototype._write
+ let first = true
+ prototype._write = function (chunk, encoding, callback) {
+ const hold = first
+ first = false
+ original.call(this, chunk, encoding, (error) => {
+ if (hold) setTimeout(() => callback(error), 3000)
+ else callback(error)
+ })
+ }
+ }, null)
+ const origin = await serve({ size: 4 * 1024 * 1024 })
+ await plexo.start(origin.url(), origin.sha256, { connections: 4 })
+ await plexo.waitForHttpStatus('completed', 15000)
+ const ranges = origin.chunkRequests()
+ expect(ranges).toHaveLength(4)
+})
diff --git a/electron-builder.yml b/electron-builder.yml
index 65d9537..4471e64 100644
--- a/electron-builder.yml
+++ b/electron-builder.yml
@@ -9,9 +9,10 @@ files:
- '!electron.vite.config.{js,ts,mjs,cjs}'
- '!{.eslintcache,eslint.config.mjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
- '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'
- # koffi is only loaded on Linux (network/deviceBinding.ts) and ships prebuilds for every OS.
+ # koffi ships prebuilds for every OS; it's loaded on Linux (network/deviceBinding.ts) and on
+ # Windows (download/sparseFile.ts).
- '!node_modules/koffi/{src,vendor,doc}/**'
- - '!node_modules/koffi/build/koffi/!(linux_x64|linux_arm64)/**'
+ - '!node_modules/koffi/build/koffi/!(linux_x64|linux_arm64|win32_x64|win32_arm64)/**'
# Plexo uses TCP peers only. simple-peer is patched to use browser globals instead of loading
# the native WebRTC polyfill; don't ship unused host-platform binaries in cross-platform builds.
- '!node_modules/node-datachannel/**'
diff --git a/src/main/download/chunkDownloader.ts b/src/main/download/chunkDownloader.ts
index 6979816..23f11cf 100644
--- a/src/main/download/chunkDownloader.ts
+++ b/src/main/download/chunkDownloader.ts
@@ -17,6 +17,8 @@ export interface ChunkDownloadOptions {
onNetworkProgress: (bytesReceivedThisRun: number) => void
/** Bytes accepted by the destination writer; safe to include in resumable progress. */
onProgress: (bytesDownloadedThisRun: number) => void
+ /** Whether delivered bytes wait on the disk: its writer is full, or writing the last bytes. */
+ onWriteWait?: (waiting: boolean) => void
signal: AbortSignal
/** The version the download started on, plus any confirmed to serve identical bytes. */
acceptedVersions: FileVersion[]
@@ -132,7 +134,8 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
signal,
acceptedVersions,
onResponse,
- throttle
+ throttle,
+ onWriteWait
} = options
return new Promise((resolve, reject) => {
@@ -179,10 +182,15 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
// download, or a chunk that retries a few times, leaks one per
// attempt until the process hits its open-file limit), and its
// buffered writes would otherwise land after a retry has started.
+ // It is closed by writing out what it holds, unless writing is what
+ // failed: every buffered byte was checked against the range before it
+ // was queued, so it counts like any other, and whoever takes the block
+ // next resumes after it instead of fetching it again.
const stream = currentFileStream
if (stream && !stream.closed) {
stream.once('close', () => reject(error))
- stream.destroy()
+ if (stream.errored) stream.destroy()
+ else stream.end()
} else {
reject(error)
}
@@ -290,6 +298,7 @@ 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 = false
const hold = (): void => {
if (holds++ === 0) {
clearWatchdog()
@@ -297,7 +306,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
}
}
const release = (): void => {
- if (--holds > 0 || settled) return
+ if (--holds > 0 || settled || bodyEnded) return
resetWatchdog()
res.resume()
}
@@ -318,13 +327,19 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
const progress = bytesDownloaded
onNetworkProgress(progress)
if (
+ // Every write's callback comes before the writer closes, and this only
+ // settles once it has, stopped or not.
!fileStream.write(usable, (error) => {
if (error) fail(error)
- else if (!settled) onProgress(progress)
+ else onProgress(progress)
})
) {
+ onWriteWait?.(true)
hold()
- fileStream.once('drain', release)
+ fileStream.once('drain', () => {
+ onWriteWait?.(bodyEnded)
+ release()
+ })
}
const wait = throttle?.(usable.length) ?? 0
if (wait > 0) {
@@ -349,6 +364,11 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
)
return
}
+ bodyEnded = true
+ // 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()
diff --git a/src/main/download/concurrency.ts b/src/main/download/concurrency.ts
index ab8c15c..0c10753 100644
--- a/src/main/download/concurrency.ts
+++ b/src/main/download/concurrency.ts
@@ -18,14 +18,35 @@
// - A server that refuses while sending nothing isn't limiting connections: it is busy, or the
// link has expired or been denied. That is waited out or reported elsewhere (see finishFailed
// in downloadManager.ts), and the count stays. The network doesn't grow meanwhile.
-// - Each network is decided on its own, so one that drops out or comes back never disturbs the
-// others.
+// - A disk that can't keep up holds each stream's reading until its writes catch up. Streams
+// writing to scattered places are what slow a hard drive down, so while the disk has been behind
+// for DISK_PATIENCE_MS (see DiskWatch) no network grows, and every network is cut to half the
+// most any runs; each DISK_RECOVER_MS it keeps up at the cap, the cap doubles. A hard drive
+// behind at every count is down to the one stream it writes fastest with in seconds, and a disk
+// that was only busy for a moment soon has its streams back. The disk is decided for the whole
+// download, not per network: a stream only fills its writer when it receives fast, so a fast
+// network would read as held and a slow one wouldn't, and the fast one alone would be cut while
+// the slow one's streams kept writing all over the disk. The cap is the disk's own: a server's
+// refusals keep their ceiling and pace beside it, and each network runs the lower of the two. A
+// download the disk keeps up with is never behind, so this costs nothing anywhere else. The
+// user's own pick is kept, as it is for everything but refusals.
+// - Otherwise each network is decided on its own, so one that drops out or comes back never
+// disturbs the others.
//
// No I/O and no clock of its own: it reads a snapshot, time included, and says what to do, so
// every rule can be checked against exact situations.
/** How long a lowered limit holds before it may rise by one. */
export const RECOVER_MS = 60_000
+/** How long the disk must stay behind before streams are cut. */
+export const DISK_PATIENCE_MS = 500
+/** How long the disk must keep up before the cap it set rises: long against DISK_PATIENCE_MS, so a
+ * count the disk fell behind at isn't tried again until it has shown it keeps up, and short enough
+ * that a disk busy for a moment has its streams back within seconds. */
+export const DISK_RECOVER_MS = 10_000
+/** No write landed for this long: the disk has stopped rather than slowed down. A working hard drive
+ * lands a writer's worth (512 KiB) in tens of ms. */
+export const DISK_STALLED_MS = 500
export interface NetworkSnapshot {
id: string
@@ -39,20 +60,77 @@ export interface NetworkSnapshot {
served: number
}
+/** What DiskWatch reads, and since when without a break. */
+export interface DiskSnapshot {
+ reading: DiskReading
+ since: number
+}
+
export interface Snapshot {
now: number
/** The networks in use: the ones it may grow. */
networks: readonly NetworkSnapshot[]
/** How many more streams the waiting blocks could keep busy. */
spareWork: number
+ /** Absent, the disk isn't judged. */
+ disk?: DiskSnapshot
}
export type Action = { kind: 'add' | 'retire'; networkId: string; count: number }
+/** - behind: most of the download's writing streams hold bytes the disk hasn't taken, while writes
+ * land.
+ * - keeping-up: most don't.
+ * - unknown: no stream writing, or held while nothing lands: a disk that has stopped altogether
+ * (another program, a drive waking up) is waited out, as fewer streams wouldn't help it. */
+export type DiskReading = 'behind' | 'keeping-up' | 'unknown'
+
+/** Whether the disk is what holds a download back, judged by the clock rather than by how often
+ * it is looked at: a look between two others only extends a reading or breaks it. */
+export class DiskWatch {
+ reading: DiskReading = 'unknown'
+ since = 0
+ private landedAt = 0
+
+ /** A write reached the disk. */
+ landed(now: number): void {
+ this.landedAt = now
+ }
+
+ /** One look at the download: its streams that are fetching a block, and how many of those hold
+ * bytes the disk hasn't taken. One between blocks, or waiting to retry, says nothing either way. */
+ sample(now: number, writing: number, held: number): void {
+ const reading: DiskReading =
+ writing === 0
+ ? 'unknown'
+ : held * 2 <= writing
+ ? 'keeping-up'
+ : now - this.landedAt < DISK_STALLED_MS
+ ? 'behind'
+ : 'unknown'
+ if (reading === this.reading) return
+ this.reading = reading
+ this.since = now
+ }
+
+ /** How long it has read `reading` without a break; 0 if it doesn't now. */
+ lasted(reading: DiskReading, now: number): number {
+ return this.reading === reading ? now - this.since : 0
+ }
+
+ reset(): void {
+ this.reading = 'unknown'
+ this.since = 0
+ }
+}
+
export class ConcurrencyController {
/** Networks a server has refused on: the most streams each may run for now, and when that was
* last changed. */
private readonly ceilings = new Map<string, { limit: number; since: number }>()
+ /** The most streams any network may run while the disk can't keep up (null: no cap), and from
+ * when what the disk does counts toward moving it: when it last moved, or last ran below it. */
+ private disk = { cap: null as number | null, from: 0 }
constructor(
/** The most streams a network may run. */
@@ -65,28 +143,27 @@ export class ConcurrencyController {
const actions: Action[] = []
const { now } = snapshot
let spare = snapshot.spareWork
+ if (this.grows) this.judgeDisk(snapshot)
+ const diskHolds = this.grows && !!snapshot.disk && snapshot.disk.reading !== 'keeping-up'
for (const network of snapshot.networks) {
const { id, streams } = network
const refusedNow = network.refused > 0 && network.served > 0
- let entry = this.ceilings.get(id)
+ const entry = this.ceilings.get(id)
if (refusedNow) {
const limit = Math.max(1, Math.min(streams - network.refused, entry?.limit ?? Infinity))
- this.ceilings.set(id, (entry = { limit, since: now }))
+ this.ceilings.set(id, { limit, since: now })
} else if (entry && network.refused === 0 && now - entry.since >= RECOVER_MS) {
entry.limit++
entry.since = now
- if (entry.limit >= this.maxPerNetwork) {
- this.ceilings.delete(id)
- entry = undefined
- }
+ if (entry.limit >= this.maxPerNetwork) this.ceilings.delete(id)
}
- const ceiling = Math.min(this.maxPerNetwork, entry?.limit ?? Infinity)
+ const ceiling = this.limit(id)
if (streams > ceiling) {
actions.push({ kind: 'retire', networkId: id, count: streams - ceiling })
continue
}
- // Not while the server is turning requests away.
- if (network.refused > 0 || streams === 0 || network.answered < streams) continue
+ // Not while the server is turning requests away, or the disk isn't keeping up.
+ if (network.refused > 0 || diskHolds || streams === 0 || network.answered < streams) continue
const count = Math.min(this.grows ? streams : Infinity, ceiling - streams, spare)
if (count < 1) continue
spare -= count
@@ -94,4 +171,32 @@ export class ConcurrencyController {
}
return actions
}
+
+ /** The most streams a network may run now: the lower of a server's ceiling and the disk's cap,
+ * and never more than the limit. Also what a network joining the download starts with at most. */
+ limit(networkId: string): number {
+ return Math.min(
+ this.maxPerNetwork,
+ this.ceilings.get(networkId)?.limit ?? Infinity,
+ this.disk.cap ?? Infinity
+ )
+ }
+
+ /** Moves the disk's cap (see the rules above). */
+ private judgeDisk({ now, disk, networks }: Snapshot): void {
+ if (!disk) return
+ const streams = Math.max(0, ...networks.map((network) => network.streams))
+ const { cap, from } = this.disk
+ // Only what was seen since the cap last moved says anything about the count it allows.
+ const lasted = now - Math.max(disk.since, from)
+ if (disk.reading === 'behind' && lasted >= DISK_PATIENCE_MS && streams > 1) {
+ this.disk.cap = Math.ceil(streams / 2)
+ } else if (disk.reading === 'keeping-up' && cap !== null && streams < cap) {
+ // Keeping up below the cap (a server's ceiling, or too few blocks left) says nothing about
+ // it: the wait starts once the streams are back at it.
+ } else if (disk.reading === 'keeping-up' && cap !== null && lasted >= DISK_RECOVER_MS) {
+ this.disk.cap = cap * 2 >= this.maxPerNetwork ? null : cap * 2
+ } else return
+ this.disk.from = now
+ }
}
diff --git a/src/main/download/downloadFile.ts b/src/main/download/downloadFile.ts
index b5c8f8c..160e623 100644
--- a/src/main/download/downloadFile.ts
+++ b/src/main/download/downloadFile.ts
@@ -2,14 +2,24 @@ import { createWriteStream, type WriteStream } from 'node:fs'
import { lstat, open, rename, rm, stat } from 'node:fs/promises'
import { basename, dirname, extname, join } from 'node:path'
+/** Per stream: 32 streams hold at most 16 MiB. A full buffer is also what tells Auto the disk is
+ * behind (see concurrency.ts): larger, and it hears it later. */
+const WRITE_BUFFER_BYTES = 512 * 1024
+
/** The only large file owned by a download. It lives beside the final file so publishing it
* requires no copy and never needs a second file's worth of disk space. */
export class DownloadFile {
constructor(readonly path: string) {}
- /** Every writer has its own descriptor and explicit offset. Never use append mode here. */
+ /** Every writer has its own descriptor and explicit offset. Never use append mode here.
+ * While a write is on its way to disk the next ones queue, up to WRITE_BUFFER_BYTES, and go
+ * down together in one writev: a disk that falls behind gets fewer, larger writes. */
writer(position: number): WriteStream {
- return createWriteStream(this.path, { flags: 'r+', start: position })
+ return createWriteStream(this.path, {
+ flags: 'r+',
+ start: position,
+ highWaterMark: WRITE_BUFFER_BYTES
+ })
}
async read(position: number, length: number): Promise<Buffer> {
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index 3b8a390..96e28a7 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -72,6 +72,8 @@ interface RuntimeFields {
publishing: boolean
pushScheduled: boolean
persistenceTimer?: NodeJS.Timeout
+ /** A routine checkpoint waits its turn: it will save whatever has changed by then. */
+ checkpointQueued?: boolean
persistenceChain: Promise<void>
removed: boolean
/** Updates sent to the window so far (see DownloadUpdate). */
@@ -1634,14 +1636,22 @@ export class DownloadManager {
}
private schedulePersistence(runtime: DownloadRuntime): void {
- if (this.suspending || runtime.removed || runtime.persistenceTimer) return
+ if (this.suspending || runtime.removed || runtime.persistenceTimer || runtime.checkpointQueued)
+ return
runtime.persistenceTimer = setTimeout(() => {
runtime.persistenceTimer = undefined
- void this.persistNow(runtime)
+ // One running and one waiting at most, however slow the disk: the waiting one takes the
+ // state when its turn comes, so nothing scheduled meanwhile is lost.
+ runtime.checkpointQueued = true
+ void this.persistNow(runtime, false, true)
}, CHECKPOINT_INTERVAL_MS)
}
- private persistNow(runtime: DownloadRuntime, required = false): Promise<void> {
+ private persistNow(
+ runtime: DownloadRuntime,
+ required = false,
+ routineCheckpoint = false
+ ): Promise<void> {
if (runtime.removed) return runtime.persistenceChain
if (runtime.persistenceTimer) {
clearTimeout(runtime.persistenceTimer)
@@ -1651,6 +1661,9 @@ export class DownloadManager {
const operation = runtime.persistenceChain
.catch(() => {})
.then(async () => {
+ // An explicit save can run ahead of a queued routine checkpoint. Only that
+ // checkpoint owns the flag; other saves must leave its place reserved.
+ if (routineCheckpoint) runtime.checkpointQueued = false
if (runtime.removed) return
const dir = this.downloadDir(runtime.state.id)
const path = this.manifestPath(runtime.state.id)
diff --git a/src/main/download/httpTransfer.ts b/src/main/download/httpTransfer.ts
index 2ab372a..c709886 100644
--- a/src/main/download/httpTransfer.ts
+++ b/src/main/download/httpTransfer.ts
@@ -8,7 +8,14 @@ import type {
} from '../../shared/types'
import { testKnobs, testStreamsPerNetwork } from '../testKnobs'
import { advanceBlock, retractBlock } from './blockProgress'
-import { ConcurrencyController, type Action, type Snapshot } from './concurrency'
+import {
+ ConcurrencyController,
+ DISK_PATIENCE_MS,
+ DISK_RECOVER_MS,
+ DiskWatch,
+ type Action,
+ type Snapshot
+} from './concurrency'
import { downloadChunk, fetchRange, HttpStatusError, RemoteChangedError } from './chunkDownloader'
import { compareVersion, type FileVersion } from './fileVersion'
import { interleave, MAX_STREAMS_PER_NETWORK, startingStreams } from './plan'
@@ -46,6 +53,7 @@ interface Attempt {
/** Bytes the network has delivered, independently of disk backpressure. */
networkReceived: number
lastNetworkAt: number
+ writeWaiting: boolean
/** When the request was sent. */
startedAt: number
/** The network previously credited for this block's prefix. */
@@ -241,6 +249,8 @@ export class HttpTransfer implements Transfer {
/** 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. */
private concurrency: ConcurrencyController | null
+ /** Whether the disk is what holds the download back, for Auto, the user and hedges. */
+ private readonly disk = new DiskWatch()
constructor(
private readonly runtime: HttpTransferTarget,
@@ -289,7 +299,10 @@ export class HttpTransfer implements Transfer {
: network.status === 'unreachable'
? 1
: joining.includes(network)
- ? Math.max(live.length, starting)
+ ? Math.max(
+ live.length,
+ Math.min(starting, this.concurrency?.limit(network.id) ?? starting)
+ )
: live.length
if (joining.includes(network)) this.trafficOf(network.id).aliveAt = now
if (live.length > target) this.retireStreams(network.id, live.length - target)
@@ -310,7 +323,9 @@ export class HttpTransfer implements Transfer {
tick(now: number): void {
this.refreshStuckConnections(now)
this.host.reconcile()
- this.adjustStreams(this.concurrency?.tick(this.concurrencySnapshot()))
+ // Taken with or without Auto: it also judges the disk, which the user is told about either way.
+ const snapshot = this.concurrencySnapshot(now)
+ this.adjustStreams(this.concurrency?.tick(snapshot))
// Each refusal is counted once.
for (const self of this.chunkRuntimes.values()) {
self.refused = false
@@ -318,6 +333,22 @@ export class HttpTransfer implements Transfer {
}
}
+ /** Looks at whether the disk is what holds the download back, and tells the user: as soon as
+ * Auto would act on it, and until the disk has kept up for as long as Auto waits to add streams
+ * back. Whatever the stream count, Auto or picked. */
+ private noteDisk(now: number, writing: number, held: number): void {
+ this.disk.sample(now, writing, held)
+ // Nothing writing, nothing for the disk to hold back: a download waiting on its networks
+ // isn't blamed on the drive.
+ const limited =
+ writing > 0 &&
+ (this.disk.lasted('behind', now) >= DISK_PATIENCE_MS ||
+ (!!this.runtime.state.diskLimited && this.disk.lasted('keeping-up', now) < DISK_RECOVER_MS))
+ if (limited === !!this.runtime.state.diskLimited) return
+ this.runtime.state.diskLimited = limited || undefined
+ this.host.scheduleUpdate()
+ }
+
running(): Iterable<Promise<void>> {
return this.workers.values()
}
@@ -329,6 +360,7 @@ export class HttpTransfer implements Transfer {
reset(): void {
this.avoidNetworkByBlock.clear()
this.reconciled.clear()
+ this.disk.reset()
}
systemResumed(now: number): void {
@@ -435,6 +467,12 @@ export class HttpTransfer implements Transfer {
const attempt = self?.attempt
if (!self || !attempt || attempt.abortReason) continue
+ // A paused reader or final flush says nothing about the connection's health: the disk set
+ // its pace. A crawling connection never fills its writer, so this never hides one.
+ if (attempt.writeWaiting) {
+ self.slowSince = null
+ continue
+ }
const silent = this.isSilent(attempt, now)
// A refresh isn't a failure, so no failed request would say that the network can't get
// through; a connection gone silent with the rest of its network says it instead, as soon
@@ -544,6 +582,7 @@ export class HttpTransfer implements Transfer {
received: 0,
networkReceived: 0,
lastNetworkAt: 0,
+ writeWaiting: false,
startedAt: Date.now(),
// Whoever held this block before now is the one whose tail bytes a truncation would
// discard — captured before the lease overwrites the field.
@@ -625,6 +664,7 @@ export class HttpTransfer implements Transfer {
acceptedVersions: this.acceptedVersions,
onResponse: (info) => (attempt.response = info),
throttle: (bytes) => this.host.limits.take(attempt.networkId, bytes),
+ onWriteWait: (waiting) => (attempt.writeWaiting = waiting),
onNetworkProgress: (bytesThisRun) => {
const delta = bytesThisRun - attempt.networkReceived
if (delta > 0) {
@@ -637,6 +677,7 @@ export class HttpTransfer implements Transfer {
const delta = bytesThisRun - attempt.received
if (delta > 0) {
attempt.received = bytesThisRun
+ this.disk.landed(Date.now())
this.onAttemptProgress(chunk, attempt)
}
}
@@ -916,8 +957,12 @@ export class HttpTransfer implements Transfer {
this.goIdle(chunk)
}
- private concurrencySnapshot(): Snapshot {
+ /** One look at the download's streams: what concurrency.ts sizes them by, and what the disk is
+ * judged by (see noteDisk). */
+ private concurrencySnapshot(now: number): Snapshot {
const inUse = this.runtime.state.networks.filter((network) => network.status === 'on')
+ let writing = 0
+ let held = 0
const networks = inUse.map(({ id }) => {
let streams = 0
let answered = 0
@@ -929,12 +974,16 @@ export class HttpTransfer implements Transfer {
if (self && self.receivedBytes > 0) answered++
if (self?.refused) refused++
if (self?.served) served++
+ if (self?.attempt) writing++
+ if (self?.attempt?.writeWaiting) held++
}
return { id, streams, answered, refused, served }
})
+ this.noteDisk(now, writing, held)
// A new stream takes a waiting block the moment it starts.
const waiting = this.runtime.blocks.filter((block) => block.status === 'pending').length
- return { now: Date.now(), networks, spareWork: waiting }
+ const { reading, since } = this.disk
+ return { now, networks, spareWork: waiting, disk: { reading, since } }
}
/** New streams on `networkId`, put on the download's list for the caller to start. */
@@ -1031,7 +1080,8 @@ export class HttpTransfer implements Transfer {
streams: this.runtime.state.streams,
attempts: this.attempts,
avoid: this.avoidNetworkByBlock,
- hedgesUsed: this.hedgesByBlock
+ hedgesUsed: this.hedgesByBlock,
+ diskBehind: this.disk.reading === 'behind'
},
{
id: chunk.id,
diff --git a/src/main/download/paths.ts b/src/main/download/paths.ts
index 0cced9b..936544c 100644
--- a/src/main/download/paths.ts
+++ b/src/main/download/paths.ts
@@ -1,6 +1,7 @@
import { app } from 'electron'
import { lstat, mkdir, open, rm, stat } from 'node:fs/promises'
import { basename, extname, join } from 'node:path'
+import { markSparse } from './sparseFile'
export function getDefaultDownloadsDir(): string {
return app.getPath('downloads')
@@ -96,7 +97,10 @@ export async function reserveDestinationPath(directory: string, fileName: string
async (candidate) => {
const handle = await open(`${candidate}.plexo`, 'wx+')
await handle.close()
- if (!(await pathExists(candidate))) return true
+ if (!(await pathExists(candidate))) {
+ await markSparse(`${candidate}.plexo`)
+ return true
+ }
await rm(`${candidate}.plexo`)
return false
}
diff --git a/src/main/download/scheduler.ts b/src/main/download/scheduler.ts
index bb5c461..fda2924 100644
--- a/src/main/download/scheduler.ts
+++ b/src/main/download/scheduler.ts
@@ -19,6 +19,9 @@ export interface AttemptView {
networkId: string
/** When its request was sent. */
startedAt: number
+ /** 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
}
export interface SchedulerState {
@@ -30,6 +33,8 @@ export interface SchedulerState {
avoid: ReadonlyMap<number, string>
/** How many hedges each block has had so far. */
hedgesUsed: ReadonlyMap<number, number>
+ /** The disk can't keep up (see DiskWatch in concurrency.ts): a hedge would only add writes. */
+ diskBehind?: boolean
}
export interface SchedulerPolicy {
@@ -81,8 +86,8 @@ function nextHedgeTarget(
now: number,
policy: SchedulerPolicy
): HttpBlockState | undefined {
- // Only when everything left is already being fetched.
- if (state.blocks.some((block) => block.status === 'pending')) return undefined
+ // Only when everything left is already being fetched, and the disk has room for more writes.
+ if (state.diskBehind || state.blocks.some((block) => block.status === 'pending')) return undefined
const speedOf = (streamId: number): number =>
state.streams.find((stream) => stream.id === streamId)?.speedBytesPerSec ?? 0
@@ -101,6 +106,8 @@ function nextHedgeTarget(
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
+ // 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
const remaining = block.rangeEnd - block.rangeStart + 1 - block.bytesDownloaded
if (remaining <= 0) continue
diff --git a/src/main/download/sparseFile.ts b/src/main/download/sparseFile.ts
new file mode 100644
index 0000000..a89564a
--- /dev/null
+++ b/src/main/download/sparseFile.ts
@@ -0,0 +1,94 @@
+import type { IKoffiLib } from 'koffi'
+
+// On NTFS a write past the last byte written so far first fills the gap with zeros, on disk,
+// while the write waits (https://devblogs.microsoft.com/oldnewthing/20110922-00/?p=9573). Streams
+// start all over the staging file, so a block's first write could zero megabytes ahead of it, and
+// most of the file would be written twice. A sparse file has no gap to fill: what hasn't been
+// written reads as zeros. Node can't ask for it, so it's asked of kernel32. Only an
+// optimisation: a file system without sparse files (exFAT, FAT32) refuses, and the file stays
+// as it was.
+
+const GENERIC_WRITE = 0x40000000
+const SHARE_ALL = 0x7 // read, write and delete: nobody else's handle is disturbed
+const OPEN_EXISTING = 3
+const FSCTL_SET_SPARSE = 0x900c4
+const INVALID_HANDLE = BigInt.asUintN(64, -1n)
+
+type KoffiFunction = ReturnType<IKoffiLib['func']>
+
+interface Kernel32 {
+ createFile: KoffiFunction
+ deviceIoControl: KoffiFunction
+ closeHandle: KoffiFunction
+ address: (handle: unknown) => bigint
+}
+
+let kernel32: Promise<Kernel32 | null> | null = null
+
+function load(): Promise<Kernel32 | null> {
+ kernel32 ??= (async () => {
+ try {
+ const { default: koffi } = await import('koffi')
+ const lib = koffi.load('kernel32.dll')
+ return {
+ createFile: lib.func(
+ 'void * __stdcall CreateFileW(const char16_t *name, uint32_t access, uint32_t share, void *security, uint32_t disposition, uint32_t flags, void *template)'
+ ),
+ deviceIoControl: lib.func(
+ 'int __stdcall DeviceIoControl(void *handle, uint32_t code, void *input, uint32_t inputSize, void *output, uint32_t outputSize, _Out_ uint32_t *returned, void *overlapped)'
+ ),
+ closeHandle: lib.func('int __stdcall CloseHandle(void *handle)'),
+ address: (handle) => koffi.address(handle)
+ }
+ } catch (error) {
+ console.warn('Sparse staging files unavailable:', error)
+ return null
+ }
+ })()
+ return kernel32
+}
+
+const call = <T>(fn: KoffiFunction, ...args: unknown[]): Promise<T> =>
+ new Promise((resolve, reject) =>
+ fn.async(...args, (error: unknown, result: T) => (error ? reject(error) : resolve(result)))
+ )
+
+/** Makes the file at `path` sparse on Windows; whether it did. Never throws. */
+export async function markSparse(path: string): Promise<boolean> {
+ if (process.platform !== 'win32') return false
+ const lib = await load()
+ if (!lib) return false
+ try {
+ const handle = await call<unknown>(
+ lib.createFile,
+ path,
+ GENERIC_WRITE,
+ SHARE_ALL,
+ null,
+ OPEN_EXISTING,
+ 0,
+ null
+ )
+ if (lib.address(handle) === INVALID_HANDLE) return false
+ try {
+ const returned = [0]
+ const ok = await call<number>(
+ lib.deviceIoControl,
+ handle,
+ FSCTL_SET_SPARSE,
+ null,
+ 0,
+ null,
+ 0,
+ returned,
+ null
+ )
+ return ok !== 0
+ } finally {
+ // The file is sparse or not by now, whatever closing says.
+ await call(lib.closeHandle, handle).catch(() => {})
+ }
+ } catch {
+ return false
+ }
+}
diff --git a/src/main/download/transfer.ts b/src/main/download/transfer.ts
index d91d7c6..c667576 100644
--- a/src/main/download/transfer.ts
+++ b/src/main/download/transfer.ts
@@ -220,6 +220,7 @@ export function clearSpeeds(state: DownloadState): void {
const connections = state.kind === 'http' ? state.streams : state.peers
for (const connection of connections) connection.speedBytesPerSec = 0
for (const network of state.networks) network.speedBytesPerSec = 0
+ if (state.kind === 'http') state.diskLimited = undefined
if (state.kind === 'torrent') {
state.uploadSpeedBytesPerSec = 0
for (const network of state.networks) network.uploadSpeedBytesPerSec = 0
diff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsx
index 97aeeb6..2bd1871 100644
--- a/src/renderer/src/screens/DownloadingScreen.tsx
+++ b/src/renderer/src/screens/DownloadingScreen.tsx
@@ -1,5 +1,5 @@
import { SPEED_HISTORY_SECONDS, type DownloadState } from '@shared/types'
-import { Folder } from 'lucide-react'
+import { AlertTriangle, Folder } from 'lucide-react'
import { memo, useEffect, useState } from 'react'
import { BlockGrid } from '../components/BlockGrid'
import { ColorBadge } from '../components/ColorBadge'
@@ -52,17 +52,21 @@ function BigStat({
label,
value,
unit,
- valueClass
+ valueClass,
+ note
}: {
label: string
value: string | number
unit?: string
valueClass: string
+ /** Beside the label, e.g. a warning about what holds the value down. */
+ note?: React.ReactNode
}): React.JSX.Element {
return (
<>
- <div className="font-mono text-[10px] leading-none font-medium tracking-[0.2em] text-muted-foreground">
+ <div className="flex items-center gap-1.5 font-mono text-[10px] leading-none font-medium tracking-[0.2em] text-muted-foreground">
{label}
+ {note}
</div>
<div className="flex items-baseline gap-[7px]">
<div
@@ -256,6 +260,28 @@ export const DownloadingScreen = memo(function DownloadingScreen({
value={isPaused ? '—' : speedValue}
unit={isPaused ? undefined : speedUnit}
valueClass={isPaused ? 'text-muted-foreground' : 'text-foreground'}
+ note={
+ !isPaused &&
+ download.kind === 'http' &&
+ download.diskLimited && (
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <span
+ role="img"
+ aria-label="Slow drive"
+ className="inline-flex text-[var(--swatch-amber)]"
+ >
+ <AlertTriangle aria-hidden className="size-3" />
+ </span>
+ }
+ />
+ <TooltipContent>
+ Your drive can’t save any faster, so this download is going slower.
+ </TooltipContent>
+ </Tooltip>
+ )
+ }
/>
<div className="flex items-center gap-2 font-mono text-[10px] leading-none font-medium tabular-nums text-muted-foreground">
<InlineStat label="AVG" value={formatSpeed(avgSpeedBytesPerSec)} />
diff --git a/src/shared/types.ts b/src/shared/types.ts
index b0833dc..15235c6 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -229,6 +229,8 @@ export interface HttpDownloadState extends DownloadStateBase {
blocks: HttpBlockState[]
totalBlocks: number
blockSizeBytes: number
+ /** The disk can't keep up: most streams are waiting for their writes (see concurrency.ts). */
+ diskLimited?: boolean
}
export interface TorrentDownloadState extends DownloadStateBase {