2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-03-17 16:17:24 +01:00
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
class ConnectionPool extends AbstractPool {
|
|
|
|
const DEFAULT_MAX_CONNECTIONS = 100;
|
|
|
|
const DEFAULT_CONNECT_TIMEOUT = 5000;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $connectionString;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $connectTimeout;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $maxConnections;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
|
|
|
* @param int $maxConnections
|
|
|
|
* @param int $connectTimeout
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
string $connectionString,
|
|
|
|
int $maxConnections = self::DEFAULT_MAX_CONNECTIONS,
|
|
|
|
int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT
|
|
|
|
) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->connectionString = $connectionString;
|
|
|
|
$this->connectTimeout = $connectTimeout;
|
|
|
|
|
|
|
|
$this->maxConnections = $maxConnections;
|
|
|
|
if (1 > $this->maxConnections) {
|
|
|
|
$this->maxConnections = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
protected function createConnection(): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
return connect($this->connectionString, $this->connectTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getMaxConnections(): int {
|
|
|
|
return $this->maxConnections;
|
|
|
|
}
|
|
|
|
}
|