magpie icon indicating copy to clipboard operation
magpie copied to clipboard

插件页:GitHub 搜索只有 8 秒预算,中国大陆直连赶不上;而配了 GitHubToken 时镜像被整个跳过

Open MYCF711 opened this issue 1 month ago • 4 comments

环境

  • magpie 0.1.1714-carries,Windows 11
  • 中国大陆家宽,裸连 api.github.com 的下载速率约 20KB/s
  • Settings 里填了 GitHubToken
  • 未填 Settings 的「代理」,也没有 HTTPS_PROXY(系统代理关闭)
  • 「下载源」填了 https://v4.gh-proxy.org/

现象

插件页的「非官方插件 · GitHub」区块一直停在

无法连接 GitHub,下面是之前获取的列表:GitHub 响应超时

四个分类都是 0,缓存里也是空的,所以任何第三方插件(哪怕确实打了 magpie-plugin topic)都搜不到。

实测

同一条请求(topic:magpie-plugin archived:false fork:false、per_page=100,响应 689KB,total_count 183 → 要翻 2 页):

路径 耗时
直连 https://api.github.com/search/repositories?... 29.3s / 30.1s / 35.9s
经 https://gh-proxy.com/https://api.github.com/search/repositories?... 3.3s / 3.4s / 6.7s

也就是说,askTagged 的预算是 8 秒,而中国大陆直连一页就要 30 秒。

根因(三处叠加)

1. taggedWait 是固定 8 秒,而一页就是 30 秒

internal/plugin/github.go:

// taggedWait is how long the page waits for GitHub
taggedWait   = 8 * time.Second
taggedPage   = 100

askTagged 用这个 context 包住整个翻页循环 + 每仓库读 package.json / npm:

c, cancel := context.WithTimeout(ctx, taggedWait)
...
b, err := fetchJSON(c, githubAPI+"/search/repositories?"+v.Encode(), 4<<20)

第一页就超时,askTagged 直接返回 error,缓存里什么都留不下 —— 这就是「列表是空的」而不是「少了几条」的原因。

2. 配了 GitHubToken,镜像就被整个跳过

internal/source/source.go 其实已经支持 api.github.com 走镜像:

case "github.com", "api.github.com", "raw.githubusercontent.com", "codeload.github.com":
    return distinct(append(out, githubBase()+"/"+raw)...)

但前提是 mirrorable(req) 为真,而这个函数见到 Authorization 头就返回 false:

for _, h := range []string{"Authorization", ...} {
    if req.Header.Get(h) != "" {
        return false
    }
}

internal/ghtoken 又恰好会给发往 api.github.com 的每个请求加上 Authorization: Bearer <token>(authorize() 按 host 判断)。

结果是:用户在 Settings 里填了 GitHubToken(很常见),MAGPIE_MIRRORS=on 就再也不起作用了,因为请求永远带着 Authorization。

我理解「带凭证的请求绝不给镜像」是刻意的安全设计,这条不该被推翻。但组合出来的效果是:越需要镜像的网络,越用不上镜像。

3. 就算镜像生效,8 秒预算也不够

candidateContext 在有多个候选地址时,只给官方地址「剩余预算的一半」,其余留给镜像。按 taggedWait = 8s 算:

  • 第 1 页:官方 4s 超时 → 镜像 3.3s 成功,累计 7.3s
  • 第 2 页:只剩 0.7s,官方与镜像都超时 → 整个 askTagged 失败

所以中国大陆用户在无 token 的情况下开 MAGPIE_MIRRORS=on,仍然拿不到列表。

另外:internal/netproxy 的 Install() 确实把 http.DefaultClient 接到了 Settings 的代理上,fetchJSONFrom 用的也正是 http.DefaultClient —— 所以填了 Settings 的「代理」是能救的(我用一个本地反代实测 0.2–0.5s 一页)。问题在于「下载源」(updateMirror)和「代理」(proxy)这两个设置项名字上很像,很容易以为配了前者就够了,而前者只用于下载更新文件,不管搜索。

建议(任选,都不是大改)

  1. 让 taggedWait 可配:加一个类似 MAGPIE_PLUGIN_LIST_WAIT 的环境变量,或按「页数 × 每页预算」分配,别用一个常量兜住整个翻页循环。
  2. API 搜索在镜像上不带 token:镜像那一跳把 Authorization 去掉再发(镜像看不到 token,安全性质不变),官方那一跳照旧带。这样配了 token 的用户也能享受镜像。
  3. 在「下载源」旁边说明它管不到插件列表,或者干脆给「代理」加一句「插件页的 GitHub 搜索走这里」——能省掉很多人的排查时间。

MYCF711 avatar Oct 27 '26 16:10 MYCF711

Checked against v0.1.1767. Two of the three causes stand; one has moved.

What moved. internal/plugin/github.go now has:

taggedWait   = 8 * time.Second
taggedEach   = 20 * time.Second
taggedAtOnce = 12
taggedRetry  = 20 * time.Second

Each repository's package.json / npm / entry-file read is bounded by its own taggedEach (20s), goes on in the background past the wait, and is kept for the next ask. That is the half I did not think was the main cost — good change.

What stands.

  1. taggedWait still wraps the search itself: c, cancel := context.WithTimeout(ctx, taggedWait) around the whole paging loop. A first page that takes 30s here still fails askTagged outright and the cache keeps nothing — that is the "list is empty, not short" behaviour in the report.
  2. mirrorable() (internal/source/source.go:185-187) still returns false on an Authorization header, and ghtoken still puts one on every request to api.github.com, so a user with a token in Settings still gets no mirror at all.
  3. candidateContext (internal/source/source.go:277, "bounds a source's try to half of the request's") is unchanged, so even when the mirror applies, the second page still gets whatever little is left.

So the report narrows to: the per-repository reads were widened, the search request that has to come first was not.

MYCF711 avatar Oct 27 '26 17:10 MYCF711

Thanks for measuring this. We took both suggestions in e4ccf67b1, released in v0.1.1775:

  • The wait. Each page of the GitHub search now has 60s of its own (taggedSearch). The 8s taggedWait now covers only the per-repository reads after the search, which carry on in the background and are kept, as before. So a page that takes ~30s straight from mainland China still produces the list, with no setting needed. With MAGPIE_MIRRORS=on, the official address gets half of the 60s and the mirror gets the rest. That fixes the second page running out of budget, which you described in point 3.
  • Mirrors with a token. A request to GitHub whose only credential is the Authorization header now goes to api.github.com with the token, and to the mirror without it. The mirror never sees the token. A request carrying any other credential (a cookie, an API key header, a token in the query) still goes only to the official address. A mirror's answer doesn't count as having carried the token, and a 401 from a mirror doesn't mark the token as refused.

Not tried: a real connection from mainland China, and a live gh-proxy answer. Could you check the Plugins page on v0.1.1775 and tell us whether the list shows up? I'll leave the issue open until you confirm.

yetone avatar Oct 27 '26 19:10 yetone

Confirmed on v0.1.1775 — the list shows up, with nothing set.

Built e4ccf67b (-tags nogui -trimpath -X main.version=v0.1.1775) and ran it against a portable data folder of its own, so my running 0.1.1773 was left alone: Windows 11, mainland China home line, no proxy, no MAGPIE_MIRRORS, a GitHub token in Settings.

/api/plugins/github answered in 16.1s with 136 repositories — the section that read "GitHub 响应超时 / 列表为空" before now has entries. The answer carried error: "no answer in 8s" (why: slow), which is the per-repository reads and exactly the part you left under taggedWait: the list appears and is short rather than empty, and Try again fills it in.

On the search itself. It is 2.4s and 3.0s a page here today — against 29-36s when I filed this. So I can't claim to have re-run the slow page on the wire. What I can confirm is the shape that was broken: taggedSearch is per page and does not borrow the page before it, so a second page no longer inherits what the first one spent.

A live gh-proxy answer — the thing you said you had not tried. An anonymous search through https://gh-proxy.com/https://api.github.com/search/repositories?q=topic:magpie-plugin+archived:false+fork:false&per_page=100&page=1 answers in 3.96s with 100 items and total_count: 185. The mirror does answer a search, so the bare path has something to fall back to.

What I could not check. The mirror leg with a token. On this line the official address answers in 2-3s today, so the mirror is never reached and I have no measurement of a token-carrying request falling back to it. I read bare() and do() and they do what you describe — the official candidate keeps the header, a mirror candidate has it deleted, and ghtoken.Do reads resp.Request — but that is reading, not a measurement, and I would rather say so.

One number for the record: the reads made 336 requests to raw.githubusercontent.com for this list, 331 of them 200 and 5 of them 404, so the short list is the 8s and not the repositories being unreadable.

Closing from my side.

MYCF711 avatar Oct 27 '26 20:10 MYCF711

Confirmed on the code of e4ccf67b, built here as a nogui binary: the list shows up.

First, about the release. v0.1.1775 is tagged (e4ccf67b) but has no release yet. yetone/magpie and yetone/magpie-releases both still answer v0.1.1774 as the latest, with its 16 assets, and releases/download/v0.1.1775/SHA256SUMS answers 404. So there was nothing to install; I built e4ccf67b with go build -tags nogui and ran it as magpie web against a copy of my settings (GitHubToken set, chinaMirror on, download source https://v4.gh-proxy.org/, no proxy — the setup in the report), in config and cache folders of its own.

The Plugins page's GitHub list, same network, same settings:

/api/plugins/github, nothing cached 175 repositories, 15.4s, missing: 29, why: slow, error: "no answer in 8s"
the same, asked again (most had been read by then) 175 repositories, 7.5s, no error
within the ten minutes 175, 0.015s

So the search half is fixed: 175 is the whole list, where before the first page timed out and the cache kept nothing. Two pages straight to api.github.com take 2.27s and 2.47s here this hour, so the 60s a page is ample even on a bad day.

What is left is the other half, and it is what the first reader sees. A cold ask spends its 8 seconds reading 175 package.jsons (1.9 MB here) and gets through 148 of them, so the first paint is 148 cards plus "GitHub didn't answer in time — Try again", and the second ask is all 175. That is taggedWait doing exactly what you described — the reads carry on and are kept — but on this network the first reader is told the list is short when it is only unread. If that is worth a look, giving the reads a budget of their own the way the search got one would do it. I have not tried a change there.

The mirror half I could not separate out. Straight to api.github.com is 2.27s and 2.47s a page for me now; the 29–36s in the report was that day's network rather than a constant, so a mirror's 3.3s is no longer distinguishable from a direct answer and I cannot say from here which one answered. It is at least no longer ruled out by the token: the same settings that made MAGPIE_MIRRORS do nothing before now leave it in play.

For the record on point 3: with 60s a page the second page no longer runs out of budget either way — both pages took 4.7s in total here.

MYCF711 avatar Oct 27 '26 20:10 MYCF711