Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
anokhinAleksey authored and mrVrAlex committed Mar 22, 2022
1 parent b4280c2 commit a5a4e0d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
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)
5 changes: 5 additions & 0 deletions example/Dockerfile
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
44 changes: 44 additions & 0 deletions example/cli.php
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)
});

17 changes: 17 additions & 0 deletions example/docker-compose.yml
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
52 changes: 52 additions & 0 deletions example/server.php
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();
12 changes: 12 additions & 0 deletions example/swoole.conf
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

0 comments on commit a5a4e0d

Please sign in to comment.