Connection Pool

General coroutine based connection pool implmentated with Channel.

Connection pool is used to reduce the TCP reconnect overhead, can be used to increase the application performance.

You can use the build-in ConnectionPool to manage a pool of connection to storage servers or remote services.

Methods

  • Swoole\ConnectionPool->__construct
  • Swoole\ConnectionPool->make
  • Swoole\ConnectionPool->fill
  • Swoole\ConnectionPool->get
  • Swoole\ConnectionPool->put
  • Swoole\ConnectionPool->close

    Example

    <?php
    declare(strict_types=1);
    
    use Swoole\Coroutine;
    use Swoole\Database\RedisConfig;
    use Swoole\Database\RedisPool;
    use Swoole\Runtime;
    
    const N = 10;
    Runtime::enableCoroutine();
    Coroutine\run(function () {
        $pool = new RedisPool((new RedisConfig)
            ->withHost('127.0.0.1')
            ->withPort(6379)
            ->withAuth('')
            ->withDbIndex(0)
            ->withTimeout(1)
        );
        for ($n = N; $n--;) {
            Coroutine::create(function () use ($pool) {
                $redis = $pool->get();
                $result = $redis->set('foo', 'bar');
                if (!$result) {
                    throw new RuntimeException('Set failed');
                }
                $result = $redis->get('foo');
                if ($result !== 'bar') {
                    throw new RuntimeException('Get failed');
                }
                $pool->put($redis);
            });
        }
    });