Windows 上的安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行,自持 QEMU 运行时
fix: Gitee 匿名限流时自动熔断 + 版本结果缓存,不再每次启动都撞墙
RoutineMikuee11af0d83eb
diff --git a/src/core/sources.js b/src/core/sources.jsindex 8952dcc..39deb6d 100644--- a/src/core/sources.js+++ b/src/core/sources.js@@ -214,6 +214,20 @@ class Sources {this.settings = typeof settingsFn === 'function' ? settingsFn : () => ({});this._rankCache = new Map(); // url -> {at, speed, ok}this._rankTtl = 30 * 60 * 1000;+ // 版本检查结果缓存:Gitee 的匿名调用限流很紧(连续几次就 403),+ // 而「有没有新版本」本来也不需要秒级新鲜度。+ this._relCache = { at: 0, key: '', value: null };+ this._relTtl = 10 * 60 * 1000;+ // 熔断:某个源被限流/报错后,一段时间内直接跳过它,不再每次启动都撞墙+ this._blocked = new Map(); // sourceId -> until(ms)+ this._blockMs = 15 * 60 * 1000;+ }++ /** 该源是否处于熔断期;返回 null 表示可用,否则返回给用户看的原因 */+ blockedReason(id) {+ const until = this._blocked.get(id) || 0;+ if (until <= Date.now()) return null;+ return `暂时跳过(${Math.ceil((until - Date.now()) / 60000)} 分钟内不再尝试)`;}/** 各源的最新 release(并发,任何一个失败都不影响另一个) */@@ -230,6 +244,8 @@ class Sources {const jobs = ids.map(async (id) => {const s = SOURCES[id];+ const skip = this.blockedReason(id);+ if (skip) return { source: id, name: s.name, web: s.web, error: skip };try {const rel = await getJson(withToken(id, `${s.api}/releases/latest`), 9000);return { source: id, name: s.name, web: s.web, rel };@@ -240,9 +256,11 @@ class Sources {const first = (Array.isArray(list) ? list : []).find((r) => r && !r.draft);if (first) return { source: id, name: s.name, web: s.web, rel: first };} catch (_) { /* 这个源彻底不可用 */ }- const hint = id === 'gitee' && !token- ? '(若是私有仓库请填 token;公开仓库通常只是被限流,稍后重试即可)'- : '';+ const limited = /\b(403|429)\b/.test(e.message);+ if (limited) this._blocked.set(id, Date.now() + this._blockMs);+ const hint = limited+ ? '(被限流,已暂停使用该源一段时间)'+ : (id === 'gitee' && !token ? '(可稍后重试,或在设置里填只读 token 提高额度)' : '');return { source: id, name: s.name, web: s.web, error: `${e.message}${hint}` };}});@@ -253,11 +271,26 @@ class Sources {* 合并两边的最新版本:版本号更高的胜出,但**资产列表取并集**,* 这样即使 Gitee 只放了元数据、资产在 GitHub,也能两条路都试。*/- async latest() {+ async latest({ force = false } = {}) {+ const pref = String((this.settings().runtime || {}).sourcePreference || 'auto');+ const cached = this._relCache;+ if (!force && cached.value && cached.key === pref && Date.now() - cached.at < this._relTtl) {+ return cached.value;+ }const all = await this.fetchReleases();+ // 被限流的源记入熔断(放在这里而不是 fetchReleases 内部,是为了让「谁来提供+ // 结果」这件事可被替换/测试,同时保证任何入口拿到的 403 都会触发熔断)+ for (const x of all) {+ if (x && x.error && /\b(403|429)\b/.test(x.error)) {+ this._blocked.set(x.source, Date.now() + this._blockMs);+ }+ }const ok = all.filter((x) => x && x.rel);if (!ok.length) {const why = all.map((x) => `${x.name || x.source}: ${x.error || '无数据'}`).join(';');+ // 全挂了但手里有上一次的结果:宁可给一份「可能是旧的」版本信息,+ // 也不要让界面直接报错 —— 用户至少还能看到当前版本和发布页入口。+ if (cached.value) return { ...cached.value, stale: true, staleReason: why };throw new Error(`连不上更新服务器(${why})`);}ok.sort((a, b) => cmpVer(b.rel.tag_name || b.rel.name, a.rel.tag_name || a.rel.name));@@ -297,7 +330,7 @@ class Sources {assets.push(a);}- return {+ const result = {version,tag,name: top.rel.name || tag,@@ -309,6 +342,8 @@ class Sources {unreachable: all.filter((x) => x && x.error).map((x) => ({ source: x.source, name: x.name, error: x.error })),bySource: top.source,};+ this._relCache = { at: Date.now(), key: pref, value: result };+ return result;}/** 某个资产在所有源 + 镜像下的候选地址(去重、保序:源站优先,其次镜像) */@@ -365,6 +400,8 @@ class Sources {/** 测速缓存清空(用户改了镜像前缀 / 换了网络后调用) */reset() {this._rankCache.clear();+ this._relCache = { at: 0, key: '', value: null };+ this._blocked.clear();}}diff --git a/src/core/updater.js b/src/core/updater.jsindex 8e0e2de..44c5193 100644--- a/src/core/updater.js+++ b/src/core/updater.js@@ -66,8 +66,12 @@ class Updater {* 检查更新:GitHub / Gitee 并发问,取版本号更高的,资产取并集。* 具体下载地址在 prepare() 里再做一次真实测速排序。*/- async check() {- const merged = await this.sources.latest();+ /**+ * @param {{force?:boolean}} [opts] force=true 时跳过 10 分钟的版本缓存+ * (用户主动点「检查更新」应该拿到最新结果;启动时的静默检查走缓存)+ */+ async check(opts = {}) {+ const merged = await this.sources.latest({ force: !!opts.force });const pick = (re) => merged.assets.find((a) => re.test(String(a.name || '')));const wantSetup = this.flavor === 'setup';diff --git a/src/main.js b/src/main.jsindex 815506c..0bd0a76 100644--- a/src/main.js+++ b/src/main.js@@ -1440,7 +1440,8 @@ function registerIpc() {ipcMain.handle('app:updateCheck', async (_e, opts = {}) => {try {- const info = await updater.check();+ // 用户主动点「检查更新」→ 跳过缓存,必定重新问一次两个源+ const info = await updater.check({ force: opts.force !== false });updateInfo = info;return { ok: true, ...info };} catch (e) {diff --git a/test/auto-test.js b/test/auto-test.jsindex 76e1711..25c72e2 100644--- a/test/auto-test.js+++ b/test/auto-test.js@@ -1002,6 +1002,63 @@ const { AdbClient, friendlyInstallError, friendlyLaunchError } = require('../src}});+ await test('双源:版本结果缓存 10 分钟,force 时强制重查', async () => {+ const { Sources } = require('../src/core/sources');+ const s = new Sources(() => ({ runtime: {} }));+ let calls = 0;+ s.fetchReleases = async () => {+ calls += 1;+ return [{ source: 'github', name: 'GitHub', web: 'g', rel: { tag_name: 'v1.0.0', assets: [] } }];+ };+ await s.latest();+ await s.latest();+ assert(calls === 1, `缓存期内不应重复请求,实际请求 ${calls} 次`);+ await s.latest({ force: true });+ assert(calls === 2, 'force 必须绕过缓存');+ s.reset();+ await s.latest();+ assert(calls === 3, 'reset 后应重新请求');+ });++ await test('双源:某个源 403 被限流后熔断跳过,不再每轮撞墙', async () => {+ const { Sources } = require('../src/core/sources');+ const s = new Sources(() => ({ runtime: {} }));+ let giteeCalls = 0;+ s.fetchReleases = async () => {+ giteeCalls += 1;+ return [+ { source: 'github', name: 'GitHub', web: 'g', rel: { tag_name: 'v1.0.0', assets: [] } },+ { source: 'gitee', name: 'Gitee', web: 'e', error: 'gitee.com 403 (被限流)' },+ ];+ };+ // 第一次:Gitee 报 403,被记入熔断+ await s.latest({ force: true });+ assert(giteeCalls === 1, '第一轮应真实请求过');+ const reason = s.blockedReason('gitee');+ assert(!!reason, '403 必须触发熔断');+ assert(s.blockedReason('github') === null, '没出错的源不应被熔断');+ // 熔断期内的请求应被直接跳过(不再打网络)+ assert(/暂时跳过/.test(reason), `跳过原因要能看懂,实际:${reason}`);+ s.reset();+ assert(s.blockedReason('gitee') === null, 'reset 后熔断应解除');+ });++ await test('双源:全部源不可达时退回上一次结果(标记 stale,不直接报错)', async () => {+ const { Sources } = require('../src/core/sources');+ const s = new Sources(() => ({ runtime: {} }));+ s.fetchReleases = async () => [+ { source: 'github', name: 'GitHub', web: 'g', rel: { tag_name: 'v1.2.3', assets: [] } },+ ];+ const first = await s.latest();+ assert(first.tag === 'v1.2.3' && !first.stale, '第一次应正常');+ s._relTtl = 0; // 让缓存失效+ s.fetchReleases = async () => [+ { source: 'github', name: 'GitHub', web: 'g', error: '超时' },+ ];+ const second = await s.latest();+ assert(second.tag === 'v1.2.3' && second.stale === true, '应退回旧结果并标记 stale');+ });+console.log('\n[资源策略 / 磁盘分配 / 日志上限]');await test('资源策略一处切换,自动翻译成 vCPU/优先级/分辨率', () => {