Repository navigation
fix(gitlab): 分页循环补重复页防护与跳数上限 - #3841
Open
linus-liu-web wants to merge 1 commit into
Open
linus-liu-web wants to merge 1 commit into
linus-liu-web wants to merge 1 commit into
Conversation
projects() 与 tree() 只依据服务端返回的 X-Next-Page 判断是否继续, 该值重复或不前进时循环不会结束:同一页被反复请求、结果切片持续 增长,只能等上层 context 到期。 两条循环改用 trackPage:页码已请求过即返回错误,不静默当作读完; 页数达到 maxPaginationHops 时记警告并返回错误。正常翻页路径不变。
linus-liu-web
force-pushed
the
fix/gitlab-pagination-guard
branch
from
September 30, 2026 08:18
4b47d09 to
50643c4
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题(#3839)
GitLab 数据源的两条分页循环只依据响应头
X-Next-Page决定是否继续,而这个值不做任何校验:projects()(internal/datasource/connector/gitlab/client.go:144-162)与tree()(:180-199)都是for { … if nextPage == "" { return } … q.Set("page", nextPage) },getPage()(:222)直接return resp.Header.Get("X-Next-Page"), nil,不持有跨轮状态。既没有"这一页已经取过"的判定,也没有页数/条数上限。服务端(或它前面的反向代理)反复返回同一个
X-Next-Page时,同一页被反复追加进结果切片,内存随请求数线性增长,循环只能等上层 context 到期:配置数据源时资源选择器一直转圈(GET /api/v1/datasource/{id}/resources),同步任务卡在「运行中」直到队列 2 小时超时。用假服务端恒返回
X-Next-Page: 2+ 2 秒超时的 context 实测:projects()发出 96308 次请求、tree()92915 次,唯一终止条件是 context 到期。projects()的翻页是 #3624 新加的,沿用了tree()既有的循环写法,两条因此带着同一个缺口。仓库里其它连接器不是这么写的:Confluence 有seen+maxPaginationHops,钉钉有seenTokens+maxPages,飞书有seenPageTokens(#3645/#3770)。改动
client.go新增maxPaginationHops = 10000与trackPage(ctx, scope, seen, page):页码重复立即返回错误(不静默当成读完),seen触顶时先Warn再返回错误;projects()与tree()在取每一页之前各调用一次,正常翻页路径不变。测试
新增
pagination_guard_test.go(4 个用例:projects / tree 的重复页各自报错、触顶时停止、正常前进页放行)。反向验证:把
trackPage改成空实现后,4 个用例全部 FAIL(重复页用例退化为把同一页读到底);还原后ok github.com/Tencent/WeKnora/internal/datasource/connector/gitlab,go vet干净。自查:
gofmt/gofumpt干净、新增行符合lll(120,tab=4)。Fixes #3839