feat(grpc-client): add support for multiple client pool and load balancing#7667
Merged
limingxinleo merged 15 commits intohyperf:masterfrom Dec 16, 2025
Merged
Conversation
huangdijia
commented
Dec 13, 2025
Member
Author
huangdijia
left a comment
There was a problem hiding this comment.
🔍 代码审查报告
✅ 优点
- 实现了多客户端池功能,支持负载均衡
- 代码结构清晰,完全符合 PSR-12 标准
- 很好地保持了向后兼容性
- 将配置验证移到构造函数是好的实践
⚠️ 必须修复的问题
1. 资源泄漏风险(严重)
public function __destruct()
{
$this->grpcClient?->close(false);
// 缺少对 grpcClients 数组中其他客户端的清理
}建议修复为:
public function __destruct()
{
$this->grpcClient?->close(false);
if ($this->grpcClients !== null) {
foreach ($this->grpcClients as $client) {
$client?->close(false);
}
}
}2. 并发安全问题
$this->grpcClient在多线程环境下可能被并发修改- 每次调用
_getGrpcClient()都可能改变客户端引用,可能影响事务一致性
💡 建议改进
1. 改进负载均衡策略
当前使用随机选择可能导致负载分布不均,建议考虑轮询或其他更可控的策略。
2. 添加配置验证
建议限制 client_count 的最大值,避免创建过多客户端导致资源耗尽:
if ($count > 50) {
throw new InvalidArgumentException('client_count should not exceed 50');
}3. 线程安全优化
考虑缓存选中的客户端,避免每次调用都重新选择。
📝 其他建议
- 添加单元测试覆盖多客户端场景
- 在文档中说明多客户端的内存和连接影响
- 考虑添加客户端健康检查机制
总结
功能实现正确,代码质量良好。在修复资源清理问题后可以合并。
There was a problem hiding this comment.
Pull request overview
This PR introduces client pooling and load balancing capabilities to the gRPC client implementation, allowing users to create multiple gRPC client instances and distribute requests across them for improved performance in high-load scenarios.
Key changes:
- Add
client_countoption to enable creation of multiple client instances - Implement random client selection from pool for load distribution
- Refactor initialization logic to handle single client, multiple clients, and custom client scenarios
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ncing - Add support for client_count option to create multiple gRPC clients - Implement random client selection from pool for load distribution - Refactor init() method to handle single and multiple client scenarios - Add property for storing multiple gRPC client instances - Improve code comments for better understanding This enhancement allows for better load balancing and connection pooling when dealing with high-load gRPC scenarios.
…or multiple clients
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
6ccbb4a to
d7b59bb
Compare
… mechanism for client access
…o use unique lock keys
…s for client access
limingxinleo
approved these changes
Dec 16, 2025
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.
Summary
client_countoption to create multiple gRPC clientsinit()method to handle single and multiple client scenariosDescription
This enhancement allows for better load balancing and connection pooling when dealing with high-load gRPC scenarios. By specifying a
client_countoption greater than 1, multiple gRPC client instances will be created, and requests will be randomly distributed across them.Usage Example
Test plan
client_countoptionclient_countvaluesFiles changed
src/grpc-client/src/BaseClient.php- Add support for multiple client pool and load balancingBreaking changes
None. This is a backward-compatible enhancement.
Checklist