Skip to content

feat(engine): give an idle connection the slowest chunk's tail - #6

Merged
troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-slow-chunk
Oct 19, 2026
Merged

troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-slow-chunk

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a connection finishes early it takes the tail of another chunk. Before, the chunk was always the one with the largest remainder. Now it is the chunk that will take longest to finish, when that is at least twice as long as the fastest one. A slow connection's tail goes to a faster one. Until paces are known, the largest remainder is still used.

Pace

  • Pace is measured per chunk over a 200 ms window (notePace).
  • currentPace decays while no bytes arrive (bytes since the last update ÷ time since), so a hung socket falls to 0 instead of keeping its last good pace. A response with no first byte after 1 s counts as 0. A pace of 0 is the slowest, so a stalled connection's chunk is the one split.
  • startPace resets the measurement at each fetch, so the first window after a pause or retry does not divide by the paused time.
  • Coverage is unchanged: only the choice of which chunk to split changes. The mid formula is the same.

Commits

  • 14d4b58 (Ryan Bagas): the original change, rebased onto current main.
  • 8e49fc1: review changes. Pace decay, first-byte stall, startPace per fetch.

Test plan

  • go test ./... and go test -race ./internal/engine
  • TestSplitPicksAStalledChunk: a chunk that was fast but has had no byte for 10 s is split, not the busy larger one. Fails without decay.
  • TestSplitPicksAChunkWithNoBytesYet: no first byte after paceStall → split. Before that, it is still unknown. Fails without the first-byte rule.
  • TestStartPaceForgetsTheEarlierResponse
  • Existing: slower chunk preferred, largest kept when paces match or are close, notePace window.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)

@troke12 troke12 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Nice idea, and coverage stays intact since only the victim choice changes. Two issues:

  • A stalled connection is never picked as slow: notePace only runs when bytes arrive, so a hung socket keeps its last good pace forever (or stays at pace 0 and is skipped). That's the case that matters most. Please decay pace by now - paceAt when computing ETA.
  • paceAt isn't reset on pause/resume or between retries, so the first window after a resume divides by the paused time. Zero it at the start of each fetch.

Note for the whole set (#1–#11): thanks a lot for these! They are all branched from 5350f79 independently, so every pair conflicts on CHANGELOG.md and most on download.go. main has also moved (e830f5d adds a stream downloader that touches download.go). Could you rebase onto current main and stack them in this order: #1 → #2 → #10 → #8 → #5/#6/#7/#11? #2, #3 and #10 each add their own pass loop / transferCancel in Start; one shared design would be much easier to merge.

rigaz29 and others added 2 commits October 19, 2026 10:37
An idle worker still takes half of one unfinished chunk. When two
chunks have a measured pace and the slower one will take at least
twice as long, that chunk gives up its tail. Until a pace is known,
the largest remainder is used.
Review changes on top of the previous commit:

- currentPace caps a chunk's pace by the bytes since its last update over
  the time since, so it falls toward 0 while the socket is quiet:
  notePace only runs when bytes arrive, and a hung socket kept its last
  good pace forever. A response without its first byte for paceStall
  (1 s) counts as pace 0 instead of unknown. Split judges paces at a
  given time (splitAt), and a pace of 0 is the slowest.
- startPace forgets the pace of an earlier response at the start of each
  fetch, so the first window after a pause or retry does not divide by
  the time in between.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
@troke12

troke12 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Rebased onto current main and applied the review: a chunk's pace now decays with the time since its last bytes (a hung socket falls to 0 and is picked first), a response with no first byte after 1 s counts as stalled, and the measurement restarts at each fetch so a pause is not divided into the first window. Your commit is kept with you as author; the review changes are a separate commit on top. Thanks!

@troke12
troke12 merged commit e403c6b into troke12:main Oct 19, 2026
5 checks passed
@rigaz29
rigaz29 deleted the engine-slow-chunk branch October 19, 2026 05:01
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