@@ -32,6 +32,9 @@ abstract class AbstractPool implements Pool {
3232 /** @var callable */
3333 private $ push ;
3434
35+ /** @var int */
36+ private $ pending = 0 ;
37+
3538 /**
3639 * @return \Amp\Promise<\Amp\Postgres\Connection>
3740 *
@@ -72,10 +75,16 @@ public function getIdleConnectionCount(): int {
7275
7376 /**
7477 * @param \Amp\Postgres\Connection $connection
78+ *
79+ * @throws \Error if the connection is already part of this pool or if the connection is dead.
7580 */
7681 protected function addConnection (Connection $ connection ) {
7782 if (isset ($ this ->connections [$ connection ])) {
78- return ;
83+ throw new \Error ("Connection is already a part of this pool " );
84+ }
85+
86+ if (!$ connection ->isAlive ()) {
87+ throw new \Error ("The connection is dead " );
7988 }
8089
8190 $ this ->connections ->attach ($ connection );
@@ -92,17 +101,13 @@ protected function addConnection(Connection $connection) {
92101 * @resolve \Amp\Postgres\Connection
93102 */
94103 private function pop (): \Generator {
95- while ($ this ->promise !== null ) {
96- try {
97- yield $ this ->promise ; // Prevent simultaneous connection creation.
98- } catch (\Throwable $ exception ) {
99- // Ignore failure or cancellation of other operations.
100- }
104+ while ($ this ->promise !== null && $ this ->connections ->count () + $ this ->pending >= $ this ->getMaxConnections ()) {
105+ yield $ this ->promise ; // Prevent simultaneous connection creation when connection count is at maximum - 1.
101106 }
102107
103108 while ($ this ->idle ->isEmpty ()) { // While loop to ensure an idle connection is available after promises below are resolved.
104109 try {
105- if ($ this ->connections ->count () >= $ this ->getMaxConnections ()) {
110+ if ($ this ->connections ->count () + $ this -> pending >= $ this ->getMaxConnections ()) {
106111 // All possible connections busy, so wait until one becomes available.
107112 $ this ->deferred = new Deferred ;
108113 yield $ this ->promise = $ this ->deferred ->promise (); // May be resolved with defunct connection.
0 commit comments