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;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $connectionString;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $maxConnections;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
|
|
|
* @param int $maxConnections
|
2017-06-05 06:42:35 +02:00
|
|
|
*
|
|
|
|
* @throws \Error If $maxConnections is less than 1.
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-06-05 06:42:18 +02:00
|
|
|
public function __construct(string $connectionString, int $maxConnections = self::DEFAULT_MAX_CONNECTIONS) {
|
2016-09-14 16:27:39 +02:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->connectionString = $connectionString;
|
|
|
|
|
|
|
|
$this->maxConnections = $maxConnections;
|
2017-06-05 06:42:35 +02:00
|
|
|
if ($this->maxConnections < 1) {
|
|
|
|
throw new \Error("Pool must contain at least one connection");
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
protected function createConnection(): Promise {
|
2017-06-05 06:42:18 +02:00
|
|
|
return connect($this->connectionString);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getMaxConnections(): int {
|
|
|
|
return $this->maxConnections;
|
|
|
|
}
|
|
|
|
}
|