Skip to content

重制 GitHub 加速#111

Open
Wind-DeterMination-backup wants to merge 4 commits intoTinyLake:mainfrom
Wind-DeterMination-backup:gh-issue
Open

重制 GitHub 加速#111
Wind-DeterMination-backup wants to merge 4 commits intoTinyLake:mainfrom
Wind-DeterMination-backup:gh-issue

Conversation

@Wind-DeterMination-backup
Copy link
Copy Markdown

@Wind-DeterMination-backup Wind-DeterMination-backup commented Mar 14, 2026

背景

原先的 GitHub 加速只是在 Hooks 里做一次简单的 URL 改写,问题是:

  • 代理源不可配置,无法按资源类型区分策略。
  • 没有失败重试,镜像异常时只能直接失败。
  • 没有本地缓存,GitHub 静态资源会重复走远程请求。
  • 逻辑散落在 Hook 中,后续继续扩展会越来越难维护。

这个 PR 把 GitHub 加速重构成独立模块,并补上请求前拦截、代理选择、失败重试和小体积静态资源缓存。

核心设计

1. Arc 层补丁:为 Http 增加请求前拦截能力

修改 patches/arc/0001-API-Http.onBeforeRequest.patch,给 arc.util.Http 增加一个轻量级的请求前扩展点:

  • 新增 Http.onBeforeRequest,在 HttpRequest.block/submit 进入真实网络访问前触发。
  • HttpRequest 暴露当前请求的 success 回调,便于在 hook 中包装成功逻辑而不是只做 URL 替换。
  • HttpRequest 新增 directResponse,允许 hook 在命中本地缓存等场景下直接短路返回,不再访问网络。
  • HttpResponse.ofBytes(...) 用于构造这类直接返回的响应。

这样 GitHub 加速不需要继续把逻辑塞进全局的“字符串替换”里,而是可以在请求对象层面安全地处理缓存命中、镜像切换和重试。

2. 新增 GithubAcceleration 模块

新增 src/mindustryX/features/GithubAcceleration.kt,统一管理 GitHub 请求的代理、缓存和重试。

请求处理链路如下:

  1. Hooks.beforeInit() 中初始化模块,并注册 Http.onBeforeRequest
  2. 拦截到请求后,先尝试把 https://proxy/.../https://github... 这类已包裹 URL 还原成原始 GitHub URL,避免重试链路重复套代理。
  3. 仅处理 GitHub 相关 host:
    • github.com
    • *.github.com
    • githubusercontent.com
    • *.githubusercontent.com
  4. 使用 api.github.com 单独识别 API 请求,和 release / raw / asset 资源走不同代理选择策略。
  5. 给请求写入内部 header:
    • X-MDTX-GH-Original:记录原始 URL,保证重试时回到未代理地址再重新选镜像。
    • X-MDTX-GH-Attempt:记录当前重试次数。
  6. 如果是非 API 的 GET 请求且本地缓存命中,则直接构造 directResponse 返回。
  7. 如果需要走网络,则根据配置选择当前尝试使用的代理,并改写 req.url
  8. GET 请求包装成功/失败回调:
    • 成功时按条件写缓存。
    • 失败时克隆原请求并切换到下一代理重试。

3. 代理模型与选择策略

代理配置使用 ProxyConfig,支持:

  • 名称
  • URL
  • 是否启用
  • 是否用于 asset 请求
  • 是否用于 API 请求
  • 是否锁定(内置源站不可删除)

默认提供三项:

  • 源站 github.com
  • ghproxy
  • kgithub

策略细节:

  • API 请求只使用勾选了 apiEnabled 的代理。
  • 静态资源请求只使用勾选了 assetEnabled 的代理。
  • 资源请求默认把“镜像代理”排在前面,“源站直连”放在最后作为 fallback。
  • 最大尝试次数取 min(maxRetries, enabledProxyCount),避免重试次数超过实际可用代理数。

4. 缓存策略

缓存只针对非 API 的 GET 请求,避免把 GitHub API 返回内容混入本地缓存。

实现细节:

  • 缓存目录:data/cache/gh-acceleration
  • 缓存 key:对原始 URL 做 SHA-1,文件名为 <sha1>.bin
  • 过期控制:按 cacheExpireMinutes 判断文件 lastModified
  • 写入条件:仅当 contentLength1..2 MiB 之间时写缓存
  • 缓存命中:直接返回 HttpResponse.ofBytes(200, bytes)

这样缓存覆盖的是 raw.githubusercontent.com、release 小资源等高频静态请求,而不会把未知大小的大资源无上限拉进内存缓存。

5. 重试实现

重试只对 GET 请求启用,避免对可能有副作用的写请求做重复提交。

失败重试时会克隆原请求,并保留这些上下文:

  • timeout
  • followRedirects
  • includeCredentials
  • content
  • contentStream
  • 原有 headers(除了内部使用的 attempt 计数)

这样可以保证重试是基于“原始请求”重建,而不是在已经被代理改写过的 URL 上继续叠加状态。

设置与 UI

设置项

在中英文 bundle 中新增 GitHub 加速配置项,支持:

  • 总开关
  • 缓存开关
  • 缓存过期时间
  • 最大重试次数
  • 代理列表编辑

代理列表通过 SettingsV2.Data<List<ProxyConfig>> 持久化,使用 UBJson 保存,支持:

  • 新增自定义代理
  • 编辑代理名称/地址
  • 分别控制 Asset/API 是否启用
  • 清空缓存

快捷入口

NewToolTable 增加 GH 按钮:

  • 点击可快速切换 GitHub 加速总开关
  • 长按/设置绑定可直接进入完整设置项

兼容处理

保留旧配置名兼容迁移:

  • githubAcceleration.enabled 增加对旧 githubMirror 的 fallback

这样用户升级后不会直接丢失旧的“是否开启 GitHub 加速”配置。

清理与替换

  • 删除 Hooks 中旧的简单 URL 改写逻辑。
  • 删除 VarsX 中旧的 githubMirror 直接配置项,改由新模块统一接管。

本次额外修正的评审问题

这个分支在重构基础上又额外修了两点 review feedback:

  • 收紧 GitHub 域名匹配,只接受精确 host / 合法子域名,避免把非 GitHub 站点误判进去。
  • 恢复普通网络响应下的 getResultAsStream() 流式读取行为,避免影响大文件下载路径;只有缓存命中的 directResponse 才会基于内存字节数组返回流。

影响范围

主要改动文件:

  • patches/arc/0001-API-Http.onBeforeRequest.patch
  • src/mindustryX/features/GithubAcceleration.kt
  • src/mindustryX/Hooks.java
  • src/mindustryX/features/ui/toolTable/NewToolTable.kt
  • assets/bundles/bundle-mdtx.properties
  • assets/bundles/bundle-mdtx_en.properties

备注

  • 当前分支共 2 个提交,已 rebase 到最新 main
  • 本 PR 只处理我自己的 gh-issue 分支,不涉及其他 PR。

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

本次拉取请求旨在全面提升 GitHub 资源的访问速度和稳定性。通过引入一个全新的加速模块,系统现在能够提供更灵活的代理配置、智能的重试机制以及高效的静态资源缓存,显著优化了用户体验,并解决了之前版本中存在的评审问题。

Highlights

  • 新增 GithubAcceleration 功能模块: 将原先单一的 GitHub 镜像开关重构为可配置的代理列表、重试策略和静态资源缓存。
  • 设置界面更新: 在设置界面补充了 GitHub 加速相关配置项,并保留旧的 githubMirror 配置名作为兼容迁移入口。
  • 移除旧的 URL 改写逻辑: 移除了 Hooks 中基于旧 Http.onBeforeRequest 的简单 URL 改写,改由新的加速模块统一接管请求处理。
  • 修复评审问题: 根据评审意见修正了域名匹配逻辑,并恢复了流式读取行为,以避免过宽匹配和请求处理副作用。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • assets/bundles/bundle-mdtx.properties
    • 新增了 GitHub 加速功能的配置项,包括类别、启用开关、缓存设置、缓存过期时间、最大尝试次数和代理列表。
  • assets/bundles/bundle-mdtx_en.properties
    • 新增了 GitHub 加速功能的英文配置项,包括类别、启用开关、缓存设置、缓存过期时间、最大尝试次数和代理列表。
  • patches/arc/0001-API-Http.onBeforeRequest.patch
    • 修改了 Http.HttpResponse 类,增加了 cachedResult 字段和相关方法,以支持直接响应和字节缓存。
    • 修改了 Http.HttpRequest 类,增加了 successdirectResponse 字段,并在 block 方法中集成了 onBeforeRequest 的直接响应处理和重试逻辑。
  • src/mindustryX/Hooks.java
    • 移除了旧的 onHttp 方法,该方法曾用于简单的 GitHub URL 重写。
    • beforeInit 方法中初始化了新的 GithubAcceleration 模块。
  • src/mindustryX/VarsX.kt
    • 移除了旧的 githubMirror 配置项。
  • src/mindustryX/features/GithubAcceleration.kt
    • 新增了 GithubAcceleration 对象,实现了 GitHub 请求的代理、重试和缓存逻辑。
    • 定义了 ProxyConfig 数据类,用于管理代理服务器的配置。
    • 提供了启用开关、缓存设置、最大重试次数和代理列表等配置项。
    • 实现了 URL 规范化、GitHub URL 判断、缓存读写等辅助功能。
  • src/mindustryX/features/ui/toolTable/NewToolTable.kt
    • 在工具栏中添加了一个“GH”按钮,用于快速切换 GitHub 加速功能的启用状态。
Activity
  • 根据评审意见,对域名匹配逻辑进行了修正,并恢复了流式读取行为,以避免过宽匹配和请求处理副作用。
  • 该分支包含 2 个提交,并且已经 rebase 到最新的 main 分支。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个PR对GitHub加速功能进行了彻底的重构,引入了可配置的代理、重试和缓存机制,这是一个很棒的改进。代码结构清晰,新的 GithubAcceleration 模块功能完善。

我对核心 Http 类的修改有一个担忧,它可能会对整个应用程序的内存使用产生影响。具体请看我的评论。

除此之外,其他部分的修改看起来都很好,包括旧代码的清理和新UI的添加。

@Wind-DeterMination-backup Wind-DeterMination-backup changed the title 重制 GitHub 加速并修复评审问题 重制 GitHub 加速 Mar 15, 2026
@Wind-DeterMination-backup
Copy link
Copy Markdown
Author

#89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants