-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4280c2
commit a5a4e0d
Showing
6 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# doctrine-dbal-swoole-pgsql-driver | ||
Doctrine DBAL Driver for Swoole Postgresql database connections | ||
|
||
## Installation | ||
|
||
The easiest way to install this package is through composer: | ||
|
||
```bash | ||
$ composer require opsway/doctrine-dbal-swoole-pgsql-driver | ||
``` | ||
|
||
## Example | ||
|
||
You can test functionality using supplied docker image, located in [example](example) folder. Cli example can be found | ||
in [example/cli.php](example/cli.php). HTTP server example can be found in [example/server.php](example/server.php) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM openswoole/swoole:4.10-php8.0 | ||
|
||
COPY ./swoole.conf /etc/supervisor/service.d | ||
|
||
WORKDIR /app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
$connectionParams = [ | ||
'dbname' => 'mydb', | ||
'user' => 'user', | ||
'password' => 'secret', | ||
'host' => 'dbhost', | ||
'driverClass' => \OpsWay\Doctrine\DBAL\Swoole\PgSQL\Driver::class, | ||
'poolSize' => 5, // MAX count connections in one pool | ||
'tickFrequency' => 60000, // when need check possibilities downscale (close) opened connection to DB in pools | ||
'connectionTtl' => 60000, // when connection not used this time - it will be close (free) | ||
'usedTimes' => 100, // 1 connection (in pool) will be re-used maximum N queries | ||
'retry' => [ | ||
'max_attempts' => 2, // if connection in pool was timeout (before use) then try re-connect | ||
'delay' => 1, // after this time | ||
] | ||
]; | ||
$pool = (new \OpsWay\Doctrine\DBAL\Swoole\PgSQL\ConnectionPullFactory())($connectionParams); | ||
$configuration = new \Doctrine\DBAL\Configuration(); | ||
$configuration->setMiddlewares( | ||
[new \OpsWay\Doctrine\DBAL\Swoole\PgSQL\DriverMiddleware($pool)] | ||
); | ||
$connFactory = static fn() => \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $configuration); | ||
|
||
Co\run(function () use ($connFactory) { | ||
for ($i = 1; $i <= 5; $i++) { // get 5 connection and make 5 async calls (2 SQL queries in each connection) | ||
go(static function () use ($connFactory, $i) { | ||
$conn = $connFactory(); | ||
$v = $conn->fetchOne('SELECT version()'); | ||
echo $v . PHP_EOL; | ||
$conn->fetchOne('SELECT pg_sleep(1)'); | ||
echo 'routine #: ' . $i . PHP_EOL; | ||
defer(static fn() => $conn->close()); | ||
}); | ||
} | ||
// If repeat this again 50 times then each 5 connection will have same connection to DB by 100 queries count | ||
// 500 SQL for 50sec (instead of 250sec) | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: '3' | ||
|
||
services: | ||
app: | ||
build: . | ||
volumes: | ||
- ./../:/app | ||
ports: | ||
- 9501:9501 | ||
container_name: test_dbal_swoole_pgsql_driver | ||
dbhost: | ||
image: postgres:13 | ||
environment: | ||
POSTGRES_PASSWORD: secret | ||
POSTGRES_USER: user | ||
POSTGRES_DB: mydb | ||
container_name: test_dbal_swoole_pgsql_driver_db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Swoole\Http\Server; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
$connectionParams = [ | ||
'dbname' => 'mydb', | ||
'user' => 'user', | ||
'password' => 'secret', | ||
'host' => 'dbhost', | ||
'driverClass' => \OpsWay\Doctrine\DBAL\Swoole\PgSQL\Driver::class, | ||
'poolSize' => 5, // MAX count connections in one pool | ||
'tickFrequency' => 60000, // when need check possibilities downscale (close) opened connection to DB in pools | ||
'connectionTtl' => 60000, // when connection not used this time - it will be close (free) | ||
'usedTimes' => 100, // 1 connection (in pool) will be re-used maximum N queries | ||
'retry' => [ | ||
'max_attempts' => 2, // if connection in pool was timeout (before use) then try re-connect | ||
'delay' => 1, // after this time | ||
] | ||
]; | ||
$pool = (new \OpsWay\Doctrine\DBAL\Swoole\PgSQL\ConnectionPullFactory())($connectionParams); | ||
$configuration = new \Doctrine\DBAL\Configuration(); | ||
$configuration->setMiddlewares( | ||
[new \OpsWay\Doctrine\DBAL\Swoole\PgSQL\DriverMiddleware($pool)] | ||
); | ||
$connFactory = static fn() => \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $configuration); | ||
|
||
$server = new Swoole\HTTP\Server("0.0.0.0", 9501); | ||
|
||
$server->on("Start", function(Server $server) | ||
{ | ||
echo "Swoole http server is started at http://0.0.0.0:9501\n"; | ||
}); | ||
|
||
$server->on("Request", function(Request $request, Response $response) use ($connFactory) | ||
{ | ||
go(static function () use ($connFactory, $response) { | ||
$conn = $connFactory(); | ||
$conn->fetchOne('SELECT version()'); | ||
$conn->fetchOne('SELECT pg_sleep(2)'); | ||
defer(static fn() => $conn->close()); | ||
$response->header("Content-Type", "text/plain"); | ||
$response->end('End'); | ||
}); | ||
}); | ||
|
||
$server->start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[supervisord] | ||
user = root | ||
|
||
[program:swoole] | ||
command =env HTTP_PORT=9501 php /app/example/server.php | ||
user = root | ||
autostart = true | ||
autorestart = true | ||
stdout_logfile=/proc/self/fd/1 | ||
stdout_logfile_maxbytes=0 | ||
stderr_logfile=/proc/self/fd/1 | ||
stderr_logfile_maxbytes=0 |