refactor: 超长文件拆分 + 函数分解 + 调度工具提取#2519
Open
Michael-Jetson wants to merge 2 commits into
Open
Conversation
Structural refactoring to improve maintainability without changing any behavior: **Phase 1 - File splitting (37 new files):** - gateway_service.go: 9,579 → 3,300 lines (split into 15 focused files) - openai_gateway_service.go: 6,397 → 3,822 lines (6 new files) - openai_ws_forwarder.go: 4,228 → 2,416 lines (5 new files) - admin_service.go: 3,698 → 2,051 lines (5 new files) - usage_log_repo.go: 4,394 → 1,682 lines (3 new files) - setting_service.go: 4,032 → 2,502 lines (3 new files) **Phase 2 - Function decomposition:** - SelectAccountWithLoadAwareness (654 lines → 25-line dispatcher + 7 sub-methods) New: gateway_account_selection_helpers.go with schedulingContext/accountPool structs - Forward (Gateway, 620 lines → 380 lines) New: gateway_forward_helpers.go with prepareForwardBody/processForwardResponse **DRY optimization:** - New: gateway_scheduling_utils.go - isAccountEligibleForScheduling: unified 7-check gate (eliminates 8 repetitions) - sortAccountsWithLoadByPriority: unified load-aware sort+shuffle - isBetterAccountCandidate: unified priority→LRU→OAuth comparison (eliminates 5 repetitions) **Verification:** - All 43 test packages pass - Zero behavior change (function signatures preserved) - Build passes with Go 1.26.3 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
概述
纯结构重构,不改变任何行为。将最大文件缩减 50-62%,最复杂函数从 654 行分解为 25 行调度器。
改动内容
文件拆分(37 个新文件):
gateway_service.goopenai_gateway_service.goopenai_ws_forwarder.goadmin_service.gousage_log_repo.gosetting_service.go函数分解:
SelectAccountWithLoadAwareness(654行)→ 7 个子方法,通过gateway_account_selection_helpers.go实现Forward(Gateway, 620行)→ 提取prepareForwardBody+processForwardResponse共享工具(
gateway_scheduling_utils.go):isAccountEligibleForScheduling— 统一 7 项资格检查(消除 8 处重复)sortAccountsWithLoadByPriority— 统一负载感知排序 + 组内随机isBetterAccountCandidate— 统一优先级→LRU→OAuth 比较(消除 5 处重复)不变的内容
测试计划
go build ./...编译通过🤖 Generated with Claude Code