@@ -54,7 +54,7 @@ public function __destruct()
54
54
}
55
55
56
56
/**
57
- * Add promise core
57
+ * Add promise to runner
58
58
*
59
59
* @param PromiseCore $core
60
60
*/
@@ -74,6 +74,22 @@ public function add(PromiseCore $core)
74
74
curl_multi_add_handle ($ this ->multiHandle , $ core ->getHandle ());
75
75
}
76
76
77
+ /**
78
+ * Remove promise from runner
79
+ *
80
+ * @param PromiseCore $core
81
+ */
82
+ public function remove (PromiseCore $ core )
83
+ {
84
+ foreach ($ this ->cores as $ index => $ existed ) {
85
+ if ($ existed === $ core ) {
86
+ curl_multi_remove_handle ($ this ->multiHandle , $ core ->getHandle ());
87
+ unset($ this ->cores [$ index ]);
88
+ return ;
89
+ }
90
+ }
91
+ }
92
+
77
93
/**
78
94
* Wait for request(s) to be completed.
79
95
*
@@ -86,18 +102,32 @@ public function wait(PromiseCore $targetCore = null)
86
102
$ info = curl_multi_info_read ($ this ->multiHandle );
87
103
if (false !== $ info ) {
88
104
$ core = $ this ->findCoreByHandle ($ info ['handle ' ]);
105
+
106
+ if (null === $ core ) {
107
+ // We have no promise for this handle. Drop it.
108
+ curl_multi_remove_handle ($ this ->multiHandle , $ info ['handle ' ]);
109
+ continue ;
110
+ }
111
+
89
112
if (CURLE_OK === $ info ['result ' ]) {
90
- $ response = $ this ->responseParser ->parse (
91
- curl_multi_getcontent ($ info ['handle ' ]),
92
- curl_getinfo ($ info ['handle ' ])
93
- );
94
- $ core ->fulfill ($ response );
113
+ try {
114
+ $ response = $ this ->responseParser ->parse (
115
+ curl_multi_getcontent ($ core ->getHandle ()),
116
+ curl_getinfo ($ core ->getHandle ())
117
+ );
118
+ $ core ->fulfill ($ response );
119
+ } catch (\Exception $ e ) {
120
+ $ core ->reject (
121
+ new RequestException ($ e ->getMessage (), $ core ->getRequest (), $ e )
122
+ );
123
+ }
95
124
} else {
96
- $ error = curl_error ($ info ['handle ' ]);
97
- $ exception = new RequestException ($ error , $ core ->getRequest ());
98
- $ core ->reject ($ exception );
125
+ $ error = curl_error ($ core ->getHandle ());
126
+ $ core ->reject (new RequestException ($ error , $ core ->getRequest ()));
99
127
}
128
+ $ this ->remove ($ core );
100
129
130
+ // This is a promise we are waited for. So exiting wait().
101
131
if ($ core === $ targetCore ) {
102
132
return ;
103
133
}
0 commit comments