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

Fixed various issues in internal/engine/types - #450

#450
Merged
SuperCoolPencil merged 12 commits into
mainSurgeDM/Surge:mainfrom
coreSurgeDM/Surge:coreCopy head branch name to clipboard
May 15, 2026
Merged

Fixed various issues in internal/engine/types#450
SuperCoolPencil merged 12 commits into
mainSurgeDM/Surge:mainfrom
coreSurgeDM/Surge:coreCopy head branch name to clipboard

Conversation

@junaid2005p

@junaid2005p junaid2005p commented May 15, 2026

Copy link
Copy Markdown
Collaborator

Greptile Summary

This PR consolidates the dual RuntimeConfig structs (one in internal/config, one in internal/engine/types) into a single types.RuntimeConfig, renames MaxConnectionsPerHostMaxConnectionsPerDownload throughout the codebase, and introduces DefaultRuntimeConfig() so callers no longer create sparse struct literals that silently disable health-check features.

  • Field rename & constant adjustment: MaxConnectionsPerHostMaxConnectionsPerDownload across 29 files; getter fallback constant changed from PerHostMax = 64 to PerDownloadMax = 32; a deprecated MaxConnectionsPerHost alias (tagged json:\"-\") is retained in NetworkSettings for backward compatibility.
  • Getter semantics: Health-check fields (StallTimeout, SlowWorkerThreshold, SlowWorkerGracePeriod, SpeedEmaAlpha) now treat zero as "opt-out" rather than falling back to the package default, backed by matching guards in health.go and worker.go. Download() and both downloader constructors now use DefaultRuntimeConfig() to fix the stall/slow-worker regression.
  • Bitmap normalisation: RestoreBitmap now allocates a correctly-sized bitmap and copies into it, preventing a panic when a persisted bitmap is shorter than expected.

Confidence Score: 5/5

Safe to merge; all production paths now use DefaultRuntimeConfig() so health checks remain active, and bitmap normalisation prevents resume-time panics.

The core fixes are correct and well-tested. Two undocumented constant changes (SlowWorkerThreshold 0.50→0.30 and StallTimeout 5s→3s) alter engine behaviour for all default-settings users, and a minor getter asymmetry means MaxTaskRetries=0 cannot disable retries while its sibling fields can be zeroed to opt out.

internal/engine/types/config.go — the constant and getter-semantics changes are the most impactful and least documented part of this PR.

Important Files Changed

Filename Overview
internal/engine/types/config.go Core runtime config refactor: renamed MaxConnectionsPerHost→MaxConnectionsPerDownload, PerHostMax 64→PerDownloadMax 32, changed getter semantics to allow 0 as an opt-out for health-check fields, added DefaultRuntimeConfig(), consolidated all constants into one block. SlowWorkerThreshold and StallTimeout defaults changed without documentation.
internal/config/settings.go Renamed MaxConnectionsPerHost→MaxConnectionsPerDownload, kept deprecated alias with json:"-", eliminated local RuntimeConfig struct in favour of types.RuntimeConfig, updated ToRuntimeConfig().
internal/engine/types/progress.go Added bitmapLayout() helper, RestoreBitmap now normalises short/malformed bitmaps, added out-of-bounds guards in setChunkState/getChunkState.
internal/engine/concurrent/health.go Added stallTimeout > 0 and threshold > 0 guards so zero values correctly disable stall detection and slow-worker cancellation.
internal/engine/types/config_convert.go Deleted: no longer needed now that settings.ToRuntimeConfig() returns types.RuntimeConfig directly.
internal/download/manager.go Download() now calls types.DefaultRuntimeConfig() instead of a hand-rolled sparse struct, fixing the previous stall/slow-worker regression.
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
internal/engine/types/config.go:41-44
**Silent behavioral changes to default health-check constants**

`SlowWorkerThreshold` dropped from `0.50` to `0.30`, and `StallTimeout` dropped from `5s` to `3s`. For users relying on engine defaults (i.e. not configuring these in settings), the slow-worker cancellation threshold is now 30 % of mean speed instead of 50 %, making it considerably less aggressive. Stall detection also fires sooner (3 s vs 5 s). These are meaningful behavioral differences that aren't captured in the PR description, making them easy to overlook in production if users report changed download behaviour.

Also note: the renamed constant `PerDownloadMax = 32` (was `PerHostMax = 64`) halves the getter fallback for connections. All known production paths now call `DefaultRuntimeConfig()` which explicitly sets 32, so this only matters for partial struct literals—but the change is worth documenting.

### Issue 2 of 2
internal/engine/types/config.go:120-125
**`GetMaxTaskRetries` asymmetry with opt-out siblings**

The struct comment documents two groups: capacity-style settings (zero → use default) and opt-out settings (zero → disable). `GetMaxTaskRetries` uses `<= 0` as the fallback, which is correct for a capacity-style field, but that means `MaxTaskRetries = 0` cannot be used to disable retries entirely. `StallTimeout`, `SlowWorkerThreshold`, `SlowWorkerGracePeriod`, and `SpeedEmaAlpha` all allow 0 to opt out. If a future caller wants zero retries (e.g. a probe-only path), they have no way to express that. A comment on the getter clarifying this constraint would close the ambiguity.

Reviews (2): Last reviewed commit: "fix: Fixed sparse runtime configs" | Re-trigger Greptile

Comment thread internal/download/manager.go Outdated
Comment on lines +350 to +352
if aliasValue != 0 && (aliasValue < 1 || aliasValue > 64) {
warnings = append(warnings, fmt.Sprintf("Max connections/download reset to default (%d)", defaults.MaxConnectionsPerDownload))
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Duplicate warning for a single misconfiguration

When the deprecated MaxConnectionsPerHost alias carries an out-of-range value (e.g., 999) while MaxConnectionsPerDownload is 0, the switch copies the bad value into MaxConnectionsPerDownload, triggering the first if block (which resets and emits warning 1), and then the second if block also fires on the original aliasValue (emitting an identical warning 2). The user sees two identical "Max connections/download reset to default" warnings for a single issue. Because MaxConnectionsPerHost is tagged json:"-" it is only ever set programmatically, so this is primarily a concern for tests and migration code, but it still obscures diagnostics.

Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/config/settings.go
Line: 350-352

Comment:
**Duplicate warning for a single misconfiguration**

When the deprecated `MaxConnectionsPerHost` alias carries an out-of-range value (e.g., `999`) while `MaxConnectionsPerDownload` is `0`, the switch copies the bad value into `MaxConnectionsPerDownload`, triggering the first `if` block (which resets and emits warning 1), and then the second `if` block also fires on the original `aliasValue` (emitting an identical warning 2). The user sees two identical "Max connections/download reset to default" warnings for a single issue. Because `MaxConnectionsPerHost` is tagged `json:"-"` it is only ever set programmatically, so this is primarily a concern for tests and migration code, but it still obscures diagnostics.

How can I resolve this? If you propose a fix, please make it concise.

@SuperCoolPencil SuperCoolPencil left a comment

Copy link
Copy Markdown
Member

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 eebbc05 into main May 15, 2026
16 checks passed
@SuperCoolPencil
SuperCoolPencil deleted the core branch May 15, 2026 05:40
StressTestor pushed a commit to StressTestor/Surge that referenced this pull request Jun 1, 2026
* refactor: Renamed maxConnectionsPerHost to maxConnectionsPerDownload since surge does not reuse old conns to the same host since HTTP 1.1 is one way

* fix(config): Did not accept 0 value for some fields although settings allowed a 0 value. Made both them consistent with each other

* refactor(config): Removed unnecessary what comments and kept constants in one place and getters in one place

* refactor(internal): Removed duplicate function to convert runtime settings into RuntimeConfig

* test: Added tests in accuracy_test to increase test coverage

* fix: Fixed a bug where download resume trusted chunk bitmap without looking at its shape which may cause silent failures

* refacor: Fixed a failing test and added better comments

* refactor: renamed maxdownloadsperhost to maxdownloadsperDownload

* fix: Fixed a failing regression test

* fix: Fixed sparse runtime configs
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.