feat(download): 走系统网络栈(Rust)+并发默认30可调;修并发限制从未生效
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 48s

This commit is contained in:
qiqi committed 2026-12-03 17:29:48 +08:00
1 parent 3894c697ac
commit 2ff1ccd2d1
8 files changed
+123 -12

No files matched your search

+21 -3
View File
@@ -28,7 +28,11 @@
function create (deps) {
const d = deps || {}
const concurrency = Math.max(1, Number(d.concurrency) || 2)
// 2026-12-03(用户裁定):默认 30 并发,设置页可调(1–64);getConcurrency 让改动立即生效。
const concurrencyOf = () => {
const v = (typeof d.getConcurrency === 'function') ? d.getConcurrency() : d.concurrency
return Math.max(1, Math.min(64, Number(v) || 30))
}
const maxRetries = Math.max(0, Number(d.retries) || 3)
const now = typeof d.now === 'function' ? d.now : () => Date.now()
const sleep = typeof d.sleep === 'function' ? d.sleep : (ms) => new Promise((r) => setTimeout(r, ms))
@@ -83,7 +87,8 @@
}
async function runOne (e) {
active++
// active 由 pump 在「决定启动」时同步 +1(见 pump):放在这里会晚一个微任务,
// pump 的 while 就会在 active 还是 0 时把整个队列一次性放出去(2026-12-03 实测并发限制从未生效)。
setStatus(e, STATE.DOWNLOADING, { lastError: '', progress: 0, receivedBytes: 0, totalBytes: 0 })
let lastErr = ''
// 2026-12-02:下载进度(有 content-length 才算百分比;拿不到就退回「下载中」文案)。
@@ -135,10 +140,11 @@
}
function pump () {
while (active < concurrency && queue.length) {
while (active < concurrencyOf() && queue.length) {
const id = queue.shift()
const e = entries.get(id)
if (!e || e.status !== STATE.QUEUED) continue
active++
Promise.resolve().then(() => runOne(e))
}
}
@@ -295,6 +301,18 @@
const d = deps || {}
const timeoutMs = Math.max(1000, Number(d.timeoutMs) || 300000)
return async function fetchBytes (url, onProgress) {
// 2026-12-03(用户裁定):优先用系统网络栈(Rust reqwest)—— 不受 CORS/WebView 限制,
// 图床/CDN 对 XHR 的限速、挂起、防盗链都不会再影响下载。拿不到再退回 WebView fetch。
if (typeof d.rustFetch === 'function') {
try {
const r = await d.rustFetch(url)
if (r && r.ok && r.b64) {
const mime = String(r.mime || 'application/octet-stream')
if (typeof onProgress === 'function') onProgress(Number(r.bytes) || 0, Number(r.bytes) || 0)
return { ok: true, dataUrl: 'data:' + mime + ';base64,' + String(r.b64), mime: mime, bytes: Number(r.bytes) || 0 }
}
} catch (eR) { /* 非 Tauri / 命令缺失 → 回退 WebView fetch */ }
}
let timer = null
const job = fetchWithProgress(url, onProgress).catch(async () => {
const blob = await d.blobFromSrc(url)