Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Networks table: switching off the last network pauses the download

Claude477ca7685655
diff --git a/e2e/liveNetworks.spec.ts b/e2e/liveNetworks.spec.ts
index 7fd5bfb..5ac5aa7 100644
--- a/e2e/liveNetworks.spec.ts
+++ b/e2e/liveNetworks.spec.ts
@@ -93,12 +93,26 @@ test.describe('two networks @smoke', () => {
expect(network(off, 'b')!.bytesDownloaded, 'what it delivered stays its own').toBeGreaterThan(0)
const offAt = origin.log.length
- // The last network in use can't be switched off: pausing is how a download stops.
- await plexo.api.setDownloadNetwork(id, 'a', false)
- expect(network((await plexo.current())!, 'a')?.enabled).toBe(true)
-
await settle(500)
expect(origin.log.slice(offAt).some(overB), 'nothing over b while it is off').toBe(false)
+
+ // Switching off the last network in use pauses the download; switching it back on resumes it.
+ await plexo.api.setDownloadNetwork(id, 'a', false)
+ const paused = await plexo.waitUntil((state) => state.status === 'paused')
+ expect(network(paused, 'a')?.enabled).toBe(false)
+ const pausedAt = origin.log.length
+ await settle(500)
+ expect(origin.log.length, 'nothing requested while every network is off').toBe(pausedAt)
+
+ await plexo.api.setDownloadNetwork(id, 'a', true)
+ await plexo.waitUntil((state) => state.status === 'downloading' && streamsOn(state, 'a') > 0)
+
+ // Resumed with none on, it switches back on the one switched off last.
+ await plexo.api.setDownloadNetwork(id, 'a', false)
+ await plexo.waitUntil((state) => state.status === 'paused')
+ await plexo.api.resumeDownload(id)
+ const resumed = await plexo.waitUntil((state) => state.status === 'downloading')
+ expect(network(resumed, 'a')?.enabled).toBe(true)
})
})
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index 17e9af7..ce19eca 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -179,6 +179,12 @@ interface DownloadRuntime {
sentUpdates: number
/** Each block as the window was last sent it, by index — what tells a changed block apart. */
sentBlocks: Pick<BlockState, 'status' | 'interfaceId' | 'bytesDownloaded'>[]
+ /** The network most recently switched off: what a resume switches back on if it finds none on.
+ * Not persisted: after a restart, the first network present is used instead. */
+ lastSwitchedOff?: string
+ /** Paused by switching off its last network, rather than by Pause: switching one back on
+ * resumes it. */
+ pausedForNoNetwork?: boolean
}
interface PersistedDownloadBase {
@@ -774,6 +780,17 @@ export class DownloadManager {
runtime.state.status = 'downloading'
runtime.state.error = undefined
runtime.state.resumable = undefined
+ runtime.pausedForNoNetwork = undefined
+ // Paused by switching off every network: resuming switches back on the one switched off
+ // last, or, after a restart, the first one present.
+ const { networks } = runtime.state
+ if (!networks.some((network) => network.enabled)) {
+ const again =
+ networks.find((network) => network.id === runtime.lastSwitchedOff) ??
+ networks.find((network) => this.networks.find(network.id)) ??
+ networks[0]
+ if (again) again.enabled = true
+ }
// A network that had failed, or couldn't get through, gets another go.
for (const network of runtime.state.networks) {
if (network.status === 'failed' || network.status === 'unreachable') {
@@ -804,23 +821,30 @@ export class DownloadManager {
runtime.runPromise = this.run(runtime)
}
- /** Switches one of a download's networks on or off, running or paused. The last network in
- * use can't be switched off: pausing is how a download stops. */
- setNetworkEnabled(id: string, networkId: string, enabled: boolean): void {
+ /** Switches one of a download's networks on or off, running or paused. Switching off the last
+ * network in use pauses the download; switching one back on then resumes it. */
+ async setNetworkEnabled(id: string, networkId: string, enabled: boolean): Promise<void> {
const runtime = this.runtimes.get(id)
const status = runtime?.state.status
if (!runtime || (status !== 'downloading' && status !== 'paused')) return
const { networks } = runtime.state
const network = networks.find((entry) => entry.id === networkId)
if (!network || network.enabled === enabled) return
- if (!enabled && !networks.some((other) => other !== network && other.enabled)) return
+ const wasLast = !enabled && !networks.some((other) => other !== network && other.enabled)
// A download that can't be split runs over one network: switching one on switches it over.
if (enabled && !splittable(runtime.requestPayload)) {
for (const other of networks) other.enabled = false
}
network.enabled = enabled
+ if (!enabled) runtime.lastSwitchedOff = network.id
+ if (wasLast && status === 'downloading') {
+ runtime.pausedForNoNetwork = true
+ await this.pause(id)
+ return
+ }
this.reconcile(runtime)
this.pushUpdate(runtime)
+ if (enabled && status === 'paused' && runtime.pausedForNoNetwork) this.resume(id)
}
/** The computer's networks changed (see NetworkMonitor). A network whose addresses changed
diff --git a/src/main/ipc/handlers.ts b/src/main/ipc/handlers.ts
index 9ddb0f1..d6ce336 100644
--- a/src/main/ipc/handlers.ts
+++ b/src/main/ipc/handlers.ts
@@ -165,7 +165,7 @@ export function registerIpcHandlers(getWindow: () => BrowserWindow | null): Down
})
handle('setDownloadNetwork', async (_event, id, networkId, enabled) => {
- manager.setNetworkEnabled(id, networkId, enabled)
+ await manager.setNetworkEnabled(id, networkId, enabled)
})
handle('cancelDownload', async (_event, id) => manager.cancel(id))
diff --git a/src/renderer/src/components/NetworkRow.tsx b/src/renderer/src/components/NetworkRow.tsx
index 8307507..34fa877 100644
--- a/src/renderer/src/components/NetworkRow.tsx
+++ b/src/renderer/src/components/NetworkRow.tsx
@@ -16,8 +16,6 @@ interface NetworkRowProps {
sharePercent: number
totalBytes?: number | null
blocks?: BlockState[]
- /** False for the last network in use, which can't be switched off. */
- canSwitch: boolean
onSwitch: (enabled: boolean) => void
}
@@ -93,7 +91,6 @@ export function NetworkRow({
sharePercent,
totalBytes,
blocks,
- canSwitch,
onSwitch
}: NetworkRowProps): React.JSX.Element {
const [expanded, setExpanded] = useState(false)
@@ -120,31 +117,14 @@ export function NetworkRow({
}}
/>
<div role="cell" className="flex min-w-0 items-center gap-[6px]">
- {/* The wrapper is the trigger: a disabled checkbox gets no hover or focus of its own. */}
- <Tooltip disabled={canSwitch}>
- <TooltipTrigger
- render={
- <span
- tabIndex={canSwitch ? undefined : 0}
- className="flex shrink-0 rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring/50"
- >
- <Checkbox
- checked={group.enabled}
- disabled={!canSwitch}
- onCheckedChange={(checked) => onSwitch(checked)}
- aria-label={`Use ${visual.name}`}
- className="data-checked:border-transparent"
- style={
- group.enabled
- ? { background: visual.solid, color: visual.onSolid }
- : undefined
- }
- />
- </span>
- }
- />
- <TooltipContent>The last network in use stays on. Pause to stop.</TooltipContent>
- </Tooltip>
+ {/* Switching off the last network in use pauses the download. */}
+ <Checkbox
+ checked={group.enabled}
+ onCheckedChange={(checked) => onSwitch(checked)}
+ aria-label={`Use ${visual.name}`}
+ className="shrink-0 data-checked:border-transparent"
+ style={group.enabled ? { background: visual.solid, color: visual.onSolid } : undefined}
+ />
<TruncatedText
text={visual.name}
className={`font-sans text-[12.5px] leading-[1.2] font-semibold ${
diff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsx
index b2e68ef..b2d1f02 100644
--- a/src/renderer/src/screens/DownloadingScreen.tsx
+++ b/src/renderer/src/screens/DownloadingScreen.tsx
@@ -156,7 +156,6 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
// Every network is a row, for switching it on or off; the charts draw only those in play.
const rows = groupByNetwork(download)
const rowVisuals = rows.map((row) => networkVisual(row.id, row.kind, row.label))
- const enabledCount = rows.filter((row) => row.enabled).length
const groups = networksInPlay(rows)
const visuals = groups.map((group) => networkVisual(group.id, group.kind, group.label))
const totalDownloadedByNetworks = rows.reduce((sum, row) => sum + row.bytesDownloaded, 0)
@@ -365,8 +364,6 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
}
totalBytes={download.totalBytes}
blocks={download.blocks}
- // The last network in use stays on: Pause is how a download stops.
- canSwitch={!(row.enabled && enabledCount === 1)}
onSwitch={(enabled) =>
void window.plexo.setDownloadNetwork(download.id, row.id, enabled)
}