open-smf / Connection Pool
Licence: mit
A common connection pool based on Swoole is usually used as a database connection pool.
Stars: ✭ 164
Labels
Projects that are alternatives of or similar to Connection Pool
vertica-swoole-adapter
Provides a DB layer for Swoole-based applications to communicate to HP Vertica databases.
Stars: ✭ 14 (-91.46%)
Mutual labels: swoole, connection-pool
swoole-postgresql-doctrine-driver
🔌 A Doctrine DBAL Driver implementation on top of Swoole Coroutine PostgreSQL client
Stars: ✭ 15 (-90.85%)
Mutual labels: swoole, connection-pool
Smproxy
Swoole MySQL Proxy 一个基于 MySQL 协议,Swoole 开发的MySQL数据库连接池。 A MySQL database connection pool based on MySQL protocol and Swoole.
Stars: ✭ 1,665 (+915.24%)
Mutual labels: swoole, connection-pool
Zhttp
基于swoole的异步轻量级web框架,内部封装协程异步非阻塞全套mysql、redis、mongo、memcached连接池,可以轻松start、reload、stop,加入数据库的查询模块,框架已经封装好近乎同步写法,底层异步调用
Stars: ✭ 131 (-20.12%)
Mutual labels: swoole
Swoole Docker
See: https://github.com/swoole/docker-swoole
Stars: ✭ 132 (-19.51%)
Mutual labels: swoole
Ti Rpc
基于swoole封装的一个简易的JSON协议的RPC框架,思路是借鉴的,代码是自己写的。小修小改的,目前服务于前公司(注意是前公司)生产环境,每日支撑大约8000万次调用。
Stars: ✭ 144 (-12.2%)
Mutual labels: swoole
Ycdatabase
The lightest php database framework written in c language, built in php extension, for mysql
Stars: ✭ 130 (-20.73%)
Mutual labels: connection-pool
Multiprocess
🚀Easy to make the common PHP/Python/js...script change daemon and multi-process execution
Stars: ✭ 151 (-7.93%)
Mutual labels: swoole
Aint Queue
🚀 An async-queue library built on top of swoole, flexable multi-consumer, coroutine supported. 基于 Swoole 的一个异步队列库,可弹性伸缩的工作进程池,工作进程协程支持。
Stars: ✭ 143 (-12.8%)
Mutual labels: swoole
R2dbc Pool
Connection Pooling for Reactive Relational Database Connectivity
Stars: ✭ 141 (-14.02%)
Mutual labels: connection-pool
Archer
基于协程Swoole的Task组件,支持多种模式。轻松实现协程Task的队列、并发、Defer、计时器等 | Swoole coroutine task kit - Swoole Humanization Library
Stars: ✭ 132 (-19.51%)
Mutual labels: swoole
Php Monitor
A free, flexible, powerful tool that helps you monitor PHP Service and profiling PHP code.
Stars: ✭ 153 (-6.71%)
Mutual labels: swoole
Phpkafka
PHP Kafka client is used in PHP-FPM and Swoole. PHP Kafka client supports 50 APIs, which might be one that supports the most message types ever.
Stars: ✭ 149 (-9.15%)
Mutual labels: swoole
Mobc
A generic connection pool for Rust with async/await support
Stars: ✭ 141 (-14.02%)
Mutual labels: connection-pool
Guzzle Swoole
让基于 Guzzle 的项目完美无缝兼容 Swoole 协程,支持:Guzzle、Elasticsearch client——来自宇润 PHP 全家桶
Stars: ✭ 143 (-12.8%)
Mutual labels: swoole
Connection pool
A common connection pool based on Swoole is usually used as the database connection pool.
Requirements
Dependency | Requirement |
---|---|
PHP | >=7.0.0 |
Swoole |
>=4.2.9 Recommend 4.2.13+
|
Install
Install package via Composer.
composer require "open-smf/connection-pool:~1.0"
Usage
See more examples.
- Available connectors
Connector | Connection description |
---|---|
CoroutineMySQLConnector | Instance of Swoole\Coroutine\MySQL
|
CoroutinePostgreSQLConnector | Instance of Swoole\Coroutine\PostgreSQL , require configuring Swoole with --enable-coroutine-postgresql
|
CoroutineRedisConnector | Instance of Swoole\Coroutine\Redis
|
PhpRedisConnector | Instance of Redis , require redis
|
PDOConnector | Instance of PDO , require PDO
|
YourConnector |
YourConnector must implement interface ConnectorInterface , any object can be used as a connection instance |
- Basic usage
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector;
use Swoole\Coroutine\MySQL;
go(function () {
// All MySQL connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new CoroutineMySQLConnector,
[
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'root',
'password' => 'xy123456',
'database' => 'mysql',
'timeout' => 10,
'charset' => 'utf8mb4',
'strict_type' => true,
'fetch_mode' => true,
]
);
echo "Initializing connection pool\n";
$pool->init();
defer(function () use ($pool) {
echo "Closing connection pool\n";
$pool->close();
});
echo "Borrowing the connection from pool\n";
/**@var MySQL $connection */
$connection = $pool->borrow();
$status = $connection->query('SHOW STATUS LIKE "Threads_connected"');
echo "Return the connection to pool as soon as possible\n";
$pool->return($connection);
var_dump($status);
});
- Usage in Swoole Server
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\ConnectionPoolTrait;
use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector;
use Smf\ConnectionPool\Connectors\PhpRedisConnector;
use Swoole\Coroutine\MySQL;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
class HttpServer
{
use ConnectionPoolTrait;
protected $swoole;
public function __construct(string $host, int $port)
{
$this->swoole = new Server($host, $port);
$this->setDefault();
$this->bindWorkerEvents();
$this->bindHttpEvent();
}
protected function setDefault()
{
$this->swoole->set([
'daemonize' => false,
'dispatch_mode' => 1,
'max_request' => 8000,
'open_tcp_nodelay' => true,
'reload_async' => true,
'max_wait_time' => 60,
'enable_reuse_port' => true,
'enable_coroutine' => true,
'http_compression' => false,
'enable_static_handler' => false,
'buffer_output_size' => 4 * 1024 * 1024,
'worker_num' => 4, // Each worker holds a connection pool
]);
}
protected function bindHttpEvent()
{
$this->swoole->on('Request', function (Request $request, Response $response) {
$pool1 = $this->getConnectionPool('mysql');
/**@var MySQL $mysql */
$mysql = $pool1->borrow();
$status = $mysql->query('SHOW STATUS LIKE "Threads_connected"');
// Return the connection to pool as soon as possible
$pool1->return($mysql);
$pool2 = $this->getConnectionPool('redis');
/**@var \Redis $redis */
$redis = $pool2->borrow();
$clients = $redis->info('Clients');
// Return the connection to pool as soon as possible
$pool2->return($redis);
$json = [
'status' => $status,
'clients' => $clients,
];
// Other logic
// ...
$response->header('Content-Type', 'application/json');
$response->end(json_encode($json));
});
}
protected function bindWorkerEvents()
{
$createPools = function () {
// All MySQL connections: [4 workers * 2 = 8, 4 workers * 10 = 40]
$pool1 = new ConnectionPool(
[
'minActive' => 2,
'maxActive' => 10,
],
new CoroutineMySQLConnector,
[
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'root',
'password' => 'xy123456',
'database' => 'mysql',
'timeout' => 10,
'charset' => 'utf8mb4',
'strict_type' => true,
'fetch_mode' => true,
]);
$pool1->init();
$this->addConnectionPool('mysql', $pool1);
// All Redis connections: [4 workers * 5 = 20, 4 workers * 20 = 80]
$pool2 = new ConnectionPool(
[
'minActive' => 5,
'maxActive' => 20,
],
new PhpRedisConnector,
[
'host' => '127.0.0.1',
'port' => '6379',
'database' => 0,
'password' => null,
]);
$pool2->init();
$this->addConnectionPool('redis', $pool2);
};
$closePools = function () {
$this->closeConnectionPools();
};
$this->swoole->on('WorkerStart', $createPools);
$this->swoole->on('WorkerStop', $closePools);
$this->swoole->on('WorkerError', $closePools);
}
public function start()
{
$this->swoole->start();
}
}
// Enable coroutine for PhpRedis
Swoole\Runtime::enableCoroutine();
$server = new HttpServer('0.0.0.0', 5200);
$server->start();
License
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].