@@ -7,7 +7,7 @@ class ConnectionPool {
7
7
private $ connectionMap = [];
8
8
private $ ready = [];
9
9
private $ readyMap = [];
10
- private $ connectionPromise ;
10
+ private $ connectionDeferred ;
11
11
private $ virtualConnection ;
12
12
private $ config ;
13
13
private $ limit ;
@@ -38,7 +38,17 @@ public function __construct($config, $limit) {
38
38
}
39
39
40
40
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 ();
42
52
}
43
53
44
54
public function addConnection () {
@@ -50,16 +60,29 @@ public function addConnection() {
50
60
$ this ->connections [] = $ conn = new Connection ($ this ->config );
51
61
end ($ this ->connections );
52
62
$ 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
+ }
55
67
if ($ error ) {
56
68
$ this ->unmapConnection (spl_object_hash ($ conn ));
57
69
if (empty ($ this ->connections )) {
58
70
$ this ->virtualConnection ->fail ($ error );
71
+ $ deferred = $ this ->connectionDeferred ;
72
+ if ($ deferred ) {
73
+ $ this ->connectionDeferred = null ;
74
+ $ deferred ->fail ($ error );
75
+ }
59
76
}
60
77
return ;
61
78
}
62
79
80
+ if (isset ($ this ->connectionDeferred )) {
81
+ $ deferred = $ this ->connectionDeferred ;
82
+ $ this ->connectionDeferred = null ;
83
+ $ deferred ->succeed ();
84
+ }
85
+
63
86
if ($ this ->config ->charset != "utf8mb4 " || ($ this ->config ->collate != "" && $ this ->config ->collate != "utf8mb4_general_ci " )) {
64
87
$ conn ->setCharset ($ this ->config ->charset , $ this ->config ->collate );
65
88
}
@@ -86,9 +109,6 @@ public function setCharset($charset, $collate = "") {
86
109
87
110
public function useExceptions ($ set ) {
88
111
$ this ->config ->exceptions = $ set ;
89
- foreach ($ this ->connections as $ conn ) {
90
- $ conn ->useExceptions ($ set );
91
- }
92
112
}
93
113
94
114
private function ready ($ hash ) {
0 commit comments