Skip to content

Commit c87fc25

Browse files
committed
Fixed amphp#28 (Pool::init() possibly returning null)
1 parent a46fcaa commit c87fc25

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/Connection.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public function __construct($config, $sslOptions = null) {
4646
$this->processor = new Processor($ready, $busy, $restore);
4747
$this->processor->config = $config;
4848
}
49-
50-
5149

5250
public static function parseConnStr($connStr, $sslOptions = null) {
5351
$db = null;

lib/ConnectionPool.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ConnectionPool {
77
private $connectionMap = [];
88
private $ready = [];
99
private $readyMap = [];
10-
private $connectionPromise;
10+
private $connectionDeferred;
1111
private $virtualConnection;
1212
private $config;
1313
private $limit;
@@ -38,7 +38,17 @@ public function __construct($config, $limit) {
3838
}
3939

4040
public function getConnectionPromise() {
41-
return $this->connectionPromise;
41+
if (isset($this->connectionDeferred)) {
42+
return $this->connectionDeferred->promise();
43+
}
44+
if ($this->connections) {
45+
foreach ($this->connections as $conn) {
46+
if ($conn->isReady()) {
47+
return new \Amp\Success;
48+
}
49+
}
50+
}
51+
return ($this->connectionDeferred = new \Amp\Deferred)->promise();
4252
}
4353

4454
public function addConnection() {
@@ -50,16 +60,29 @@ public function addConnection() {
5060
$this->connections[] = $conn = new Connection($this->config);
5161
end($this->connections);
5262
$this->connectionMap[spl_object_hash($conn)] = key($this->connections);
53-
$this->connectionPromise = $conn->connect();
54-
$this->connectionPromise->when(function ($error) use ($conn) {
63+
$conn->connect()->when(function ($error) use ($conn) {
64+
if (!$error && !$conn->isReady()) {
65+
$error = new InitializationException("Connection closed before being established");
66+
}
5567
if ($error) {
5668
$this->unmapConnection(spl_object_hash($conn));
5769
if (empty($this->connections)) {
5870
$this->virtualConnection->fail($error);
71+
$deferred = $this->connectionDeferred;
72+
if ($deferred) {
73+
$this->connectionDeferred = null;
74+
$deferred->fail($error);
75+
}
5976
}
6077
return;
6178
}
6279

80+
if (isset($this->connectionDeferred)) {
81+
$deferred = $this->connectionDeferred;
82+
$this->connectionDeferred = null;
83+
$deferred->succeed();
84+
}
85+
6386
if ($this->config->charset != "utf8mb4" || ($this->config->collate != "" && $this->config->collate != "utf8mb4_general_ci")) {
6487
$conn->setCharset($this->config->charset, $this->config->collate);
6588
}
@@ -86,9 +109,6 @@ public function setCharset($charset, $collate = "") {
86109

87110
public function useExceptions($set) {
88111
$this->config->exceptions = $set;
89-
foreach ($this->connections as $conn) {
90-
$conn->useExceptions($set);
91-
}
92112
}
93113

94114
private function ready($hash) {

0 commit comments

Comments
 (0)