Skip to content

refactor: unify stream caching via HybridCache#2460

Open
j2rong4cn wants to merge 23 commits into
OpenListTeam:mainfrom
j2rong4cn:pr/refactor6
Open

refactor: unify stream caching via HybridCache#2460
j2rong4cn wants to merge 23 commits into
OpenListTeam:mainfrom
j2rong4cn:pr/refactor6

Conversation

@j2rong4cn
Copy link
Copy Markdown
Member

@j2rong4cn j2rong4cn commented May 10, 2026

Description / 描述

🛠️ 主要改动

  • 引入 LinearMemoryHybridCache,用于改进内存管理和缓存机制。
  • 替换 mmap 实现为 LinearMemory
  • 新增 GuardedMemory,并增加 min_free_memoryMB 配置项。
  • 重构 FileStreamStreamSectionReader 接口,统一使用 HybridCache。
  • HybridCache 支持内存优先,失败时回退到文件缓存

⚙️ 配置

  • 新增配置项:

    • cache_threshold 单位MB
      • 默认值:4
      • 大于该阈值的数据流将使用HybridCache,可主动释放内存。 如果为0,HybridCache会使用文件缓存,不占用内存。 否则使用Go的内存分配,直到GC回收。
    • max_block_limit 单位BM
      • 默认值:自动大小,总内存的3%,最小4,最大64
      • 限制HybridCache单次内存、磁盘缓存的扩容大小,超过该阈值将分多次扩容。 CacheThreshold大于0时,也限制 Downloader 的PartSize
    • min_free_memory 单位MB
      • 默认值:自动大小,总内存的10%,最小16,最大1024
      • 最小空闲内存,当内存不足时,HybridCache会回退到文件缓存。
  • 废弃配置项:

    • max_buffer_limitMB
    • mmap_thresholdMB

Motivation and Context / 背景

How Has This Been Tested? / 测试

  • go test ./internal/stream
  • go test -run '^(TestDownloadOrder|TestDownloadSingle)$' ./internal/net

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it’s needed).
    我已相应更新了相关仓库(若适用)。

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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/buffer section/stream abstractions (Section, StreamBuffer) and updated stream readers to use them.
  • Added min_free_memoryMB configuration 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.

Comment thread pkg/buffer/pipe.go
Comment thread pkg/buffer/pipe.go
Comment thread pkg/buffer/bytes.go Outdated
Comment thread pkg/buffer/bytes.go Outdated
Comment thread internal/mem/utils.go
Comment thread internal/stream/util.go Outdated
Comment thread internal/mem/cache.go Outdated
Comment thread internal/mem/mem_unix.go
Comment thread internal/mem/mem_windows.go
Comment thread internal/mem/mem_other.go
@j2rong4cn j2rong4cn marked this pull request as draft May 10, 2026 10:56
@j2rong4cn j2rong4cn requested a review from Copilot May 10, 2026 10:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

Comment thread internal/mem/cache.go
Comment thread internal/mem/cache.go
Comment thread internal/mem/mem_unix.go
Comment thread internal/mem/mem_windows.go
Comment thread internal/mem/mem_other.go
Comment thread pkg/buffer/bytes.go
Comment thread internal/stream/util.go
Comment thread internal/stream/stream.go Outdated
Comment thread internal/net/request.go Outdated
Comment thread internal/stream/util.go
@j2rong4cn j2rong4cn self-assigned this May 11, 2026
@j2rong4cn j2rong4cn marked this pull request as ready for review May 11, 2026 05:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

Comment thread internal/stream/util.go
Comment thread internal/stream/util.go Outdated
Comment thread internal/mem/utils.go Outdated
Comment thread internal/mem/mem_other.go
Comment thread internal/net/request.go
Comment thread internal/net/request.go Outdated
Comment thread internal/net/request.go
Comment thread internal/stream/stream_test.go
Comment thread internal/stream/stream_test.go
Comment thread internal/mem/cache.go Outdated
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.

2 participants