Skip to content

feat: implement concurrent NAR streaming - #619

Merged
kalbasit merged 3 commits into
mainfrom
01-22-feat_implement_concurrent_nar_streaming
Jan 23, 2026
Merged

kalbasit merged 3 commits into
mainfrom
01-22-feat_implement_concurrent_nar_streaming

Conversation

@kalbasit

@kalbasit kalbasit commented Jan 23, 2026 •

Copy link
Copy Markdown
Owner

Implement concurrent NAR streaming to clients as soon as the download starts
from an upstream cache. Previously, ncps would download the entire NAR into its
store before starting to stream it back to the client. For large NAR files,
this often led to client timeouts.

This change refactors coordinateDownload to allow an early return (after the
download starts) for jobs that support streaming, like NAR downloads. It also
ensures that the distributed lock is held until the asset is fully stored in
the background, preventing other instances from starting a redundant download
while allowing the current instance to stream the file being downloaded.

Detailed changes:

  • Refactored coordinateDownload to accept a waitForStorage boolean.
  • Updated prePullNar to call coordinateDownload with waitForStorage=false.
  • Updated prePullNarInfo to call coordinateDownload with waitForStorage=true.
  • Modified distributed lock handling in coordinateDownload to release the lock
    in a background goroutine when waitForStorage is false.
  • Updated GetNar to handle the -1 size returned during streaming and correctly
    stream from the temporary file using io.Pipe.
  • Fixed a race condition in GetNar where clients could receive EOF before the
    NAR was fully stored in final storage by waiting for storage completion in the
    streaming goroutine.
  • Added TestNarStreaming to verify the fix and ensured it checks for asset
    presence after completion.
  • Updated TestConcurrentDownload_CancelOneClient_OthersContinue to handle -1
    size during streaming.

fixes #618

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels Jan 23, 2026

Copy link
Copy Markdown
Owner Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kalbasit, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant performance improvement by enabling concurrent streaming of NAR (Nix Archive) files. Previously, clients would experience timeouts when downloading large NARs because the entire file had to be downloaded and stored locally before streaming could begin. The changes refactor the coordinateDownload mechanism to allow immediate client streaming as soon as the upstream download starts, while intelligently managing distributed locks in the background to prevent duplicate downloads. This ensures a smoother and faster experience for clients requesting large NAR assets.

Highlights

  • Concurrent NAR Streaming: Implemented the ability to stream NARs to clients as soon as the download starts from an upstream cache, preventing client timeouts for large files.
  • Refactored coordinateDownload: The core download coordination function now accepts a waitForStorage boolean, allowing callers to specify whether to wait for the asset to be fully stored or just for the download to begin.
  • Optimized Lock Handling: When waitForStorage is false (for NAR streaming), the distributed lock is released in a background goroutine after the asset is fully stored, ensuring other instances don't start redundant downloads while the current instance streams.
  • Updated NAR Pulling Logic: prePullNar now calls coordinateDownload with waitForStorage=false to enable streaming, while prePullNarInfo uses waitForStorage=true to ensure complete metadata availability.
  • New Test Case: Added TestNarStreaming to specifically validate the concurrent streaming behavior, simulating a slow upstream and verifying early client access.
  • Test Adaptation: Modified TestConcurrentDownload_CancelOneClient_OthersContinue to correctly handle cases where the NAR size might be -1 during streaming.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@kalbasit kalbasit added the backport release-0.7 Backport PR automatically label Jan 23, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces concurrent NAR streaming, a significant performance improvement for large files. The changes in coordinateDownload are well-structured, correctly using a detached context and a background goroutine to manage the distributed lock lifecycle while allowing the client request to proceed. The new test case TestNarStreaming effectively validates the streaming behavior, and existing tests are updated to accommodate the changes. I have one minor suggestion regarding a small piece of duplicated code for better maintainability, keeping in mind that duplicated code can sometimes be acceptable.

@kalbasit
kalbasit enabled auto-merge (squash) January 23, 2026 03:45
@kalbasit
kalbasit force-pushed the 01-22-feat_implement_concurrent_nar_streaming branch from fb3bd2c to cca276b Compare January 23, 2026 03:53
@kalbasit
kalbasit disabled auto-merge January 23, 2026 03:59
@kalbasit

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces concurrent NAR streaming, a significant feature to prevent client timeouts on large file downloads. The changes are well-structured, refactoring coordinateDownload to support both streaming (for NARs) and non-streaming (for NarInfo) downloads. The use of a background goroutine to hold the distributed lock for streaming downloads until storage is complete is a clever and effective solution to prevent redundant downloads across instances. The addition of TestNarStreaming provides good coverage for the new functionality. I've found one area for improvement to enhance the robustness of lock handling.

Comment thread pkg/cache/cache.go Resolved
Implement concurrent NAR streaming to clients as soon as the download starts
from an upstream cache. Previously, ncps would download the entire NAR into its
store before starting to stream it back to the client. For large NAR files,
this often led to client timeouts.

This change refactors coordinateDownload to allow an early return (after the
download starts) for jobs that support streaming, like NAR downloads. It also
ensures that the distributed lock is held until the asset is fully stored in
the background, preventing other instances from starting a redundant download
while allowing the current instance to stream the file being downloaded.

Detailed changes:
- Refactored coordinateDownload to accept a waitForStorage boolean.
- Updated prePullNar to call coordinateDownload with waitForStorage=false.
- Updated prePullNarInfo to call coordinateDownload with waitForStorage=true.
- Modified distributed lock handling in coordinateDownload to release the lock
  in a background goroutine when waitForStorage is false.
- Updated GetNar to handle the -1 size returned during streaming and correctly
  stream from the temporary file using io.Pipe.
- Fixed a race condition in GetNar where clients could receive EOF before the
  NAR was fully stored in final storage by waiting for storage completion in the
  streaming goroutine.
- Added TestNarStreaming to verify the fix and ensured it checks for asset
  presence after completion.
- Updated TestConcurrentDownload_CancelOneClient_OthersContinue to handle -1
  size during streaming.
  Changes Made

  1. Error Handling After Storage Wait (pkg/cache/cache.go:860-872)

  Added proper error checking when waiting for storage completion during streaming:
  - Added comment for successful storage case
  - Added error checking for the done case that logs warnings if the download failed
  - This ensures we properly handle and log any errors that occur during background storage

  2. Improved Documentation (pkg/cache/cache.go:2541-2549)

  Expanded the comment explaining the lock release strategy to be more detailed:
  - Clearly explains the two different strategies based on waitForStorage
  - Documents why NarInfo needs immediate release (full completion required)
  - Documents why NAR uses background release (enables streaming while preventing duplicate downloads)

  3. Better Variable Naming (pkg/cache/cache.go:2454, 2527)

  Renamed waitChan to completionChan in both locations:
  - More descriptive name that better conveys the purpose
  - Appears in two places within coordinateDownload function

  4. Test Comment Improvement (pkg/cache/cache_test.go:1917-1918)

  Improved the comment explaining the fake content length:
  - Changed from inline comment to full line comment
  - Better explains that the simplified size is intentional for the streaming test

  5. Test Timeout Adjustments (pkg/cache/cache_test.go:1986, 1994)

  Increased timeouts to be more CI-friendly:
  - Server start timeout: 5s → 10s
  - Streaming start timeout: 2s → 5s
  - Reduces likelihood of flaky test failures in CI environments

  Verification

  All changes have been verified:
  - ✅ Linter passes with 0 issues
  - ✅ TestNarStreaming passes
  - ✅ All cache package tests pass with race detector enabled

  The code is now ready with all the high and medium priority suggestions from the review implemented.
@kalbasit
kalbasit force-pushed the 01-22-feat_implement_concurrent_nar_streaming branch from aadeb38 to cb4d0d1 Compare January 23, 2026 04:12
@kalbasit

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces concurrent NAR streaming to improve client experience with large files. The changes are well-structured, refactoring coordinateDownload to support both blocking and non-blocking (streaming) modes via a waitForStorage flag. The distributed lock handling is cleverly modified to support this, holding the lock in the background for streaming downloads to prevent redundant work while allowing the client to receive data immediately. A race condition in GetNar is also correctly addressed by waiting for the asset to be fully stored. The addition of TestNarStreaming provides excellent test coverage for the new functionality. The implementation appears robust and correct.

@kalbasit
kalbasit enabled auto-merge (squash) January 23, 2026 04:17
@kalbasit
kalbasit merged commit c6ce7c6 into main Jan 23, 2026
@kalbasit
kalbasit deleted the 01-22-feat_implement_concurrent_nar_streaming branch January 23, 2026 04:22
@kalbasit

Copy link
Copy Markdown
Owner Author

Backport failed for release-0.7, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release-0.7
git worktree add -d .worktree/backport-619-to-release-0.7 origin/release-0.7
cd .worktree/backport-619-to-release-0.7
git switch --create backport-619-to-release-0.7
git cherry-pick -x c6ce7c6f69f81972ba4b1ffc4c2e7b3db716543e

kalbasit added a commit that referenced this pull request Jan 23, 2026
Implement concurrent NAR streaming to clients as soon as the download
starts
from an upstream cache. Previously, ncps would download the entire NAR
into its
store before starting to stream it back to the client. For large NAR
files,
this often led to client timeouts.

This change refactors coordinateDownload to allow an early return (after
the
download starts) for jobs that support streaming, like NAR downloads. It
also
ensures that the distributed lock is held until the asset is fully
stored in
the background, preventing other instances from starting a redundant
download
while allowing the current instance to stream the file being downloaded.

Detailed changes:
- Refactored coordinateDownload to accept a waitForStorage boolean.
- Updated prePullNar to call coordinateDownload with
waitForStorage=false.
- Updated prePullNarInfo to call coordinateDownload with
waitForStorage=true.
- Modified distributed lock handling in coordinateDownload to release
the lock
  in a background goroutine when waitForStorage is false.
- Updated GetNar to handle the -1 size returned during streaming and
correctly
  stream from the temporary file using io.Pipe.
- Fixed a race condition in GetNar where clients could receive EOF
before the
NAR was fully stored in final storage by waiting for storage completion
in the
  streaming goroutine.
- Added TestNarStreaming to verify the fix and ensured it checks for
asset
  presence after completion.
- Updated TestConcurrentDownload_CancelOneClient_OthersContinue to
handle -1
  size during streaming.

fixes #618

(cherry picked from commit c6ce7c6)
kalbasit added a commit that referenced this pull request Jan 23, 2026
Implement concurrent NAR streaming to clients as soon as the download
starts
from an upstream cache. Previously, ncps would download the entire NAR
into its
store before starting to stream it back to the client. For large NAR
files,
this often led to client timeouts.

This change refactors coordinateDownload to allow an early return (after
the
download starts) for jobs that support streaming, like NAR downloads. It
also
ensures that the distributed lock is held until the asset is fully
stored in
the background, preventing other instances from starting a redundant
download
while allowing the current instance to stream the file being downloaded.

Detailed changes:
- Refactored coordinateDownload to accept a waitForStorage boolean.
- Updated prePullNar to call coordinateDownload with
waitForStorage=false.
- Updated prePullNarInfo to call coordinateDownload with
waitForStorage=true.
- Modified distributed lock handling in coordinateDownload to release
the lock
  in a background goroutine when waitForStorage is false.
- Updated GetNar to handle the -1 size returned during streaming and
correctly
  stream from the temporary file using io.Pipe.
- Fixed a race condition in GetNar where clients could receive EOF
before the
NAR was fully stored in final storage by waiting for storage completion
in the
  streaming goroutine.
- Added TestNarStreaming to verify the fix and ensured it checks for
asset
  presence after completion.
- Updated TestConcurrentDownload_CancelOneClient_OthersContinue to
handle -1
  size during streaming.

fixes #618

(cherry picked from commit c6ce7c6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport release-0.7 Backport PR automatically enhancement New feature or request go Pull requests that update go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Large files cause timeout issues downstream

1 participant