Skip to content

feat(core): let finished connections take over slower ones' remaining bytes - #581

Merged
linroid merged 3 commits into
mainfrom
feat/work-stealing-segments
Oct 18, 2026
Merged

linroid merged 3 commits into
mainfrom
feat/work-stealing-segments

Conversation

@linroid

@linroid linroid commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Implements the README roadmap item Faster segmented downloads: connections that finish early take over the rest of the slower ones, so a download no longer waits on its slowest connection.

How it works

  • Each segment runs as a SegmentLane on one connection. When a lane finishes, its connection asks SegmentLedger.steal for the tail of the lane expected to finish last and downloads it with a request of its own, instead of closing.
  • The split is where both finish together, from rates measured once a lane ran 1 s (0.5 s counted for the new request's setup); before that, in half. A lane that received nothing in that time hands over everything it has not admitted, and its idle transfer is cancelled.
  • Only ranges of at least 1 MiB (DEFAULT_MIN_SPLIT_BYTES) are handed over, so small files stay on their connections; never without range support.
  • Sources write what SegmentLane.admit allows and report it with commit. A lane whose tail was taken ends its transfer once it reaches its new end (a CancellationException the helper counts as done), which closes the response.
  • Split ranges are saved as segments of their own, ordered and indexed by offset, so pause/resume and live connection changes work as before.

API change

SegmentedDownloadHelper.downloadAll's transfer lambda now takes a SegmentLane instead of (Segment, onProgress) -> Segment; HttpDownloadSource and FtpDownloadSource are updated. Pre-1.0, so allowed.

Not in this PR

A failed range still fails the whole batch (retried from saved segments); retrying a range alone is next (AGENTS.md roadmap item 6, now "Lane scheduler").

Tests

  • SegmentLedgerTest: half splits, admitted bytes never moved, minimum tail, rate-based split point, stalled victim, refusing splits that would not finish sooner, cut lanes ending their transfer.
  • SegmentedDownloadHelperTest: a fast connection takes over a slow one's bytes on virtual time, every byte written once; no splits without range support.
  • HttpDownloadSourceTest: a 4 MiB download with a slow second connection is taken over and written byte-for-byte correctly.
  • :library:core:jvmTest, :library:ftp:jvmTest and :library:core:compileTestKotlinIosSimulatorArm64 pass locally.

… bytes

A connection that finishes its segment now splits the tail off the connection expected to finish last, where both finish together from their measured rates (in half until known), instead of closing. Only ranges of at least 1 MiB are handed over and never without range support. Sources write through SegmentLane.admit/commit, and a lane whose tail was taken ends its transfer at its new end. HTTP and FTP use it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T15:59:31.792965Z 83ba80c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 83ba80cf7c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…request quotas

Closing the data and control sockets ran in a cancellable withContext, so a pause or a takeover that cancelled an idle FTP transfer could leave them open; they now close under NonCancellable. Taking over a range sends one more request, so downloads whose server reports RateLimit-Remaining no longer take ranges over.
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 2 052 files  + 4   2 080 suites  +4   32m 21s ⏱️ +8s
13 768 tests +62  13 504 ✅ +61  264 💤 +2  0 ❌  - 1 
16 759 runs  +66  16 495 ✅ +65  264 💤 +2  0 ❌  - 1 

Results for commit ae814c6. ± Comparison against base commit 1ad0e33.

This pull request removes 4 and adds 66 tests. Note that renamed tests count towards both.
com.linroid.ketch.segment.SegmentedDownloadHelperTest ‑ downloadAll_usesFinalProgressReturnedBySource
com.linroid.ketch.segment.SegmentedDownloadHelperTest ‑ downloadAll_usesFinalProgressReturnedBySource[jvm]
iosSimulatorArm64Test.com.linroid.ketch.segment.SegmentedDownloadHelperTest ‑ downloadAll_usesFinalProgressReturnedBySource[iosSimulatorArm64]
jsNodeTest.com.linroid.ketch.segment.SegmentedDownloadHelperTest ‑ downloadAll_usesFinalProgressReturnedBySource[js, node]
com.linroid.ketch.app.snapshot.ConnectionGridSnapshots ‑ detailsCard_peerAndHttp_listEveryDetail[jvm]
com.linroid.ketch.app.snapshot.ConnectionGridSnapshots ‑ page_hoverClickAndSort_onlyTheTaskShowsTheTask[jvm]
com.linroid.ketch.app.ui.connections.ConnectionRowsTest ‑ arrangement_roundTrips_andFallsBackToTheDefault[jvm]
com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_finishedConnectionTakesOverTheSlowOnesTail
com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_finishedConnectionTakesOverTheSlowOnesTail[jvm]
com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_requestQuota_neverTakesOverRanges
com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_requestQuota_neverTakesOverRanges[jvm]
com.linroid.ketch.segment.SegmentLedgerTest ‑ cutLane_endsItsTransferAtItsNewEnd
com.linroid.ketch.segment.SegmentLedgerTest ‑ cutLane_endsItsTransferAtItsNewEnd[jvm]
com.linroid.ketch.segment.SegmentLedgerTest ‑ steal_fromLaneReceivingNothing_takesEverythingNotAdmitted
…

♻️ This comment has been updated with latest results.

@linroid
linroid merged commit 3c88467 into main Oct 18, 2026
10 of 11 checks passed
@linroid
linroid deleted the feat/work-stealing-segments branch October 18, 2026 00:11
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.

1 participant