Skip to content

连接池 ​

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_connections1初始化时预创建连接数
max_connections10最大连接数
connect_timeout10.0建立连接超时
wait_timeout3.0从池中取连接超时
heartbeat-1心跳间隔,-1 表示关闭(当前框架内部未消费,预留配置)
max_idle_time-1最大空闲时间,-1 表示不限制(当前框架内部未消费,预留配置)
aged_timeout300连接最大存活时间(秒),超时后归还时销毁重建;-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);
});

行为:

  1. take() 取连接
  2. delegate->sendDirect($connection, $request)
  3. 成功:healthCheck 通过则 release(),否则 discard()
  4. 异常: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()。

基于 MIT 许可发布