Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

feat: add connection stall watchdog, desktop download notifications, and optimized stream buffering optimizations

Anmol Kapil1d277a414a6d
diff --git a/src/main/download/chunkDownloader.ts b/src/main/download/chunkDownloader.ts
index 9c39cd9..df3ffc1 100644
--- a/src/main/download/chunkDownloader.ts
+++ b/src/main/download/chunkDownloader.ts
@@ -113,10 +113,26 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
let settled = false
let currentReq: ClientRequest | null = null
let currentFileStream: WriteStream | null = null
+ let stallWatchdog: NodeJS.Timeout | null = null
+
+ const clearWatchdog = (): void => {
+ if (stallWatchdog) {
+ clearTimeout(stallWatchdog)
+ stallWatchdog = null
+ }
+ }
+
+ const resetWatchdog = (): void => {
+ clearWatchdog()
+ stallWatchdog = setTimeout(() => {
+ fail(new Error('Connection stalled: no response from server'))
+ }, STALL_TIMEOUT_MS)
+ }
const finish = (fn: () => void): void => {
if (settled) return
settled = true
+ clearWatchdog()
signal.removeEventListener('abort', onAbort)
fn()
}
@@ -240,11 +256,16 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
res.on('error', fail)
fileStream.on('error', fail)
+ // Arm watchdog for incoming body bytes — drops and retries if the server sends
+ // headers then freezes, or goes silent mid-stream.
+ resetWatchdog()
+
// Written by hand rather than piped so an overlong body can be cut off
// at the range boundary: a part file longer than its block would push
// every byte after it out of place at reassembly time.
res.on('data', (chunk: Buffer) => {
if (settled) return
+ resetWatchdog()
const remaining =
expectedBytes === null ? chunk.length : expectedBytes - bytesDownloaded
@@ -256,7 +277,10 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
onProgress(bytesDownloaded)
if (!fileStream.write(usable)) {
res.pause()
- fileStream.once('drain', () => res.resume())
+ fileStream.once('drain', () => {
+ resetWatchdog()
+ res.resume()
+ })
}
}
@@ -267,6 +291,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
res.on('end', () => {
if (settled) return
+ clearWatchdog()
if (expectedBytes !== null && bytesDownloaded !== expectedBytes) {
fail(
new Error(
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index 8aebe55..473ba04 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -14,7 +14,7 @@ import {
import { basename, join } from 'node:path'
import { finished, pipeline } from 'node:stream/promises'
import type { BrowserWindow } from 'electron'
-import { app } from 'electron'
+import { app, Notification } from 'electron'
import { IpcChannels } from '../../shared/ipc-channels'
import type {
BlockState,
@@ -793,9 +793,11 @@ export class DownloadManager {
runtime.state.status = 'completed'
runtime.state.completedAt = Date.now()
runtime.state.bytesDownloaded = runtime.state.totalBytes || runtime.state.bytesDownloaded
+ this.notify('Download Complete', `${runtime.state.fileName} has finished downloading.`)
} catch (error) {
runtime.state.status = 'error'
runtime.state.error = error instanceof Error ? error.message : String(error)
+ this.notify('Download Failed', `${runtime.state.fileName}: ${runtime.state.error}`)
}
this.pushUpdate(runtime)
@@ -803,6 +805,24 @@ export class DownloadManager {
await this.discardUnfinishedDestination(runtime)
}
+ private notify(title: string, body: string): void {
+ if (testKnobs.userDataDir || !Notification.isSupported()) return
+ try {
+ const notification = new Notification({ title, body })
+ notification.on('click', () => {
+ const window = this.getWindow()
+ if (window && !window.isDestroyed()) {
+ if (window.isMinimized()) window.restore()
+ window.show()
+ window.focus()
+ }
+ })
+ notification.show()
+ } catch {
+ // Best-effort notification
+ }
+ }
+
private async runWorker(runtime: DownloadRuntime, chunk: ChunkState): Promise<void> {
const iface =
runtime.activeInterfaces.find((i) => i.id === chunk.interfaceId) ??
@@ -982,6 +1002,7 @@ export class DownloadManager {
if (allErrored && (runtime.state.status as DownloadStatus) === 'downloading') {
runtime.state.status = 'error'
runtime.state.error = message
+ this.notify('Download Failed', `${runtime.state.fileName}: ${message}`)
for (const cr of runtime.chunkRuntimes.values()) {
cr.controller.abort()
}
@@ -1141,7 +1162,10 @@ export class DownloadManager {
)
}
- const output = createWriteStream(runtime.state.destinationPath)
+ const ASSEMBLE_STREAM_BUFFER_BYTES = 1024 * 1024 // 1 MB buffer for fast sequential disk assembly
+ const output = createWriteStream(runtime.state.destinationPath, {
+ highWaterMark: ASSEMBLE_STREAM_BUFFER_BYTES
+ })
let bytesWritten = 0
// Set only for a dev-tool simulated download that asked for a slowed-down assemble — real
@@ -1171,7 +1195,11 @@ export class DownloadManager {
// pipeline rejects on an error from either side; a hand-rolled pause/'drain' loop here
// would wait forever for a 'drain' that an errored output never emits.
- await pipeline(createReadStream(partPath), output, { end: false })
+ await pipeline(
+ createReadStream(partPath, { highWaterMark: ASSEMBLE_STREAM_BUFFER_BYTES }),
+ output,
+ { end: false }
+ )
if (outputErrors.length > 0) throw outputErrors[0]
bytesWritten += actualBytes