Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
fix chunking and parallel download basics
Anmol Kapilb0c0bc61923e
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 8f73de8..6f49955 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -59,22 +59,6 @@ function pushSpeedSample(samples: SpeedSample[], bytes: number, time: number): nreturn deltaSeconds > 0 ? (bytes - oldest.bytes) / deltaSeconds : 0}-function splitIntoRanges(- totalBytes: number,- count: number-): Array<{ start: number; end: number | null }> {- const ranges: Array<{ start: number; end: number | null }> = []- const baseSize = Math.floor(totalBytes / count)- let start = 0- for (let index = 0; index < count; index += 1) {- const isLast = index === count - 1- const end = isLast ? totalBytes - 1 : start + baseSize - 1- ranges.push({ start, end })- start = end + 1- }- return ranges-}-// Defensive cap independent of whatever the renderer sends — chunks are// distributed round-robin across interfaces, not tied 1:1 to them anymore.const MAX_CHUNKS = 32@@ -197,30 +181,73 @@ export class DownloadManager {requestPayload.suggestedFileName)- const chunkCount = Math.max(1, Math.min(requestPayload.chunkCount, MAX_CHUNKS))+ const connectionsPerNetwork = Math.max(+ 1,+ Math.min(+ 8,+ requestPayload.connectionsPerNetwork ??+ Math.max(1, Math.round(requestPayload.chunkCount / interfaces.length))+ )+ )const canSplit =- requestPayload.supportsRanges && requestPayload.totalBytes > 0 && chunkCount > 1- const ranges = canSplit- ? splitIntoRanges(requestPayload.totalBytes, chunkCount)- : [{ start: 0, end: requestPayload.totalBytes > 0 ? requestPayload.totalBytes - 1 : null }]- // Round-robin: chunks no longer have to match interfaces 1:1, so a single- // physical network can carry more than one chunk's worth of connections.- const activeInterfaces = canSplit- ? Array.from({ length: chunkCount }, (_, index) => interfaces[index % interfaces.length])- : [interfaces[0]]-- const chunks: ChunkState[] = ranges.map((range, index) => ({- id: index,- interfaceId: activeInterfaces[index].id,- interfaceLabel: activeInterfaces[index].displayName,- interfaceKind: activeInterfaces[index].kind,- rangeStart: range.start,- rangeEnd: range.end,- bytesDownloaded: 0,- speedBytesPerSec: 0,- status: 'pending',- retryCount: 0- }))+ requestPayload.supportsRanges &&+ requestPayload.totalBytes > 0 &&+ (interfaces.length > 1 || connectionsPerNetwork > 1)++ const chunks: ChunkState[] = []+ const activeInterfaces: NetworkInterfaceInfo[] = []++ if (canSplit) {+ const netCount = interfaces.length+ let chunkIdCounter = 0++ for (let netIdx = 0; netIdx < netCount; netIdx++) {+ const iface = interfaces[netIdx]+ const netStart = Math.floor((netIdx * requestPayload.totalBytes) / netCount)+ const netEnd =+ netIdx === netCount - 1+ ? requestPayload.totalBytes - 1+ : Math.floor(((netIdx + 1) * requestPayload.totalBytes) / netCount) - 1+ const netSize = netEnd - netStart + 1++ for (let connIdx = 0; connIdx < connectionsPerNetwork; connIdx++) {+ if (chunks.length >= MAX_CHUNKS) break+ const chunkStart = netStart + Math.floor((connIdx * netSize) / connectionsPerNetwork)+ const chunkEnd =+ connIdx === connectionsPerNetwork - 1+ ? netEnd+ : netStart + Math.floor(((connIdx + 1) * netSize) / connectionsPerNetwork) - 1++ activeInterfaces.push(iface)+ chunks.push({+ id: chunkIdCounter++,+ interfaceId: iface.id,+ interfaceLabel: iface.displayName,+ interfaceKind: iface.kind,+ rangeStart: chunkStart,+ rangeEnd: chunkEnd,+ bytesDownloaded: 0,+ speedBytesPerSec: 0,+ status: 'pending',+ retryCount: 0+ })+ }+ }+ } else {+ activeInterfaces.push(interfaces[0])+ chunks.push({+ id: 0,+ interfaceId: interfaces[0].id,+ interfaceLabel: interfaces[0].displayName,+ interfaceKind: interfaces[0].kind,+ rangeStart: 0,+ rangeEnd: requestPayload.totalBytes > 0 ? requestPayload.totalBytes - 1 : null,+ bytesDownloaded: 0,+ speedBytesPerSec: 0,+ status: 'pending',+ retryCount: 0+ })+ }const state: DownloadState = {id,diff --git a/src/renderer/src/components/NetworkRow.tsx b/src/renderer/src/components/NetworkRow.tsxindex 62eb401..bb54cd8 100644--- a/src/renderer/src/components/NetworkRow.tsx+++ b/src/renderer/src/components/NetworkRow.tsx@@ -1,4 +1,3 @@-import type { ChunkStatus } from '@shared/types'import { useState } from 'react'import { useAppStore } from '../store/useAppStore'import {@@ -13,14 +12,6 @@ import type { NetworkGroup } from '../utils/format'import { formatBytes, formatSpeed } from '../utils/format'import { NetworkEditorFields } from './NetworkEditorFields'-/** A chunk still filling in fades up from a floor opacity rather than snapping straight to- * full color, so its own progress reads at a glance within the network's mini chunk bar. */-function chunkOpacity(percent: number, status: ChunkStatus): number {- if (status === 'completed') return 1- if (status === 'pending') return 0.16- return Math.max(0.3, Math.min(1, percent / 100))-}-export function NetworkRow({group,totalBytes,@@ -33,6 +24,7 @@ export function NetworkRow({latencyMs?: number | null}): React.JSX.Element {const [editing, setEditing] = useState(false)+ const [expanded, setExpanded] = useState(true)const preference = useAppStore((store) => store.networkPreferences[group.interfaceId])const setNetworkPreference = useAppStore((store) => store.setNetworkPreference)const visual = resolveNetworkVisual(group.interfaceKind, group.interfaceLabel, preference)@@ -73,11 +65,28 @@ export function NetworkRow({>{visual.name}</div>- <div- style={{ marginTop: 3, font: `10px/1 ${FONT_MONO}`, color: 'var(--text-tertiary)' }}+ <button+ type="button"+ onClick={() => setExpanded((v) => !v)}+ title={expanded ? 'Collapse stream details' : 'Expand stream details'}+ style={{+ marginTop: 3,+ display: 'inline-flex',+ alignItems: 'center',+ gap: 4,+ border: 'none',+ background: 'none',+ padding: 0,+ font: `500 10px/1 ${FONT_MONO}`,+ color: 'var(--text-tertiary)',+ cursor: 'pointer'+ }}>- {group.chunks.length} chunk{group.chunks.length === 1 ? '' : 's'}- </div>+ <span>+ {group.chunks.length} parallel stream{group.chunks.length === 1 ? '' : 's'}+ </span>+ <span style={{ fontSize: 8 }}>{expanded ? '▲' : '▼'}</span>+ </button></div><buttontype="button"@@ -96,23 +105,45 @@ export function NetworkRow({⋯</button></div>- <div style={{ display: 'flex', gap: 3, height: 15 }}>- {group.chunks.map((chunk) => {+ <div style={{ display: 'flex', gap: 4, height: 16 }}>+ {group.chunks.map((chunk, index) => {const chunkSize =chunk.rangeEnd !== null? chunk.rangeEnd - chunk.rangeStart + 1: totalBytes - chunk.rangeStart- const percent = chunkSize > 0 ? (chunk.bytesDownloaded / chunkSize) * 100 : 0+ const percent =+ chunkSize > 0 ? Math.min(100, (chunk.bytesDownloaded / chunkSize) * 100) : 0+ const isChunkDone = chunk.status === 'completed'+ const isChunkActive = chunk.status === 'downloading'+ const isChunkError = chunk.status === 'error'+ const title = `Stream #${index + 1}: ${Math.round(percent)}% · ${formatBytes(chunk.bytesDownloaded)} / ${formatBytes(chunkSize)}${chunk.speedBytesPerSec > 0 ? ` · ${formatSpeed(chunk.speedBytesPerSec)}` : ''}`+return (<divkey={chunk.id}+ title={title}style={{flex: 1,- borderRadius: 2,- background: chunk.status === 'error' ? DANGER : visual.solid,- opacity: chunkOpacity(percent, chunk.status)+ borderRadius: 3,+ background: 'var(--track-bg)',+ border: '0.5px solid var(--border-strong)',+ overflow: 'hidden',+ position: 'relative',+ display: 'flex',+ alignItems: 'center'}}- />+ >+ <div+ style={{+ height: '100%',+ width: isChunkDone ? '100%' : `${percent}%`,+ background: isChunkError ? DANGER : visual.solid,+ opacity: isChunkDone ? 1 : isChunkActive ? 0.9 : 0.45,+ transition: 'width 0.2s ease-out',+ borderRadius: 2+ }}+ />+ </div>)})}</div>@@ -156,6 +187,138 @@ export function NetworkRow({{latencyMs != null ? `${latencyMs} ms` : '—'}</div></div>+ {expanded && group.chunks.length > 0 && (+ <div+ style={{+ padding: '6px 20px 10px 42px',+ display: 'flex',+ flexDirection: 'column',+ gap: 6,+ background: 'var(--bg-secondary)',+ borderTop: '0.5px solid var(--border-subtle)',+ borderBottom: '0.5px solid var(--border-subtle)'+ }}+ >+ {group.chunks.map((chunk, index) => {+ const chunkSize =+ chunk.rangeEnd !== null+ ? chunk.rangeEnd - chunk.rangeStart + 1+ : totalBytes - chunk.rangeStart+ const percent =+ chunkSize > 0 ? Math.min(100, (chunk.bytesDownloaded / chunkSize) * 100) : 0+ const isChunkActive = chunk.status === 'downloading'+ const isChunkDone = chunk.status === 'completed'+ const isChunkError = chunk.status === 'error'++ const rangeText =+ chunk.rangeEnd !== null+ ? `${formatBytes(chunk.rangeStart)} – ${formatBytes(chunk.rangeEnd)}`+ : `${formatBytes(chunk.rangeStart)}+`++ return (+ <div+ key={chunk.id}+ style={{+ display: 'grid',+ gridTemplateColumns: '110px 1fr 52px 82px 140px',+ gap: 12,+ alignItems: 'center',+ padding: '3px 0',+ font: `11px/1.2 ${FONT_MONO}`+ }}+ >+ <div style={{ display: 'flex', alignItems: 'center', gap: 6, minWidth: 0 }}>+ <div+ style={{+ width: 5,+ height: 5,+ borderRadius: '50%',+ background: isChunkError+ ? DANGER+ : isChunkDone+ ? visual.solid+ : isChunkActive+ ? visual.solid+ : 'var(--icon-muted)',+ opacity: isChunkActive || isChunkDone ? 1 : 0.4+ }}+ />+ <span+ style={{+ color: 'var(--text)',+ fontWeight: 600,+ whiteSpace: 'nowrap'+ }}+ >+ Stream #{index + 1}+ </span>+ </div>++ <div+ style={{+ height: 6,+ borderRadius: 999,+ background: 'var(--track-bg)',+ overflow: 'hidden',+ position: 'relative'+ }}+ >+ <div+ style={{+ height: '100%',+ width: isChunkDone ? '100%' : `${percent}%`,+ background: isChunkError ? DANGER : visual.solid,+ borderRadius: 999,+ transition: 'width 0.2s ease-out'+ }}+ />+ </div>++ <div+ style={{+ textAlign: 'right',+ color: isChunkDone ? visual.text : 'var(--text)',+ fontVariantNumeric: 'tabular-nums',+ fontWeight: 500+ }}+ >+ {Math.round(percent)}%+ </div>++ <div+ style={{+ textAlign: 'right',+ color: isChunkActive ? visual.text : 'var(--text-tertiary)',+ fontVariantNumeric: 'tabular-nums',+ fontWeight: 500+ }}+ >+ {isChunkActive+ ? formatSpeed(chunk.speedBytesPerSec)+ : isChunkDone+ ? 'Done'+ : chunk.status === 'retrying'+ ? 'Retrying…'+ : 'Waiting'}+ </div>++ <div+ style={{+ textAlign: 'right',+ color: 'var(--text-secondary)',+ fontVariantNumeric: 'tabular-nums',+ fontSize: '10.5px',+ whiteSpace: 'nowrap'+ }}+ title={`Byte range: ${rangeText}`}+ >+ {formatBytes(chunk.bytesDownloaded)} / {formatBytes(chunkSize)}+ </div>+ </div>+ )+ })}+ </div>+ )}{editing && (<div style={{ padding: '0 20px 11px' }}><NetworkEditorFieldsdiff --git a/src/renderer/src/screens/IdleScreen.tsx b/src/renderer/src/screens/IdleScreen.tsxindex c1a219e..bfe1cf9 100644--- a/src/renderer/src/screens/IdleScreen.tsx+++ b/src/renderer/src/screens/IdleScreen.tsx@@ -21,8 +21,7 @@ type ProbeState =| { status: 'error'; message: string }const PROBE_DEBOUNCE_MS = 600-const MIN_CHUNKS_PER_NETWORK = 1-const MAX_CHUNKS_PER_NETWORK = 8+const PRESET_STREAMS = [1, 2, 4, 8] as constconst fieldLabelStyle: React.CSSProperties = {font: `500 10px/1 ${FONT_MONO}`,@@ -31,15 +30,6 @@ const fieldLabelStyle: React.CSSProperties = {flexShrink: 0}-const stepperArrowStyle: React.CSSProperties = {- border: 'none',- background: 'none',- color: 'var(--text-secondary)',- font: `600 13px/1 ${FONT_UI}`,- cursor: 'pointer',- padding: '0 2px'-}-export function IdleScreen(): React.JSX.Element {useNetworkPolling(true)@@ -54,8 +44,8 @@ export function IdleScreen(): React.JSX.Element {const setDestinationDir = useAppStore((store) => store.setDraftDestinationDir)const [probe, setProbe] = useState<ProbeState>({ status: 'idle' })- const [selectedInterfaceIds, setSelectedInterfaceIds] = useState<string[]>([])- const [chunksPerNetwork, setChunksPerNetwork] = useState(1)+ const [deselectedInterfaceIds, setDeselectedInterfaceIds] = useState<string[]>([])+ const [chunksPerNetwork, setChunksPerNetwork] = useState(2)const [starting, setStarting] = useState(false)const [startError, setStartError] = useState<string | null>(null)@@ -86,10 +76,6 @@ export function IdleScreen(): React.JSX.Element {if (probeRequestId.current !== requestId) returnconst multiChunkAllowed = result.supportsRanges && result.totalBytes !== nullsetProbe({ status: 'ready', result, multiChunkAllowed })- setSelectedInterfaceIds(- (multiChunkAllowed ? interfaces : interfaces.slice(0, 1)).map((iface) => iface.id)- )- setChunksPerNetwork(1)} catch (error) {if (probeRequestId.current !== requestId) returnsetProbe({ status: 'error', message: describeError(error) })@@ -97,16 +83,33 @@ export function IdleScreen(): React.JSX.Element {}, PROBE_DEBOUNCE_MS)return () => clearTimeout(timer)- // interfaces is intentionally omitted: we only want this to re-run when the URL changes,- // using whatever interface list is current at the moment the probe resolves.- // eslint-disable-next-line react-hooks/exhaustive-deps}, [url])+ const isSingleRangeServer = probe.status === 'ready' && !probe.multiChunkAllowed++ const activeDetectedIds = interfaces.map((iface) => iface.id)+ const rawSelectedIds = activeDetectedIds.filter((id) => !deselectedInterfaceIds.includes(id))+ const selectedInterfaceIds = isSingleRangeServer ? rawSelectedIds.slice(0, 1) : rawSelectedIds+const handleToggleInterface = (id: string): void => {- const multiChunkAllowed = probe.status === 'ready' && probe.multiChunkAllowed- setSelectedInterfaceIds((prev) => {- if (!multiChunkAllowed) return [id]- return prev.includes(id) ? prev.filter((entry) => entry !== id) : [...prev, id]+ if (isSingleRangeServer) {+ // Single-range servers can only download through 1 interface at a time+ setDeselectedInterfaceIds(activeDetectedIds.filter((otherId) => otherId !== id))+ return+ }++ setDeselectedInterfaceIds((prev) => {+ const isCurrentlySelected = !prev.includes(id)+ if (isCurrentlySelected) {+ // Deselecting: keep at least 1 interface selected+ const remainingCount = activeDetectedIds.filter(+ (otherId) => !prev.includes(otherId) && otherId !== id+ ).length+ if (remainingCount === 0) return prev+ return [...prev, id]+ } else {+ return prev.filter((entry) => entry !== id)+ }})}@@ -133,6 +136,7 @@ export function IdleScreen(): React.JSX.Element {supportsRanges: probe.multiChunkAllowed,interfaceIds: selectedInterfaceIds,chunkCount: probe.multiChunkAllowed ? selectedInterfaceIds.length * chunksPerNetwork : 1,+ connectionsPerNetwork: probe.multiChunkAllowed ? chunksPerNetwork : 1,etag: probe.result.etag,lastModified: probe.result.lastModified})@@ -148,8 +152,8 @@ export function IdleScreen(): React.JSX.Element {selectedInterfaceIds.length > 0 &&Boolean(destinationDir) &&!starting- const multiChunkAllowed = probe.status === 'ready' && probe.multiChunkAllowed- const totalChunks = multiChunkAllowed ? selectedInterfaceIds.length * chunksPerNetwork : 1+ const effectiveNetworkCount = Math.max(1, selectedInterfaceIds.length)+ const totalChunks = isSingleRangeServer ? 1 : effectiveNetworkCount * chunksPerNetworkreturn (<div@@ -210,7 +214,7 @@ export function IdleScreen(): React.JSX.Element {disabled={!canStart}style={canStart ? primaryButtonStyle : disabledPrimaryButtonStyle}>- {starting ? 'Starting…' : 'Start'}+ {starting ? 'Starting…' : probe.status === 'probing' ? 'Checking…' : 'Start'}</button></div>@@ -258,57 +262,82 @@ export function IdleScreen(): React.JSX.Element {Browse…</button></div>+ </div>++ <div+ style={{+ display: 'flex',+ alignItems: 'center',+ justifyContent: 'space-between',+ gap: 12,+ padding: '8px 12px',+ borderRadius: 9,+ background: 'var(--bg-secondary)',+ border: '0.5px solid var(--border)',+ opacity: isSingleRangeServer ? 0.6 : 1+ }}+ >+ <div style={{ display: 'flex', alignItems: 'center', gap: 10, flexWrap: 'wrap' }}>+ <div style={fieldLabelStyle}>PARALLEL STREAMS</div>+ <div style={{ display: 'flex', gap: 4 }}>+ {PRESET_STREAMS.map((preset) => {+ const isSelected = chunksPerNetwork === preset+ return (+ <button+ key={preset}+ type="button"+ disabled={isSingleRangeServer}+ onClick={() => setChunksPerNetwork(preset)}+ style={{+ border: isSelected+ ? '0.5px solid var(--color-accent)'+ : '0.5px solid var(--border)',+ borderRadius: 5,+ background: isSelected ? 'var(--color-usb-bg)' : 'var(--track-bg)',+ color: isSelected ? 'var(--color-usb-text)' : 'var(--text-secondary)',+ font: `600 10.5px/1 ${FONT_MONO}`,+ padding: '4px 8px',+ cursor: isSingleRangeServer ? 'not-allowed' : 'pointer',+ transition: 'all 0.15s ease'+ }}+ >+ {preset}×+ </button>+ )+ })}+ </div>+ </div>+<divstyle={{- width: 122,- flexShrink: 0,- display: 'flex',- alignItems: 'center',- justifyContent: 'center',- gap: 8,- padding: '7px 0',- borderRadius: 9,- background: 'var(--bg-secondary)',- border: '0.5px solid var(--border)',- opacity: multiChunkAllowed ? 1 : 0.5+ font: `500 11px/1 ${FONT_MONO}`,+ color: isSingleRangeServer ? 'var(--text-tertiary)' : 'var(--text-secondary)',+ textAlign: 'right',+ whiteSpace: 'nowrap'}}>- <button- type="button"- onClick={() =>- setChunksPerNetwork((count) => Math.max(MIN_CHUNKS_PER_NETWORK, count - 1))- }- disabled={!multiChunkAllowed || chunksPerNetwork <= MIN_CHUNKS_PER_NETWORK}- style={stepperArrowStyle}- >- −- </button>- <div- style={{- font: `11px/1 ${FONT_MONO}`,- color: 'var(--text-secondary)',- whiteSpace: 'nowrap'- }}- >- {totalChunks} chunk{totalChunks === 1 ? '' : 's'}- </div>- <button- type="button"- onClick={() =>- setChunksPerNetwork((count) => Math.min(MAX_CHUNKS_PER_NETWORK, count + 1))- }- disabled={!multiChunkAllowed || chunksPerNetwork >= MAX_CHUNKS_PER_NETWORK}- style={stepperArrowStyle}- >- +- </button>+ {isSingleRangeServer ? (+ '1 stream (server does not support ranges)'+ ) : selectedInterfaceIds.length > 0 ? (+ <>+ <span style={{ color: 'var(--text)', fontWeight: 600 }}>{chunksPerNetwork}</span> /+ network ·{' '}+ <span style={{ color: 'var(--text)', fontWeight: 600 }}>{totalChunks}</span> total+ parallel streams+ </>+ ) : (+ <>+ <span style={{ color: 'var(--text)', fontWeight: 600 }}>{chunksPerNetwork}</span> /+ network+ </>+ )}</div></div>{probe.status === 'error' && (<div style={{ font: `12px/1.4 ${FONT_UI}`, color: DANGER }}>{probe.message}</div>)}- {probe.status === 'ready' && !probe.multiChunkAllowed && (+ {isSingleRangeServer && (<div style={{ font: `11.5px/1.4 ${FONT_UI}`, color: 'var(--text-tertiary)' }}>This server doesn't support multi-chunk downloads for this file — using a singlenetwork.@@ -368,6 +397,9 @@ export function IdleScreen(): React.JSX.Element {<div style={{ font: `11px/1.4 ${FONT_MONO}`, color: 'var(--text-tertiary)' }}>{selectedInterfaceIds.length} {selectedInterfaceIds.length === 1 ? 'network' : 'networks'}{' '}selected+ {selectedInterfaceIds.length > 0+ ? ` · ${totalChunks} ${totalChunks === 1 ? 'stream' : 'parallel streams'}`+ : ''}{probe.status === 'ready' && probe.result.totalBytes !== null? ` · ${formatBytes(probe.result.totalBytes)}`: ''}diff --git a/src/shared/types.ts b/src/shared/types.tsindex c21af73..181f94a 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -81,8 +81,10 @@ export interface StartDownloadRequest {totalBytes: numbersupportsRanges: booleaninterfaceIds: string[]- /** Total chunks to split the download into, distributed round-robin across interfaceIds. */+ /** Total chunks to split the download into across interfaceIds. */chunkCount: number+ /** Number of parallel connections allocated per physical network. */+ connectionsPerNetwork?: numberetag: string | nulllastModified: string | null}