Skip to content

feat: implement concurrent NAR streaming (#619) - #621

Merged
kalbasit merged 1 commit into
release-0.7from
cherry-pick-619
Jan 23, 2026
Merged

kalbasit merged 1 commit into
release-0.7from
cherry-pick-619

Conversation

@kalbasit

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

(cherry picked from commit c6ce7c6)

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)
@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
@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 files. Previously, the system would fully download large NARs before serving them, leading to client timeouts. The core change refactors the download coordination logic to allow clients to receive data as soon as the download starts, while intelligently managing distributed locks to prevent duplicate downloads and ensure eventual storage. This enhancement drastically improves the user experience for large asset transfers.

Highlights

  • Concurrent NAR Streaming: Implemented the ability to stream NAR (Nix Archive) files to clients as soon as the download begins from an upstream cache, addressing client timeouts for large files.
  • Refactored Download Coordination: Modified the coordinateDownload function to support early returns for streaming jobs by introducing a waitForStorage boolean parameter, allowing different behaviors for NARs and NarInfos.
  • Enhanced Distributed Lock Handling: The distributed lock is now held until the asset is fully stored in the background for streaming NARs, preventing redundant downloads while allowing immediate client streaming. For NarInfo, the lock is released after storage.
  • Race Condition Fix in GetNar: Addressed a race condition where clients could receive an EOF before a NAR was fully stored by ensuring GetNar waits for storage completion during streaming.
  • New Streaming Test Coverage: Added TestNarStreaming to validate the new concurrent streaming behavior and ensure asset presence after completion, alongside an update to TestConcurrentDownload_CancelOneClient_OthersContinue to handle streaming-specific size returns.
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. ↩

Copy link
Copy Markdown
Owner Author

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

@kalbasit
kalbasit enabled auto-merge (squash) January 23, 2026 04:35

@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 NAR files. The changes are well-structured, refactoring coordinateDownload to support both streaming and waiting for full storage. The distributed lock handling is correctly updated to hold the lock in the background during streaming, preventing redundant downloads while allowing clients to receive data immediately. The addition of TestNarStreaming is excellent, as it thoroughly verifies the new functionality and the fix for a potential race condition. The code is of high quality. I have one suggestion to improve maintainability by refactoring a small piece of duplicated code.

Comment thread pkg/cache/cache.go Resolved
@kalbasit
kalbasit merged commit c873afd into release-0.7 Jan 23, 2026
@kalbasit
kalbasit deleted the cherry-pick-619 branch January 23, 2026 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

1 participant