Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Decouple connection count from selected interfaces

Anmol Kapil547169d704d2
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index f559bb8..5b9834b 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -72,6 +72,10 @@ function splitIntoRanges(
return ranges
}
+// Defensive cap independent of whatever the renderer sends — connections are
+// distributed round-robin across interfaces, not tied 1:1 to them anymore.
+const MAX_CONNECTIONS = 32
+
const MAX_CHUNK_RETRIES = 5
const RETRY_BASE_DELAY_MS = 1000
const RETRY_MAX_DELAY_MS = 15_000
@@ -183,12 +187,17 @@ export class DownloadManager {
requestPayload.suggestedFileName
)
+ const connectionCount = Math.max(1, Math.min(requestPayload.connectionCount, MAX_CONNECTIONS))
const canSplit =
- requestPayload.supportsRanges && requestPayload.totalBytes > 0 && interfaces.length > 1
+ requestPayload.supportsRanges && requestPayload.totalBytes > 0 && connectionCount > 1
const ranges = canSplit
- ? splitIntoRanges(requestPayload.totalBytes, interfaces.length)
+ ? splitIntoRanges(requestPayload.totalBytes, connectionCount)
: [{ start: 0, end: requestPayload.totalBytes > 0 ? requestPayload.totalBytes - 1 : null }]
- const activeInterfaces = canSplit ? interfaces : [interfaces[0]]
+ // Round-robin: connections no longer have to match interfaces 1:1, so an
+ // interface can carry more than one connection's worth of chunks.
+ const activeInterfaces = canSplit
+ ? Array.from({ length: connectionCount }, (_, index) => interfaces[index % interfaces.length])
+ : [interfaces[0]]
const chunks: ChunkState[] = ranges.map((range, index) => ({
id: index,
diff --git a/src/renderer/src/screens/IdleScreen.tsx b/src/renderer/src/screens/IdleScreen.tsx
index d21c083..1f0d015 100644
--- a/src/renderer/src/screens/IdleScreen.tsx
+++ b/src/renderer/src/screens/IdleScreen.tsx
@@ -22,6 +22,19 @@ type ProbeState =
| { status: 'error'; message: string }
const PROBE_DEBOUNCE_MS = 600
+const MIN_CONNECTIONS_PER_INTERFACE = 1
+const MAX_CONNECTIONS_PER_INTERFACE = 8
+
+const stepperButtonStyle: React.CSSProperties = {
+ width: 20,
+ height: 20,
+ borderRadius: 5,
+ border: '0.5px solid #b9b9bb',
+ background: 'linear-gradient(#fefefe, #f3f3f3)',
+ color: '#1d1d1f',
+ font: '13px/1 -apple-system, sans-serif',
+ cursor: 'pointer'
+}
export function IdleScreen(): React.JSX.Element {
useNetworkPolling(true)
@@ -38,6 +51,7 @@ export function IdleScreen(): React.JSX.Element {
const [probe, setProbe] = useState<ProbeState>({ status: 'idle' })
const [selectedInterfaceIds, setSelectedInterfaceIds] = useState<string[]>([])
+ const [connectionsPerInterface, setConnectionsPerInterface] = useState(1)
const [starting, setStarting] = useState(false)
const [startError, setStartError] = useState<string | null>(null)
@@ -71,6 +85,7 @@ export function IdleScreen(): React.JSX.Element {
setSelectedInterfaceIds(
(multiConnectionAllowed ? interfaces : interfaces.slice(0, 1)).map((iface) => iface.id)
)
+ setConnectionsPerInterface(1)
} catch (error) {
if (probeRequestId.current !== requestId) return
setProbe({
@@ -116,6 +131,9 @@ export function IdleScreen(): React.JSX.Element {
totalBytes: probe.result.totalBytes ?? 0,
supportsRanges: probe.multiConnectionAllowed,
interfaceIds: selectedInterfaceIds,
+ connectionCount: probe.multiConnectionAllowed
+ ? selectedInterfaceIds.length * connectionsPerInterface
+ : 1,
etag: probe.result.etag,
lastModified: probe.result.lastModified
})
@@ -185,6 +203,64 @@ export function IdleScreen(): React.JSX.Element {
))}
</div>
+ {probe.status === 'ready' &&
+ probe.multiConnectionAllowed &&
+ selectedInterfaceIds.length > 0 && (
+ <div
+ style={{
+ display: 'flex',
+ alignItems: 'center',
+ gap: 10,
+ padding: '10px 20px',
+ borderTop: '0.5px solid #ececee'
+ }}
+ >
+ <div style={{ font: '12.5px/1 -apple-system, sans-serif', color: '#1d1d1f' }}>
+ Connections per link
+ </div>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
+ <button
+ type="button"
+ onClick={() =>
+ setConnectionsPerInterface((count) =>
+ Math.max(MIN_CONNECTIONS_PER_INTERFACE, count - 1)
+ )
+ }
+ disabled={connectionsPerInterface <= MIN_CONNECTIONS_PER_INTERFACE}
+ style={stepperButtonStyle}
+ >
+ −
+ </button>
+ <div
+ style={{
+ font: `12.5px/1 ${'"SF Mono", ui-monospace, Menlo, monospace'}`,
+ color: '#1d1d1f',
+ width: 14,
+ textAlign: 'center'
+ }}
+ >
+ {connectionsPerInterface}
+ </div>
+ <button
+ type="button"
+ onClick={() =>
+ setConnectionsPerInterface((count) =>
+ Math.min(MAX_CONNECTIONS_PER_INTERFACE, count + 1)
+ )
+ }
+ disabled={connectionsPerInterface >= MAX_CONNECTIONS_PER_INTERFACE}
+ style={stepperButtonStyle}
+ >
+ +
+ </button>
+ </div>
+ <div style={{ flex: 1 }} />
+ <div style={{ font: '11.5px/1.4 -apple-system, sans-serif', color: '#8a8a8e' }}>
+ {selectedInterfaceIds.length * connectionsPerInterface} connections total
+ </div>
+ </div>
+ )}
+
{probe.status === 'ready' && !probe.multiConnectionAllowed && (
<div
style={{
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 72ab304..a35e062 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -27,13 +27,7 @@ export interface ProbeResult {
export type DownloadStatus = 'downloading' | 'paused' | 'completed' | 'error' | 'cancelled'
export type ChunkStatus =
- | 'pending'
- | 'downloading'
- | 'retrying'
- | 'paused'
- | 'completed'
- | 'error'
- | 'cancelled'
+ 'pending' | 'downloading' | 'retrying' | 'paused' | 'completed' | 'error' | 'cancelled'
export interface ChunkState {
id: number
@@ -73,6 +67,8 @@ export interface StartDownloadRequest {
totalBytes: number
supportsRanges: boolean
interfaceIds: string[]
+ /** Total parallel connections to open, distributed round-robin across interfaceIds. */
+ connectionCount: number
etag: string | null
lastModified: string | null
}