优化网络层并发模型:实现连接协程“瘦身”#409
Open
redfox1999 wants to merge 6 commits into
Open
Conversation
1. 优化连接关闭逻辑,在reader协程中统一执行清理操作 2. 新增client_v2压测客户端,支持多尺寸数据包、随机发包策略与重连模拟 3. 扩展c10k测试服务端,添加小/中/大数据包路由处理 4. 添加client_v2二进制编译支持与gitignore规则
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.
优化网络层并发模型:实现连接协程“瘦身”
🔍 概述 (Overview)
本 PR 对网络层的并发模型进行了重构,通过合并与解耦不必要的调度损耗,实现了每个连接减少 1 个常驻协程的优化目标:
SendMsg):单连接常驻协程由 2 个减至 1 个(C10K 峰值由 20K 降至 10K)。SendBuffMsg):单连接常驻协程由 3 个减至 2 个(C10K 峰值由 30K 降至 20K)。优化后,系统在 C10K 基准测试(10,000 并发 × 1,000 次请求,共 10,000,000 次请求)中表现出了更低的调度开销、更高的吞吐量以及更稳定的长尾时延。
📊 优化前后全指标深度对比 (Benchmark Comparison)
SendMsgSendMsg(优化)SendBuffMsgSendBuffMsg(优化)💡 核心优化成果与技术分析
1. 大幅降低 Go 运行时调度成本 (Goroutine Scheduling Cost)
在并发连接达到 10,000 时,两种方案均成功在线减少了 10,000 个协程的上下文切换与调度开销。这使得 CPU 能够将更多算力投入到网络 I/O 与数据序列化本身,直接催生了 QPS 的全面普涨。优化后的同步方案(
SendMsg)跑出了现阶段的性能峰值 63,284.47 QPS。2. 异步缓冲方案 (
SendBuffMsg) 瘦身收益显著SendBuffMsg有效 QPS 提升了 1.8%(由 60.3K 升至 61.4K)。3. 内存控制与 GC 稳定性
SendMsg闲置内存回落至 5M,SendBuffMsg回落至 9M,无任何资源残留与协程泄漏隐患。🛠️ 改动清单 (Changes)
SendMsg逻辑,实现单连接单协程(1-Goroutine per Connection)的高效读写设计。SendBuffMsg机制,将单连接常驻协程缩减至 2 个,精简 Channel 监听链条。🧪 测试验证 (Verification)
展开/收起 数据展示