Repository navigation
refactor: unify stream caching via HybridCache - #2460
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors stream buffering/caching to use a new HybridCache abstraction (memory-first with file fallback) and removes the prior mmap-based buffering path, while also introducing guarded memory allocation tied to a configurable minimum free-memory threshold.
Changes:
- Added
internal/mem(LinearMemory, GuardedMemory, HybridCache) and wired it into stream caching + downloader buffering. - Introduced
pkg/buffersection/stream abstractions (Section,StreamBuffer) and updated stream readers to use them. - Added
min_free_memoryMBconfiguration and updated bootstrap logic to derive memory/buffer limits from system memory.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/pool/pool.go | Simplifies generic pool and adds Close() for closer-based lifecycle management. |
| pkg/buffer/utils.go | Adds helpers to obtain reader/writer views for Section implementations. |
| pkg/buffer/type.go | Introduces SectionReader/SectionWriter interfaces. |
| pkg/buffer/stream.go | Adds a concurrent 1-reader/1-writer streaming buffer over a Section. |
| pkg/buffer/bytes.go | Adds Close() to Reader and introduces byteSection (Section) backed by a byte slice. |
| internal/stream/util.go | Refactors StreamSectionReader to use pooled sections and HybridCache instead of mmap/temp-file logic. |
| internal/stream/stream.go | Refactors FileStream caching/peek behavior to use HybridCache for large streams. |
| internal/stream/stream_test.go | Updates tests to exercise the new caching thresholds and behavior. |
| internal/net/request.go | Refactors downloader chunk buffering to use buffer.StreamBuffer + optional HybridCache. |
| internal/net/request_test.go | Removes unused test helpers after downloader refactor. |
| internal/mem/utils.go | Adds memory growth checks and guarded allocation wrappers. |
| internal/mem/type.go | Defines LinearMemory and GrowCheck primitives. |
| internal/mem/mem_windows.go | Implements LinearMemory via Windows VirtualAlloc. |
| internal/mem/mem_unix.go | Implements LinearMemory via mmap/mprotect on unix. |
| internal/mem/mem_other.go | Fallback LinearMemory implementation using Go slices on other platforms. |
| internal/mem/cache.go | Implements HybridCache (memory-first, file fallback) and a reader over cached sections. |
| internal/conf/var.go | Adds MinFreeMemory global var. |
| internal/conf/config.go | Adds min_free_memoryMB config field. |
| internal/bootstrap/config.go | Computes and logs MinFreeMemory, MaxBufferLimit, and MmapThreshold from system memory/config. |
| drivers/teldrive/types.go | Updates interface type name to the new StreamSectionReader. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
j2rong4cn
marked this pull request as ready for review
May 11, 2026 05:53
5 of 10 tasks
EnterTang
pushed a commit
to EnterTang/OpenList
that referenced
this pull request
Jul 12, 2026
* add LinearMemory * replace mmap with LinearMemory * remove unused code * add GuardedMemory; add `min_free_memoryMB` conf * add HybridCache and StreamBuffer * log * rename SizedReadWriterAt to Section * 重构 FileStream,改用 HybridCache * 重构 HybridCache,更新方法名并添加回滚功能;优化请求和流处理逻辑 * 重构 StreamSectionReader 接口,使用HybridCache * 在 NewGuardedMemory 函数中添加了对 LinearMemory 的最终化处理,以确保内存释放 * . * 优化检查逻辑 * 重命名 * 改进、重命名 * 修复 * 添加测试 * 移除HybridCacheReader并引入DynamicReadAtSeeker * 重构缓存读取逻辑,简化代码并引入ReadFromN方法 * 优化缓存配置注释并修复下载器部分大小限制逻辑 * 优化中断逻辑 * 优化下载器代码 * 修复bug * HybridCache添加多文件缓存模式 * 优化下载器并发,添加测试 * 修复bug * 重命名+注释 * . * fix(net): always cleanup downloader on interrupt * fix(net): guard chunk enqueue with context cancel * fix(net): update interrupt logic and add download interrupt test * fix(test): update concurrency limit in high concurrency test * refactor(buffer): simplify ReadAt logic * refactor(config): update memory configuration logic * . --------- Co-authored-by: Suyunmeng <Susus0175@proton.me>
8 of 11 tasks
cjd6568358
pushed a commit
to cjd6568358/OpenList-zig
that referenced
this pull request
Oct 18, 2026
* add LinearMemory * replace mmap with LinearMemory * remove unused code * add GuardedMemory; add `min_free_memoryMB` conf * add HybridCache and StreamBuffer * log * rename SizedReadWriterAt to Section * 重构 FileStream,改用 HybridCache * 重构 HybridCache,更新方法名并添加回滚功能;优化请求和流处理逻辑 * 重构 StreamSectionReader 接口,使用HybridCache * 在 NewGuardedMemory 函数中添加了对 LinearMemory 的最终化处理,以确保内存释放 * . * 优化检查逻辑 * 重命名 * 改进、重命名 * 修复 * 添加测试 * 移除HybridCacheReader并引入DynamicReadAtSeeker * 重构缓存读取逻辑,简化代码并引入ReadFromN方法 * 优化缓存配置注释并修复下载器部分大小限制逻辑 * 优化中断逻辑 * 优化下载器代码 * 修复bug * HybridCache添加多文件缓存模式 * 优化下载器并发,添加测试 * 修复bug * 重命名+注释 * . * fix(net): always cleanup downloader on interrupt * fix(net): guard chunk enqueue with context cancel * fix(net): update interrupt logic and add download interrupt test * fix(test): update concurrency limit in high concurrency test * refactor(buffer): simplify ReadAt logic * refactor(config): update memory configuration logic * . --------- Co-authored-by: Suyunmeng <Susus0175@proton.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description / 描述
🛠️ 主要改动
mmap实现为LinearMemory。min_free_memory配置项。⚙️ 配置
新增配置项:
cache_threshold单位MBmin_free_memory单位MBmax_block_limit单位BMmin_free_memory大于0时,也限制多线程下载的分片大小废弃配置项:
max_buffer_limitMBmmap_thresholdMBMotivation and Context / 背景
Close #2395
How Has This Been Tested? / 测试
go test -run '^(TestRangeRead|TestPreHash|TestStreamSectionReader)$' ./internal/streamgo test -run '^(TestDownloadOrder|TestHighConcurrency|TestDownloadSingle)$' ./internal/netgo test -run '^TestMultiFileCache$' ./internal/cacheChecklist / 检查清单
我已阅读 CONTRIBUTING 文档。
go fmtor prettier.我已使用
go fmt或 prettier 格式化提交的代码。我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
我已相应更新了相关仓库(若适用)。