new files

This commit is contained in:
panchengyong
2026-03-07 22:29:07 +08:00
parent cd7e80b502
commit 7acbf45ff7
12516 changed files with 1808447 additions and 194 deletions

View File

@@ -0,0 +1,48 @@
<?php
include '../vendor/autoload.php';
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);
});

View File

@@ -0,0 +1,41 @@
<?php
include '../vendor/autoload.php';
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\CoroutinePostgreSQLConnector;
use Swoole\Coroutine\PostgreSQL;
go(function () {
// All PostgreSQL connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new CoroutinePostgreSQLConnector,
[
'connection_strings' => 'host=127.0.0.1 port=5432 dbname=postgres user=postgres password=xy123456',
]
);
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 PostgreSQL $connection */
$connection = $pool->borrow();
$result = $connection->query("SELECT * FROM pg_stat_database where datname='postgres';");
$stat = $connection->fetchAssoc($result);
echo "Return the connection to pool as soon as possible\n";
$pool->return($connection);
var_dump($stat);
});

View File

@@ -0,0 +1,48 @@
<?php
include '../vendor/autoload.php';
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\CoroutineRedisConnector;
use Swoole\Coroutine\Redis;
go(function () {
// All Redis connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new CoroutineRedisConnector,
[
'host' => '127.0.0.1',
'port' => '6379',
'database' => 0,
'password' => null,
'options' => [
'connect_timeout' => 1,
'timeout' => 5,
],
]
);
echo "Initializing connection pool\n";
$pool->init();
defer(function () use ($pool) {
echo "Close connection pool\n";
$pool->close();
});
echo "Borrowing the connection from pool\n";
/**@var Redis $connection */
$connection = $pool->borrow();
$connection->set('test', uniqid());
$test = $connection->get('test');
echo "Return the connection to pool as soon as possible\n";
$pool->return($connection);
var_dump($test);
});

View File

@@ -0,0 +1,49 @@
<?php
include '../vendor/autoload.php';
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\PDOConnector;
// Enable coroutine for PDO
Swoole\Runtime::enableCoroutine();
go(function () {
// All PDO connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new PDOConnector,
[
'dsn' => 'mysql:host=127.0.0.1;port=3306;dbname=mysql;charset=utf8mb4',
'username' => 'root',
'password' => 'xy123456',
'options' => [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_TIMEOUT => 30,
],
]
);
echo "Initializing connection pool\n";
$pool->init();
defer(function () use ($pool) {
echo "Close connection pool\n";
$pool->close();
});
echo "Borrowing the connection from pool\n";
/**@var \PDO $connection */
$connection = $pool->borrow();
$statement = $connection->query('SHOW STATUS LIKE "Threads_connected"');
echo "Return the connection to pool as soon as possible\n";
$pool->return($connection);
var_dump($statement->fetch());
});

View File

@@ -0,0 +1,47 @@
<?php
include '../vendor/autoload.php';
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\PhpRedisConnector;
// Enable coroutine for PhpRedis
Swoole\Runtime::enableCoroutine();
go(function () {
// All Redis connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new PhpRedisConnector,
[
'host' => '127.0.0.1',
'port' => '6379',
'database' => 0,
'password' => null,
'timeout' => 5,
]
);
echo "Initializing connection pool\n";
$pool->init();
defer(function () use ($pool) {
echo "Close connection pool\n";
$pool->close();
});
echo "Borrowing the connection from pool\n";
/**@var Redis $connection */
$connection = $pool->borrow();
$connection->set('test', uniqid());
$test = $connection->get('test');
echo "Return the connection to pool as soon as possible\n";
$pool->return($connection);
var_dump($test);
});

View File

@@ -0,0 +1,65 @@
<?php
include '../vendor/autoload.php';
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\PDOConnector;
use Swoole\Coroutine;
Swoole\Runtime::enableCoroutine();
go(function () {
// All MySQL connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new PDOConnector,
[
'dsn' => 'mysql:host=127.0.0.1;port=3306;dbname=mysql;charset=utf8mb4',
'username' => 'root',
'password' => 'xy123456',
'options' => [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_TIMEOUT => 30,
],
]
);
$pool->init();
// For debug
$peakCount = 0;
swoole_timer_tick(1000, function () use ($pool, &$peakCount) {
$count = $pool->getConnectionCount();
$idleCount = $pool->getIdleCount();
if ($peakCount < $count) {
$peakCount = $count;
}
echo "Pool connection count: $count, peak count: $peakCount, idle count: $idleCount\n";
});
while (true) {
$count = mt_rand(1, 45);
echo "Query count: $count\n";
for ($i = 0; $i < $count; $i++) {
go(function () use ($pool) {
/**@var \PDO $pdo */
$pdo = $pool->borrow();
defer(function () use ($pool, $pdo) {
$pool->return($pdo);
});
$statement = $pdo->query('show status like \'Threads_connected\'');
$ret = $statement->fetch();
if (!isset($ret['Variable_name'])) {
echo "Invalid query result: \n", print_r($ret, true);
}
echo $ret['Variable_name'] . ': ' . $ret['Value'] . "\n";
});
}
Coroutine::sleep(mt_rand(1, 15));
}
});

View File

@@ -0,0 +1,132 @@
<?php
include '../vendor/autoload.php';
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();