Skip to content

Commit ed52de1

Browse files
authored
Merge pull request #3 from makise-co/feature/initial
Pool configuration options
2 parents aadd85e + 25712a9 commit ed52de1

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml, yaml}]
15+
indent_size = 2

src/Driver/MakisePostgres/PooledMakisePostgresDriver.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use DateTimeImmutable;
1414
use DateTimeInterface;
1515
use DateTimeZone;
16-
use MakiseCo\Database\Driver\MakisePostgres\Bridge\ExceptionMapper;
1716
use MakiseCo\Database\Driver\MakisePostgres\Bridge\OptionsProcessor;
1817
use MakiseCo\Database\Driver\MakisePostgres\Bridge\Statement;
1918
use MakiseCo\Postgres\ConnectionConfig;
@@ -31,6 +30,7 @@
3130
use Spiral\Database\Driver\Postgres\PostgresCompiler;
3231
use Spiral\Database\Driver\ReadonlyHandler;
3332
use Spiral\Database\Exception\DriverException;
33+
use Spiral\Database\Exception\HandlerException;
3434
use Spiral\Database\Exception\StatementException;
3535
use Spiral\Database\Injection\ParameterInterface;
3636
use Spiral\Database\Query\BuilderInterface;
@@ -39,11 +39,16 @@
3939
use Spiral\Database\Query\QueryBuilder;
4040
use Spiral\Database\Query\SelectQuery;
4141
use Spiral\Database\Query\UpdateQuery;
42+
use Spiral\Database\Schema\AbstractTable;
4243
use Spiral\Database\StatementInterface;
4344
use Swoole\Coroutine;
4445
use Throwable;
4546

47+
use function array_key_exists;
4648
use function array_merge;
49+
use function is_string;
50+
use function strpos;
51+
use function substr;
4752

4853
class PooledMakisePostgresDriver implements DriverInterface
4954
{
@@ -95,7 +100,22 @@ class PooledMakisePostgresDriver implements DriverInterface
95100
'queryCache' => true,
96101

97102
// disable schema modifications
98-
'readonlySchema' => false
103+
'readonlySchema' => false,
104+
105+
// default connector is Pq connector
106+
'connector' => \MakiseCo\Postgres\Driver\Pq\PqConnector::class,
107+
108+
// minimal connection count in the pool
109+
'poolMinActive' => 0,
110+
111+
// maximum connection count in the pool
112+
'poolMaxActive' => 2,
113+
114+
// maximum connection idle time (seconds, int)
115+
'poolMaxIdleTime' => 30,
116+
117+
// how often pool will check idle connections
118+
'poolValidationInterval' => 15.0,
99119
];
100120

101121
public function __construct(array $options)
@@ -109,7 +129,7 @@ public function __construct(array $options)
109129
))->withDriver($this);
110130
$this->queryCompiler = new PostgresCompiler('""');
111131

112-
if (\array_key_exists('connection', $options)) {
132+
if (array_key_exists('connection', $options)) {
113133
OptionsProcessor::parseDsn($options['connection'], $options);
114134
}
115135

@@ -128,7 +148,23 @@ public function __construct(array $options)
128148
$this->schemaHandler = new ReadonlyHandler($this->schemaHandler);
129149
}
130150

131-
$this->pool = new Bridge\PostgresPool($this->connectionConfig, new PqConnector());
151+
$this->pool = new Bridge\PostgresPool(
152+
$this->connectionConfig,
153+
new $options['connector']()
154+
);
155+
156+
$this->pool->setMaxActive($this->options['poolMaxActive']);
157+
$this->pool->setMinActive($this->options['poolMinActive']);
158+
$this->pool->setMaxIdleTime($this->options['poolMaxIdleTime']);
159+
$this->pool->setValidationInterval($this->options['poolValidationInterval']);
160+
161+
if (array_key_exists('poolMaxWaitTime', $this->options)) {
162+
$this->pool->setMaxWaitTime($this->options['poolMaxWaitTime']);
163+
}
164+
165+
if (array_key_exists('poolMaxLifeTime', $this->options)) {
166+
$this->pool->setMaxLifeTime($this->options['poolMaxLifeTime']);
167+
}
132168
}
133169

134170
public function __destruct()
@@ -251,7 +287,7 @@ public function beginTransaction(string $isolationLevel = null): bool
251287
try {
252288
$transaction = $this->pool->beginTransaction($isolation);
253289
} catch (Throwable $e) {
254-
$e = ExceptionMapper::map($e, 'BEGIN TRANSACTION');
290+
$e = Bridge\ExceptionMapper::map($e, 'BEGIN TRANSACTION');
255291

256292
if (!$e instanceof StatementException\ConnectionException || !$this->options['reconnect']) {
257293
throw $e;
@@ -260,7 +296,7 @@ public function beginTransaction(string $isolationLevel = null): bool
260296
try {
261297
$transaction = $this->pool->beginTransaction($isolation);
262298
} catch (Throwable $e) {
263-
throw ExceptionMapper::map($e, 'BEGIN TRANSACTION');
299+
throw Bridge\ExceptionMapper::map($e, 'BEGIN TRANSACTION');
264300
}
265301
}
266302

@@ -333,11 +369,11 @@ public function getSource(): string
333369
*
334370
* @param string $table
335371
* @param string|null $prefix
336-
* @return \Spiral\Database\Schema\AbstractTable
372+
* @return AbstractTable
337373
*
338-
* @throws \Spiral\Database\Exception\HandlerException
374+
* @throws HandlerException
339375
*/
340-
public function getSchema(string $table, ?string $prefix = null): \Spiral\Database\Schema\AbstractTable
376+
public function getSchema(string $table, ?string $prefix = null): AbstractTable
341377
{
342378
return $this->getSchemaHandler()->getSchema(
343379
$table,
@@ -461,7 +497,7 @@ protected function statement(
461497

462498
return new Statement($statement, $result);
463499
} catch (Throwable $e) {
464-
$e = ExceptionMapper::map($e, Interpolator::interpolate($query, $parameters));
500+
$e = Bridge\ExceptionMapper::map($e, Interpolator::interpolate($query, $parameters));
465501

466502
if (
467503
$retry
@@ -517,8 +553,8 @@ protected function getParameters(iterable $parameters): array
517553
$normParams = [];
518554

519555
foreach ($parameters as $key => $param) {
520-
if (\is_string($key) && 0 === \strpos($key, ':')) {
521-
$key = \substr($key, 1);
556+
if (is_string($key) && 0 === strpos($key, ':')) {
557+
$key = substr($key, 1);
522558
}
523559

524560
if ($param instanceof ParameterInterface) {
@@ -571,4 +607,4 @@ protected function formatDatetime(DateTimeInterface $value): string
571607

572608
return $datetime->setTimestamp($value->getTimestamp())->format(static::DATETIME);
573609
}
574-
}
610+
}

0 commit comments

Comments
 (0)