| Crates.io | zc_buffer |
| lib.rs | zc_buffer |
| version | 0.1.0 |
| created_at | 2025-05-12 15:06:17.278555+00 |
| updated_at | 2025-05-12 15:06:17.278555+00 |
| description | High-Performance Rust Zero-Copy Inspired Buffer Library based on Bytes |
| homepage | https://github.com/GlorYouth/zc_buffer_rust |
| repository | https://github.com/GlorYouth/zc_buffer_rust |
| max_upload_size | |
| id | 1670729 |
| size | 363,565 |
zc_buffer_rust is a high-performance zero-copy inspired buffer library written in Rust, based on the bytes crate. It
aims to provide efficient asynchronous data processing capabilities. Through a well-designed Manager Actor model,
Agent submission mechanism, and Handle interaction interface, the library implements data reservation, chunked
submission, automatic group merging, and finalization processing. It is particularly suitable for stream data processing
scenarios requiring high throughput and low latency.
The library mainly consists of the following core modules and structures:
Manager:
Handle via MPSC channels and processes them asynchronously.finalize mechanism to process all remaining groups and report information about failed groups.ZeroCopyHandle (Operations Handle):
Manager, encapsulating asynchronous communication logic.reserve_writer), returning a SubmitAgent.finalize method to trigger the Manager's finalization process.Manager connection.SubmitAgent, SingleAgent, ChunkAgent (Submission Agents):
SubmitAgent: Represents a successfully acquired write reservation credential; users need to convert it to either
SingleAgent or ChunkAgent.SingleAgent: Used for executing single, complete data submissions.ChunkAgent: Used for executing chunked data submissions, allowing multiple data chunks to be added and then
committed together via the commit method.Manager of unfinished reservations upon Drop, ensuring resource state
consistency.types.rs (Core Type Definitions):
ReservationId, AbsoluteOffset, GroupId.ReserveRequest, SubmitBytesRequest) and reply channel types.FailedReservationInfo, FailedGroupData, SuccessfulGroupData, GroupState, etc.,
to describe the state of reservations, groups, and processing results.error.rs (Error Handling):
ManagerError (internal Manager errors) and BufferError (top-level errors users might encounter when
interacting with the Handle).Bytes type, aiming to reduce unnecessary data copies and improve performance.Manager are asynchronous, based on the tokio runtime.SingleAgent or in chunks via ChunkAgent.Manager aggregates logically adjacent or related reservations into groups (GroupState).min_group_commit_size threshold, the group is sealed.Manager of their unfinished reservations upon Drop.Manager records information about failed reservations.finalize process handles all remaining groups, sending successfully merged data out and dispatching
information about unsuccessfully processed groups (including committed data chunks and details of failed
reservations) to a dedicated consumer via a failure data channel.The src/main.rs file in the project provides a detailed example of how to use the library:
handle.reserve_writer().SubmitAgent into a SingleAgent or ChunkAgent.handle.finalize() to process all unfinished reservations and groups and to obtain a final report.The project includes a benchmark test located in benches/zc_buffer_benchmark.rs. You can run it using Criterion:
cargo bench
zc_buffer_rust 是一个使用 Rust 语言编写的高性能零拷贝缓冲库,它基于 bytes crate,旨在提供高效的异步数据处理能力。该库通过精心设计的 Manager Actor模型、Agent 提交机制以及 Handle 交互句柄,实现了数据的预留、分块提交、自动分组合并与最终化处理,特别适用于需要高吞吐量和低延迟的流数据处理场景。
该库主要由以下几个核心模块和结构组成:
Manager (管理器):
Handle 的请求,并异步处理。finalize 机制,用于处理所有剩余的分组,并报告处理失败的分组信息。ZeroCopyHandle (操作句柄):
Manager 交互的入口,封装了异步通信逻辑。reserve_writer),并返回一个 SubmitAgent。finalize 方法来触发 Manager 的最终化处理流程。Manager 连接。SubmitAgent, SingleAgent, ChunkAgent (提交代理):
SubmitAgent: 代表一个已成功获取的预留写入凭证,用户需将其转换为 SingleAgent 或 ChunkAgent。SingleAgent: 用于执行单次完整数据提交。ChunkAgent: 用于执行分块数据提交,允许多次添加数据块,最后通过 commit 方法统一提交。Drop 时会自动通知 Manager 未完成的预留,确保资源状态的一致性。types.rs (核心类型定义):
ReservationId, AbsoluteOffset, GroupId 等基础类型。ReserveRequest, SubmitBytesRequest) 和回复通道类型。FailedReservationInfo, FailedGroupData, SuccessfulGroupData, GroupState
等,用于描述预留、分组和处理结果的状态。error.rs (错误处理):
ManagerError (Manager 内部错误) 和 BufferError (用户与 Handle 交互时可能遇到的顶层错误)。Bytes 类型,旨在减少不必要的数据拷贝,提高性能。Manager 的交互都是异步的,基于 tokio 运行时。SingleAgent 一次性提交完整数据,或通过 ChunkAgent 分块提交数据。Manager 会将逻辑上相邻或相关的预留聚合到分组 (GroupState) 中。min_group_commit_size 阈值时,分组会被密封。Manager 其未完成的预留。Manager 记录失败的预留信息。finalize 过程会处理所有剩余的分组,将成功合并的数据发送出去,并将无法成功处理的分组信息(包括已提交的数据块和失败的预留详情)通过失败数据通道发送给专门的消费者。项目中的 src/main.rs 文件提供了一个详细的示例,展示了如何使用该库:
handle.reserve_writer() 请求预留。SubmitAgent 转换为 SingleAgent 或 ChunkAgent。handle.finalize() 来处理所有未完成的预留和分组,并获取最终报告。项目包含了一个基准测试,位于 benches/zc_buffer_benchmark.rs。 你可以使用 Criterion 来运行它:
cargo bench