Skip to content

Commit

Permalink
Merge pull request #3 from makise-co/feature/pool
Browse files Browse the repository at this point in the history
Connection pooling
codercms authored May 19, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents a458e9b + 9c619c2 commit 7719043
Showing 41 changed files with 2,039 additions and 385 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -28,22 +28,22 @@ More examples can be found in the [`examples`](examples) directory.

declare(strict_types=1);

use MakiseCo\Postgres\ConnectConfigBuilder;
use MakiseCo\Postgres\ConnectionConfigBuilder;
use MakiseCo\Postgres\Connection;
use MakiseCo\Postgres\ResultSet;

use function Swoole\Coroutine\run;

run(static function () {
$config = (new ConnectConfigBuilder())
$config = (new ConnectionConfigBuilder())
->withHost('127.0.0.1')
->withPort(5432)
->withUser('makise')
->withPassword('el-psy-congroo')
->withDatabase('cern')
->build();
// or:
$config = (new ConnectConfigBuilder())
$config = (new ConnectionConfigBuilder())
->fromArray([
'host' => '127.0.0.1',
'port' => 5432,
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@
"require": {
"php": ">=7.4",
"ext-swoole": ">=4.4",
"ext-pq": "*"
"ext-pq": "*",
"open-smf/connection-pool": "^1.0"
},
"require-dev": {
"swoole/ide-helper": "^4.5",
36 changes: 0 additions & 36 deletions examples/basic.php

This file was deleted.

39 changes: 39 additions & 0 deletions examples/connection/basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the Makise-Co Postgres Client
* World line: 0.571024a
*
* (c) Dmitry K. <[email protected]>
*/

declare(strict_types=1);

require dirname(__DIR__) . '/../vendor/autoload.php';

use MakiseCo\Postgres\Connection;
use MakiseCo\Postgres\ConnectionConfigBuilder;
use MakiseCo\Postgres\ResultSet;

use function Swoole\Coroutine\run;

run(
static function () {
$config = (new ConnectionConfigBuilder())
->withHost('127.0.0.1')
->withPort(5432)
->withUser('makise')
->withPassword('el-psy-congroo')
->withDatabase('makise')
->build();

$connection = new Connection($config);
$connection->connect();

/** @var ResultSet $result */
$result = $connection->query('SHOW ALL');

while ($row = $result->fetchAssoc()) {
printf("%-35s = %s (%s)\n", $row['name'], $row['setting'], $row['description']);
}
}
);
74 changes: 74 additions & 0 deletions examples/connection/listen_by_callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* This file is part of the Makise-Co Postgres Client
* World line: 0.571024a
*
* (c) Dmitry K. <[email protected]>
*/

declare(strict_types=1);

require \dirname(__DIR__) . '/../vendor/autoload.php';

use MakiseCo\Postgres\Connection;
use MakiseCo\Postgres\ConnectionConfigBuilder;
use MakiseCo\Postgres\Notification;
use Swoole\Timer;

use function Swoole\Coroutine\run;

run(
static function () {
$config = (new ConnectionConfigBuilder())
->withHost('127.0.0.1')
->withPort(5432)
->withUser('makise')
->withPassword('el-psy-congroo')
->withDatabase('makise')
->build();

$connection = new Connection($config);
$connection->connect();

$channel = "test";

$connection->listen(
$channel,
static function (Notification $notification) {
printf(
"Received notification from PID %d on channel '%s' with payload: %s\n",
$notification->pid,
$notification->channel,
$notification->payload
);
}
);

printf("Listening on channel '%s'\n", $channel);

Timer::after(
3000,
function () use ($connection, $channel) { // Unlisten in 3 seconds.
printf("Unlistening from channel '%s'\n", $channel);
$connection->unlisten($channel);
}
);

Timer::after(
1000,
function () use ($connection, $channel) {
$connection->notify($channel, "Data 1"); // Send first notification.
}
);

Timer::after(
2000,
function () use ($connection, $channel) {
$connection->notify($channel, "Data 2"); // Send second notification.
}
);

print_r("[+] Done\n");
}
);

73 changes: 73 additions & 0 deletions examples/connection/listen_by_listerner_object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* This file is part of the Makise-Co Postgres Client
* World line: 0.571024a
*
* (c) Dmitry K. <[email protected]>
*/

declare(strict_types=1);

require \dirname(__DIR__) . '/../vendor/autoload.php';

use MakiseCo\Postgres\Connection;
use MakiseCo\Postgres\ConnectionConfigBuilder;
use Swoole\Timer;

use function Swoole\Coroutine\run;

run(
static function () {
$config = (new ConnectionConfigBuilder())
->withHost('127.0.0.1')
->withPort(5432)
->withUser('makise')
->withPassword('el-psy-congroo')
->withDatabase('makise')
->build();

$connection = new Connection($config);
$connection->connect();

$channel = "test";

/* @var \MakiseCo\Postgres\Listener $listener */
$listener = $connection->listen($channel, null);

printf("Listening on channel '%s'\n", $channel);

Timer::after(
3000,
function () use ($listener) { // Unlisten in 3 seconds.
printf("Unlistening from channel '%s'\n", $listener->getChannel());
$listener->unlisten();
}
);

Timer::after(
1000,
function () use ($connection, $channel) {
$connection->notify($channel, "Data 1"); // Send first notification.
}
);

Timer::after(
2000,
function () use ($connection, $channel) {
$connection->notify($channel, "Data 2"); // Send second notification.
}
);

while ($notification = $listener->getNotification()) {
printf(
"Received notification from PID %d on channel '%s' with payload: %s\n",
$notification->pid,
$notification->channel,
$notification->payload
);
}

print_r("[+] Done\n");
}
);

54 changes: 54 additions & 0 deletions examples/connection/transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* This file is part of the Makise-Co Postgres Client
* World line: 0.571024a
*
* (c) Dmitry K. <[email protected]>
*/

declare(strict_types=1);

require \dirname(__DIR__) . '/../vendor/autoload.php';

use MakiseCo\Postgres\Connection;
use MakiseCo\Postgres\ConnectionConfigBuilder;

use function Swoole\Coroutine\run;

run(
static function () {
$config = (new ConnectionConfigBuilder())
->withHost('127.0.0.1')
->withPort(5432)
->withUser('makise')
->withPassword('el-psy-congroo')
->withDatabase('makise')
->build();

$connection = new Connection($config);
$connection->connect();

$connection->query('DROP TABLE IF EXISTS test');

$transaction = $connection->beginTransaction();

$transaction->query('CREATE TABLE test (domain VARCHAR(63), tld VARCHAR(63), PRIMARY KEY (domain, tld))');

$statement = $transaction->prepare('INSERT INTO test VALUES (?, ?)');

$statement->execute(['amphp', 'org']);
$statement->execute(['google', 'com']);
$statement->execute(['github', 'com']);

/** @var \MakiseCo\Postgres\ResultSet $result */
$result = $transaction->execute('SELECT * FROM test WHERE tld = :tld', ['tld' => 'com']);

$format = "%-20s | %-10s\n";
printf($format, 'TLD', 'Domain');
while ($row = $result->fetchAssoc()) {
printf($format, $row['domain'], $row['tld']);
}

$transaction->rollback();
}
);
59 changes: 0 additions & 59 deletions examples/listen_by_callback.php

This file was deleted.

Loading

0 comments on commit 7719043

Please sign in to comment.