refactor(cache)!: extract hybrid_cache package#2477
Merged
Conversation
…_threshold to auto_memory_limit * split HybridCache and stores into internal/hybrid_cache package * introduce BackingStore abstraction with BufferStore and FileStore * rename CacheThreshold to AutoMemoryLimit in config/env/bootstrap * update stream/request integration and related tests
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors stream caching by extracting the former internal/mem.HybridCache into a dedicated internal/hybrid_cache package (with BackingStore abstractions), and introduces a breaking configuration rename from cache_threshold to auto_memory_limit to control when Go-managed memory vs manual/file-backed caching is used.
Changes:
- Extract HybridCache into
internal/hybrid_cacheand addBackingStoreimplementations (BufferStore,FileStore). - Remove
pkg/buffer’s oldReader/PeekFileutilities and consolidateNewByteBlockinto a smaller file. - Rename config
cache_threshold→auto_memory_limitand update bootstrap + tests accordingly.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/buffer/utils.go | Moves provider interface types out; keeps adapter helpers. |
| pkg/buffer/type.go | Defines WriteAtSeekerProvider / ReadAtSeekerProvider alongside core buffer types. |
| pkg/buffer/file.go | Removes PeekFile implementation (deleted). |
| pkg/buffer/bytes.go | Removes buffer.Reader and embedded byteBlock (deleted). |
| pkg/buffer/buffer.go | Reintroduces NewByteBlock + byteBlock in a dedicated file. |
| internal/stream/util.go | Switches stream section caching to internal/hybrid_cache. |
| internal/stream/stream.go | Reworks stream caching to always use the new HybridCache path. |
| internal/stream/stream_test.go | Updates tests for the AutoMemoryLimit rename/behavior. |
| internal/net/request.go | Switches downloader caching to internal/hybrid_cache. |
| internal/mem/cache.go | Removes old HybridCache implementation (deleted). |
| internal/hybrid_cache/type.go | Introduces BackingStore interface. |
| internal/hybrid_cache/hybrid_cache.go | New HybridCache implementation and policy selection logic. |
| internal/hybrid_cache/file.go | File-backed store implementation and constructors. |
| internal/hybrid_cache/file_test.go | Updates tests to new package/types (MultiFileStore, GrowTo). |
| internal/hybrid_cache/buffer.go | Adds BufferStore (Go-managed memory backing store). |
| internal/hybrid_cache/buffer_test.go | Adds/updates tests for BufferStore. |
| internal/conf/var.go | Renames threshold var to AutoMemoryLimit and updates comments. |
| internal/conf/config.go | Renames config field/tag to auto_memory_limit and updates defaults. |
| internal/bootstrap/config.go | Reads and logs AutoMemoryLimit instead of CacheThreshold. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 / 描述
主要改动
HybridCache 的缓存策略
auto_memory_limit的数据流,仅使用[]byte缓存(Go自动管理内存),无回退文件缓存auto_memory_limit的数据流,优先使用手动管理内存(Unix Mmap或者Windows VirtualAlloc)用作缓存,内存不足回退到文件缓存min_free_memory小于0时,仅使用文件缓存HybridCache 相关配置项:
auto_memory_limit单位MB[]byte缓存数据流的限制,内存为自动管理,直到GC。min_free_memory单位MBmax_block_limit单位BM手动管理内存单次的扩容大小,超过该阈值将分多次扩容(为了检测内存是否充足)。min_free_memory大于0时,也限制多线程下载的分片大小Motivation and Context / 背景
Relate #2460
How Has This Been Tested? / 测试
go test -run '^(TestRangeRead|TestPreHash|TestStreamSectionReader)$' ./internal/streamgo test -run '^(TestDownloadOrder|TestHighConcurrency|TestDownloadSingle)$' ./internal/netgo test ./internal/hybrid_cacheChecklist / 检查清单
我已阅读 CONTRIBUTING 文档。
go fmtor prettier.我已使用
go fmt或 prettier 格式化提交的代码。我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
我已相应更新了相关仓库(若适用)。