Skip to content

feat(grpc-client): add support for multiple client pool and load balancing#7667

Merged
limingxinleo merged 15 commits intohyperf:masterfrom
huangdijia:feature/grpc-client-pool-support
Dec 16, 2025
Merged

feat(grpc-client): add support for multiple client pool and load balancing#7667
limingxinleo merged 15 commits intohyperf:masterfrom
huangdijia:feature/grpc-client-pool-support

Conversation

@huangdijia
Copy link
Member

Summary

  • 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

Description

This enhancement allows for better load balancing and connection pooling when dealing with high-load gRPC scenarios. By specifying a client_count option greater than 1, multiple gRPC client instances will be created, and requests will be randomly distributed across them.

Usage Example

// Single client (existing behavior)
$client = new BaseClient('localhost:50051', [
    'options' => [...]
]);

// Multiple clients with load balancing (new feature)
$client = new BaseClient('localhost:50051', [
    'client_count' => 3,  // Create 3 client instances
    'options' => [...]
]);

Test plan

  • Test single client creation (backward compatibility)
  • Test multiple client creation with client_count option
  • Verify random client selection works correctly
  • Test with invalid client_count values
  • Performance testing under load

Files changed

  • src/grpc-client/src/BaseClient.php - Add support for multiple client pool and load balancing

Breaking changes

None. This is a backward-compatible enhancement.

Checklist

  • Code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Copilot AI review requested due to automatic review settings December 13, 2025 01:33
Copy link
Member Author

@huangdijia huangdijia left a comment

Choose a reason for hiding this comment

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

🔍 代码审查报告

✅ 优点

  • 实现了多客户端池功能,支持负载均衡
  • 代码结构清晰,完全符合 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. 线程安全优化
考虑缓存选中的客户端,避免每次调用都重新选择。

📝 其他建议

  • 添加单元测试覆盖多客户端场景
  • 在文档中说明多客户端的内存和连接影响
  • 考虑添加客户端健康检查机制

总结

功能实现正确,代码质量良好。在修复资源清理问题后可以合并。

Copy link

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 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_count option 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.

huangdijia and others added 11 commits December 14, 2025 20:07
…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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@huangdijia huangdijia force-pushed the feature/grpc-client-pool-support branch from 6ccbb4a to d7b59bb Compare December 14, 2025 12:07
@limingxinleo limingxinleo merged commit 53f2d38 into hyperf:master Dec 16, 2025
77 checks passed
@huangdijia huangdijia deleted the feature/grpc-client-pool-support branch December 16, 2025 08:26
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