rust-system-event-driven
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust System Event-Driven Best Practices
Rust事件驱动系统编程最佳实践
Comprehensive best practices guide for event-driven system programming in Rust. Contains 42 rules across 8 categories, prioritized by impact to guide async runtime usage, channel communication, threading, networking, and terminal handling.
这是一份关于Rust事件驱动系统编程的综合性最佳实践指南,包含8个类别共42条规则,按影响优先级排序,指导异步运行时使用、通道通信、线程处理、网络编程和终端操作。
When to Apply
适用场景
Reference these guidelines when:
- Building async applications with Tokio or async-std
- Implementing network servers or clients
- Writing terminal user interfaces (TUIs)
- Managing concurrent tasks and shared state
- Handling Unix signals and graceful shutdown
在以下场景中可参考本指南:
- 使用Tokio或async-std构建异步应用
- 实现网络服务器或客户端
- 编写终端用户界面(TUI)
- 管理并发任务与共享状态
- 处理Unix信号与优雅停机
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Async Runtime Patterns | CRITICAL | |
| 2 | Channel Communication | CRITICAL | |
| 3 | Threading & Synchronization | HIGH | |
| 4 | Socket & Network I/O | HIGH | |
| 5 | Terminal & TTY Handling | MEDIUM-HIGH | |
| 6 | Signal & Process Control | MEDIUM | |
| 7 | File I/O Streaming | MEDIUM | |
| 8 | Event Loop Architecture | LOW-MEDIUM | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 异步运行时模式 | 极高 | |
| 2 | 通道通信 | 极高 | |
| 3 | 线程与同步 | 高 | |
| 4 | 套接字与网络I/O | 高 | |
| 5 | 终端与TTY处理 | 中高 | |
| 6 | 信号与进程控制 | 中 | |
| 7 | 文件I/O流 | 中 | |
| 8 | 事件循环架构 | 中低 | |
Quick Reference
快速参考
1. Async Runtime Patterns (CRITICAL)
1. 异步运行时模式(极高)
- - Use spawn_blocking for CPU-bound work
async-spawn-blocking - - Use biased select for priority handling
async-select-biased - - Avoid std blocking calls in async context
async-no-std-block - - Design cancellation-safe async operations
async-cancellation-safe - - Use task-local storage for request context
async-task-local - - Use JoinSet for structured concurrency
async-structured-concurrency
- - 对CPU密集型任务使用spawn_blocking
async-spawn-blocking - - 使用biased select处理优先级
async-select-biased - - 避免在异步上下文中调用标准库阻塞函数
async-no-std-block - - 设计支持取消的异步操作
async-cancellation-safe - - 使用任务本地存储管理请求上下文
async-task-local - - 使用JoinSet实现结构化并发
async-structured-concurrency
2. Channel Communication (CRITICAL)
2. 通道通信(极高)
- - Use bounded channels for backpressure
chan-bounded-backpressure - - Use oneshot channels for request-response
chan-oneshot-response - - Use broadcast channels for fan-out
chan-broadcast-fanout - - Use watch channels for shared state
chan-watch-state - - Use channel closure for graceful shutdown
chan-graceful-shutdown
- - 使用有界通道实现背压
chan-bounded-backpressure - - 使用一次性通道处理请求-响应模式
chan-oneshot-response - - 使用广播通道实现扇出模式
chan-broadcast-fanout - - 使用watch通道管理共享状态
chan-watch-state - - 通过通道关闭实现优雅停机
chan-graceful-shutdown
3. Threading & Synchronization (HIGH)
3. 线程与同步(高)
- - Use Arc<Mutex> for shared mutable state
sync-arc-mutex-shared - - Use RwLock for read-heavy workloads
sync-rwlock-read-heavy - - Use atomics for simple counters and flags
sync-atomic-counters - - Avoid holding std Mutex across await
sync-avoid-lock-await - - Use Semaphore to limit concurrency
sync-semaphore-limit - - Use parking_lot for high-contention locks
sync-parking-lot
- - 使用Arc<Mutex>管理共享可变状态
sync-arc-mutex-shared - - 对读密集型工作负载使用RwLock
sync-rwlock-read-heavy - - 使用原子类型实现简单计数器与标志位
sync-atomic-counters - - 避免在await期间持有标准库Mutex
sync-avoid-lock-await - - 使用Semaphore限制并发数
sync-semaphore-limit - - 对高竞争场景使用parking_lot锁
sync-parking-lot
4. Socket & Network I/O (HIGH)
4. 套接字与网络I/O(高)
- - Split sockets into reader and writer halves
net-split-reader-writer - - Use framing for message-based protocols
net-framing-codec - - Use connection pools for repeated connections
net-connection-pool - - Add timeouts to all network operations
net-timeout-all-io - - Set TCP_NODELAY for low-latency protocols
net-tcp-nodelay - - Implement graceful connection shutdown
net-graceful-disconnect
- - 将套接字拆分为读取和写入两个独立部分
net-split-reader-writer - - 对基于消息的协议使用帧编码
net-framing-codec - - 对重复连接使用连接池
net-connection-pool - - 为所有网络操作添加超时
net-timeout-all-io - - 为低延迟协议设置TCP_NODELAY
net-tcp-nodelay - - 实现连接的优雅断开
net-graceful-disconnect
5. Terminal & TTY Handling (MEDIUM-HIGH)
5. 终端与TTY处理(中高)
- - Always restore terminal state on exit
term-raw-mode-restore - - Use alternate screen for full-screen apps
term-alternate-screen - - Use async event stream for terminal input
term-async-event-stream - - Buffer terminal output for performance
term-buffered-output - - Handle terminal resize events
term-handle-resize
- - 程序退出时务必恢复终端状态
term-raw-mode-restore - - 对全屏应用使用备用屏幕
term-alternate-screen - - 使用异步事件流处理终端输入
term-async-event-stream - - 对终端输出使用缓冲以提升性能
term-buffered-output - - 处理终端窗口大小调整事件
term-handle-resize
6. Signal & Process Control (MEDIUM)
6. 信号与进程控制(中)
- - Handle Ctrl-C for graceful shutdown
sig-ctrl-c-graceful - - Handle Unix signals asynchronously
sig-unix-signals - - Reap child processes to avoid zombies
sig-child-reap - - Set shutdown timeout to force exit
sig-timeout-shutdown
- - 处理Ctrl-C信号以实现优雅停机
sig-ctrl-c-graceful - - 异步处理Unix信号
sig-unix-signals - - 回收子进程以避免僵尸进程
sig-child-reap - - 设置停机超时以强制退出
sig-timeout-shutdown
7. File I/O Streaming (MEDIUM)
7. 文件I/O流(中)
- - Use async file operations in async context
io-async-file-ops - - Stream large files instead of loading entirely
io-stream-large-files - - Use copy_bidirectional for proxying
io-copy-bidirectional - - Use pipes for process communication
io-pipe-communication - - Flush writes before expecting responses
io-flush-before-read
- - 在异步上下文中使用异步文件操作
io-async-file-ops - - 流式处理大文件而非一次性加载
io-stream-large-files - - 使用copy_bidirectional实现代理
io-copy-bidirectional - - 使用管道实现进程间通信
io-pipe-communication - - 读取响应前先刷新写入操作
io-flush-before-read
8. Event Loop Architecture (LOW-MEDIUM)
8. 事件循环架构(中低)
- - Use actor pattern for stateful components
loop-actor-model - - Use typed events over dynamic dispatch
loop-event-types - - Model protocol state as type-safe state machine
loop-state-machine - - Separate I/O from business logic
loop-layered-architecture - - Use CancellationToken for coordinated shutdown
loop-cancellation-token
- - 对有状态组件使用Actor模式
loop-actor-model - - 使用类型化事件替代动态分发
loop-event-types - - 将协议状态建模为类型安全的状态机
loop-state-machine - - 将I/O层与业务逻辑分离
loop-layered-architecture - - 使用CancellationToken实现协同停机
loop-cancellation-token
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明与代码示例:
- 章节定义 - 类别结构与影响级别说明
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |