Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

refactor: modularize concurrent downloader logic by extracting helper methods and state management into dedicated routines - #444

#444
Merged
SuperCoolPencil merged 5 commits into
mainSurgeDM/Surge:mainfrom
speed-limit-hndwtnSurgeDM/Surge:speed-limit-hndwtnCopy head branch name to clipboard
May 14, 2026
Merged

refactor: modularize concurrent downloader logic by extracting helper methods and state management into dedicated routines#444
SuperCoolPencil merged 5 commits into
mainSurgeDM/Surge:mainfrom
speed-limit-hndwtnSurgeDM/Surge:speed-limit-hndwtnCopy head branch name to clipboard

Conversation

@SuperCoolPencil

@SuperCoolPencil SuperCoolPencil commented May 13, 2026

Copy link
Copy Markdown
Member

Greptile Summary

This PR refactors the monolithic Download method in the concurrent downloader by extracting its internal phases into dedicated methods (initMirrorStatus, setupNetwork, getWorkerMirrors, setupTasks, startHelpers, runBalancer, runCompletionMonitor, runHealthMonitor, executeWorkers, handlePause, syncFile). A companion test file covers the extracted helpers.

  • The refactoring fixes a nil-pointer risk in runCompletionMonitor by guarding d.State.Downloaded.Load() behind a d.State != nil check that the original inline goroutine lacked.
  • executeWorkers now deduplicates and joins multiple worker errors with errors.Join instead of silently overwriting with the last error, surfacing all unique failure causes to callers.
  • The bitmap initialization order (InitBitmap \u2192 setupTasks/RestoreBitmap) is preserved correctly, and the pause-at-completion-boundary path now explicitly returns d.syncFile(outFile) rather than falling through to the cancel check.

Confidence Score: 5/5

Safe to merge; the extracted helpers preserve the original control flow and the only finding is a redundant test.

The decomposition is mechanically faithful to the original logic, the defer ordering is correct, the nil-guard improvement is sound, and the new tests cover the key extracted paths. No functional regressions were identified.

No files require special attention.

Important Files Changed

Filename Overview
internal/engine/concurrent/downloader.go Download logic decomposed into focused helpers; nil-guard for d.State in runCompletionMonitor added; error deduplication via errors.Join introduced; defer ordering and file-close logic are correct.
internal/engine/concurrent/downloader_helpers_test.go New test file covering extracted helpers; TestHandlePause_CompletionBoundary and TestHandlePause_CompletionFinalization duplicate the same scenario and one should be removed.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
internal/engine/concurrent/downloader_helpers_test.go:12-35
**Duplicate test scenario**

`TestHandlePause_CompletionBoundary` (lines 12–35) and `TestHandlePause_CompletionFinalization` (lines 183–213) are structurally identical: both construct a fresh `ConcurrentDownloader` with an empty queue and assert that `handlePause` returns `nil` and leaves the state unpaused. Running the same setup twice adds noise and maintenance overhead without covering any additional behavior — if the logic under test changes, both tests will break (or pass) in lockstep. One of them should be removed.

Reviews (3): Last reviewed commit: "Merge branch 'main' into speed-limit-hnd..." | Re-trigger Greptile

… methods and state management into dedicated routines
Comment thread internal/engine/concurrent/downloader.go Outdated
Comment thread internal/engine/concurrent/downloader.go
Comment thread internal/engine/concurrent/downloader.go
Comment thread internal/engine/concurrent/downloader.go Outdated

@junaid2005p junaid2005p left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@SuperCoolPencil
SuperCoolPencil merged commit 44df933 into main May 14, 2026
15 checks passed
@SuperCoolPencil
SuperCoolPencil deleted the speed-limit-hndwtn branch May 14, 2026 04:30
StressTestor pushed a commit to StressTestor/Surge that referenced this pull request Jun 1, 2026
… methods and state management into dedicated routines (SurgeDM#444)
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.