连接池
优质
小牛编辑
142浏览
2023-12-01
Swoole 在 v4 版本后内置了 Library 模块,使用 PHP 代码编写内核功能,使得底层设施更加稳定可靠,并且提供了内置协程连接池,本章节会说明如何使用对应的连接池。
也可以观看Swoole微课程视频教程学习内置连接池的使用。
ConnectionPool
ConnectionPool,原始连接池,基于Channel自动调度,支持传入任意构造器(callable
),构造器需返回一个连接对象
get
方法获取连接(连接池未满时会创建新的连接)put
方法回收连接fill
方法填充连接池(提前创建连接)close
关闭连接池
Database
各种数据库连接池和对象代理的高级封装,支持自动断线重连。目前包含PDO,Mysqli,Redis三种类型的数据库支持:
PDOConfig
,PDOProxy
,PDOPool
MysqliConfig
,MysqliProxy
,MysqliPool
RedisConfig
,RedisProxy
,RedisPool
!> MySQL断线重连可自动恢复大部分连接上下文(fetch模式,已设置的attribute,已编译的Statement等等),但诸如事务等上下文无法恢复,若处于事务中的连接断开,将会抛出异常,请自行评估重连的可靠性;
将处于事务中的连接归还给连接池是未定义行为,开发者需要自己保证归还的连接是可重用的;
若有连接对象出现异常不可重用,开发者需要调用$pool->put(null);
归还一个空连接以保证连接池的数量平衡。
PDO
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use Swoole\Runtime;
const N = 1024;
Runtime::enableCoroutine();
$s = microtime(true);
Coroutine\run(function () {
$pool = new PDOPool((new PDOConfig)
->withHost('127.0.0.1')
->withPort(3306)
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName('test')
->withCharset('utf8mb4')
->withUsername('root')
->withPassword('root')
);
for ($n = N; $n--;) {
Coroutine::create(function () use ($pool) {
$pdo = $pool->get();
$statement = $pdo->prepare('SELECT ? + ?');
if (!$statement) {
throw new RuntimeException('Prepare failed');
}
$a = mt_rand(1, 100);
$b = mt_rand(1, 100);
$result = $statement->execute([$a, $b]);
if (!$result) {
throw new RuntimeException('Execute failed');
}
$result = $statement->fetchAll();
if ($a + $b !== (int)$result[0][0]) {
throw new RuntimeException('Bad result');
}
$pool->put($pdo);
});
}
});
$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL;
Redis
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;
use Swoole\Runtime;
const N = 1024;
Runtime::enableCoroutine();
$s = microtime(true);
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);
});
}
});
$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . (N * 2) . ' queries' . PHP_EOL;
Mysqli
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Database\MysqliConfig;
use Swoole\Database\MysqliPool;
use Swoole\Runtime;
const N = 1024;
Runtime::enableCoroutine();
$s = microtime(true);
Coroutine\run(function () {
$pool = new MysqliPool((new MysqliConfig)
->withHost('127.0.0.1')
->withPort(3306)
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName('test')
->withCharset('utf8mb4')
->withUsername('root')
->withPassword('root')
);
for ($n = N; $n--;) {
Coroutine::create(function () use ($pool) {
$mysqli = $pool->get();
$statement = $mysqli->prepare('SELECT ? + ?');
if (!$statement) {
throw new RuntimeException('Prepare failed');
}
$a = mt_rand(1, 100);
$b = mt_rand(1, 100);
if (!$statement->bind_param('dd', $a, $b)) {
throw new RuntimeException('Bind param failed');
}
if (!$statement->execute()) {
throw new RuntimeException('Execute failed');
}
if (!$statement->bind_result($result)) {
throw new RuntimeException('Bind result failed');
}
if (!$statement->fetch()) {
throw new RuntimeException('Fetch failed');
}
if ($a + $b !== (int)$result) {
throw new RuntimeException('Bad result');
}
while ($statement->fetch()) {
continue;
}
$pool->put($mysqli);
});
}
});
$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL;