连接池
RPC 模块的 TCP 传输器可以配合 kuiper\swoole\pool 连接池复用连接。
HTTP + Guzzle 传输器由 Guzzle 自身管理连接,不需要
PooledTransporter。
1. PoolInterface
php
interface PoolInterface
{
public function take(): mixed;
public function release(mixed $connection): void;
public function discard(mixed $connection): void;
public function close(): void;
public function getName(): string;
}take():取出连接release():归还健康连接discard():丢弃损坏连接,不再复用close():关闭池
2. PoolConfig
php
$config = new PoolConfig([
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => -1,
'aged_timeout' => -1,
]);| 选项 | 默认值 | 说明 |
|---|---|---|
min_connections | 1 | 初始化时预创建连接数 |
max_connections | 10 | 最大连接数 |
connect_timeout | 10.0 | 建立连接超时 |
wait_timeout | 3.0 | 从池中取连接超时 |
heartbeat | -1 | 心跳间隔,-1 表示关闭(当前框架内部未消费,预留配置) |
max_idle_time | -1 | 最大空闲时间,-1 表示不限制(当前框架内部未消费,预留配置) |
aged_timeout | 300 | 连接最大存活时间(秒),超时后归还时销毁重建;-1 表示永不过期 |
3. PoolFactory
php
use kuiper\swoole\pool\PoolFactory;
$poolFactory = new PoolFactory(
coroutineEnabled: false, // 非 Swoole 环境
// 可选:poolName => PoolConfig
);coroutineEnabled=true:使用DefaultConnectionPool(Swoole 协程)coroutineEnabled=false:使用SingleConnectionPool(单连接复用)
非 Swoole 环境(FrankenPHP、php-fpm)使用:
php
$poolFactory = new PoolFactory(false);在容器中可直接使用 PoolConfiguration:
php
'configuration' => [
\kuiper\swoole\PoolConfiguration::class,
],4. 创建连接池
php
$pool = $poolFactory->create('demo.DemoService', function ($id, &$connection) use ($endpoint) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_connect($socket, $endpoint->getHost(), $endpoint->getPort())) {
throw new \RuntimeException('connect failed');
}
$connection = $socket;
return $socket;
});连接工厂签名:
php
function (int $id, mixed &$connection): mixed可以直接返回连接,也可以通过引用参数设置;连接池优先使用 $connection。
5. PooledTransporter
php
use kuiper\rpc\transporter\PooledTransporter;
use kuiper\rpc\transporter\SocketTcpTransporter;
$delegate = new SocketTcpTransporter($endpoint);
$transporter = new PooledTransporter($pool, $delegate, healthCheck: function ($connection) {
return is_resource($connection);
});行为:
take()取连接delegate->sendDirect($connection, $request)- 成功:
healthCheck通过则release(),否则discard() - 异常:
discard()并重新抛出
健康检查
php
$transporter = new PooledTransporter(
$pool,
$delegate,
healthCheck: static fn ($connection): bool => is_resource($connection)
);返回 false 时连接会被丢弃,不会归还池中。
6. PooledSession
php
$session = new PooledSession($pool, $connection, $transporter, $request);
$response = $session->send();PooledSession 在 send() 时自动:
- 成功 →
release() - 异常 →
discard()
适合需要延迟发送的调用链。
7. 使用场景
| 传输器 | 是否需要连接池 |
|---|---|
GuzzleHttpTransporter | 否,Guzzle 自带连接管理 |
SocketTcpTransporter | 是,推荐配合 PooledTransporter |
SwooleTcpTransporter | 视场景,协程下可配合池 |
SwooleCoroutineTcpTransporter | 视场景 |
8. 容器配置
php
'configuration' => [
\kuiper\swoole\PoolConfiguration::class,
],PoolConfiguration 提供默认 PoolFactoryInterface:
php
PoolFactoryInterface::class => factory(
static fn (): PoolFactoryInterface => new PoolFactory(false)
);需要自定义 PoolConfig 时,可覆盖该 Bean:
php
#[Bean]
public function poolFactory(): PoolFactoryInterface
{
return new PoolFactory(false, [
'demo.DemoService' => new PoolConfig(['max_connections' => 20]),
]);
}9. 注意事项
GuzzleHttpTransporter::sendDirect()不使用连接参数,HTTP 场景无需池。SingleConnectionPool只维护一个连接,适合非协程环境。DefaultConnectionPool使用 Swoole 协程 Channel,需要ext-swoole。- 连接损坏时必须
discard(),不要release()。