Swoole Coroutine Library

Swoole Library

Swoole Library is a group of Swoole Coroutine build-in functions implmeneted with PHP. Swoole Library has to be installed with composer:

composer require swoole/library

Swoole\Coroutine\WaitGroup

Swoole Coroutine WaitGroup can be used waiting for multiple coroutines finishing.

Swoole\Coroutine\WaitGroup->__construct()

Construct a Swoole Coroutine WaitGroup.

Swoole\Coroutine\WaitGroup->add(int $delta = 1)

Add one or more coroutines into the WaitGroup.

Swoole\Coroutine\WaitGroup->done()

Mark one coroutine as done status.

Swoole\Coroutine\WaitGroup->wait(int $timeout = -1)

Waiting the group of coroutines to be finished until timeout.

Swoole\ConnectionPool

General coroutine based connection pool implmentation with Channel.

Swoole\ConnectionPool->__construct(callable $constructor, int $size = 64, ?string $proxy = null)

Create a new ConnectionPool with constructor function, pool size, optional proxy function.

Swoole\ConnectionPool->make()

Create a new connection and add into the connection pool/

Swoole\ConnectionPool->fill()

Fill the connection popl with defined number of connections.

Swoole\ConnectionPool->get()

Get one connection from the Connection pool to use.

Swoole\ConnectionPool->put($connection)

Put the connection back to connection pool once it is not used anymore.

Swoole\ConnectionPool->close()

Close all the connections in the connection pool.

Swoole\Database\PDOPool

PDO Connection pool implmentation with Swoole\ConnectionPool.

Swoole\Database\PDOPool->__construct(PDOConfig $config, int $size = 64)

Create a PDOPool with the configration.

Swoole\Database\PDOConfig

PDO Connection pool configuration

Swoole\Database\PDOConfig->withHost(string $host)
Swoole\Database\PDOConfig->withUnixSocket(string $unixSocket)
Swoole\Database\PDOConfig->withPort(int $port)
Swoole\Database\PDOConfig->withDbname(string $dbname)
Swoole\Database\PDOConfig->withCharset(string $charset)
Swoole\Database\PDOConfig->withUsername(string $username)
Swoole\Database\PDOConfig->withPassword(string $withPassword)
Swoole\Database\PDOConfig->withOptions(array $options)

Swoole\Database\RedisPool

Redis connection pool implmentation with Swoole\ConnectionPool.

Swoole Coroutine Connection Pool 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);
        });
    }
});
Swoole\Database\RedisPool->__construct(RedisConfig $config, int $size = 64)

Create a RedisPool with the configration.

Swoole\Database\RedisConfig

Redis connection pool configuration

Swoole\Database\RedisConfig->withHost(string $host)
Swoole\Database\RedisConfig->withPort(int $port)
Swoole\Database\RedisConfig->withTimeout(float $timeout)
Swoole\Database\RedisConfig->withReserved(string $reserved)
Swoole\Database\RedisConfig->withRetryInterval(int $retry_interval)
Swoole\Database\RedisConfig->withReadTimeout(float $read_timeout)
Swoole\Database\RedisConfig->withAuth(string $auth)
Swoole\Database\RedisConfig->withDbIndex(int $dbIndex)

Swoole\Database\MysqliPool

MysqliPool connection pool implmentation with Swoole\ConnectionPool.

Swoole\Database\MysqliPool->__construct(MysqliConfig $config, int $size = 64)

Create a RedisPool with the configration.

Swoole\Database\MysqliConfig

MysqliPool connection pool configuration

Swoole\Database\MysqliConfig->withHost(string $host)
Swoole\Database\MysqliConfig->withUnixSocket(string $unixSocket)
Swoole\Database\MysqliConfig->withPort(int $port)
Swoole\Database\MysqliConfig->withDbname(string $dbname)
Swoole\Database\MysqliConfig->withCharset(string $charset)
Swoole\Database\MysqliConfig->withUsername(string $username)
Swoole\Database\MysqliConfig->withPassword(string $withPassword)
Swoole\Database\MysqliConfig->withOptions(array $options)

Swoole\Coroutine\Server

Coroutine TCP Server can be created dynamically and used in Coroutine context. The methods and features are the same as Swoole\Server.

Example:

<?php
declare(strict_types=1);

use Swoole\Coroutine\Server;
use Swoole\Coroutine\Server\Connection;
go(function () {
    $server = new Server('0.0.0.0', 9601, false);
    $server->handle(function (Connection $conn) use ($server) {
        while('' !== $data = $conn->recv()) {
            $json = json_decode($data, true);
            if(is_array($json) && 'hello' === $json['data']) {
                $conn->send("world\n");
            }
        }
        echo 'disconnected', PHP_EOL;
    });
    $server->start();
});