@@ -56,8 +56,8 @@ public function add(
56
56
UriInterface $ uri ,
57
57
array $ curlOptArray ,
58
58
Deferred $ deferred ,
59
- string $ integrity = null ,
60
- Controller $ signal = null ,
59
+ ? string $ integrity = null ,
60
+ ? Controller $ signal = null ,
61
61
):void {
62
62
/** @var CurlInterface $curl */
63
63
$ curl = new $ this ->curlClass ($ uri );
@@ -101,55 +101,71 @@ public function tick():void {
101
101
$ totalActive = 0 ;
102
102
103
103
foreach ($ this ->curlMultiList as $ i => $ curlMulti ) {
104
- $ active = 0 ;
104
+ $ this ->processCurlMulti ($ curlMulti , $ i , $ totalActive );
105
+ }
105
106
106
- // 1) This first do-while loop initiates all underlying curl handles, but on
107
- // slow networks, this might not be enough to download all responses...
108
- do {
109
- $ status = $ curlMulti ->exec ($ active );
110
- }
111
- while ($ status === CURLM_CALL_MULTI_PERFORM );
107
+ if ($ totalActive === 0 ) {
108
+ $ this ->loop ->halt ();
109
+ }
110
+ }
112
111
113
- if ($ status !== CURLM_OK ) {
114
- $ errNo = curl_multi_errno ($ curlMulti ->getHandle ());
115
- $ errString = curl_multi_strerror ($ errNo );
116
- throw new CurlException ($ errString );
117
- }
112
+ private function processCurlMulti (
113
+ CurlMultiInterface $ curlMulti ,
114
+ int $ index ,
115
+ int &$ totalActive ,
116
+ ):void {
117
+ $ active = 0 ;
118
118
119
+ $ this ->executeCurlMultiLoop ($ curlMulti , $ active );
120
+
121
+ if ($ active === 0 ) {
122
+ $ this ->handleCompletedCurlMulti ($ index );
123
+ }
124
+ else {
119
125
$ totalActive += $ active ;
126
+ }
127
+ }
120
128
121
- if ( $ active === 0 ) {
122
- $ response = $ this -> responseList [ $ i ] ?? null ;
123
- $ response -> endDeferredResponse (
124
- $ this -> integrityList [ $ i ]
125
- );
126
- if ( $ this -> deferredList [ $ i ]) {
127
- $ this -> deferredList [ $ i ]-> resolve ( $ response );
128
- }
129
+ private function executeCurlMultiLoop (
130
+ CurlMultiInterface $ curlMulti ,
131
+ int & $ active ,
132
+ ): void {
133
+ do {
134
+ $ status = $ curlMulti -> exec ( $ active );
135
+ }
136
+ while ( $ status === CURLM_CALL_MULTI_PERFORM );
129
137
130
- $ response = null ;
131
- $ this ->deferredList [$ i ] = null ;
132
- }
133
- else {
134
- while ($ active && $ status === CURLM_OK ) {
135
- // 2) We must wait for network activity, because there may be no activity
136
- // between us starting the request and checking the response, especially with
137
- // slow servers.
138
- if ($ curlMulti ->select () !== -1 ) {
139
- do {
140
- $ status = $ curlMulti ->exec ($ active );
141
- }
142
- while ($ status === CURLM_CALL_MULTI_PERFORM );
143
- }
144
- }
145
- }
138
+ if ($ status !== CURLM_OK ) {
139
+ $ this ->handleCurlMultiError ($ curlMulti );
146
140
}
141
+ }
147
142
148
- if ($ totalActive === 0 ) {
149
- $ this ->loop ->halt ();
143
+ private function handleCurlMultiError (CurlMultiInterface $ curlMulti ):void {
144
+ $ errNo = curl_multi_errno ($ curlMulti ->getHandle ());
145
+ $ errString = curl_multi_strerror ($ errNo );
146
+ throw new CurlException ($ errString );
147
+ }
148
+
149
+ private function handleCompletedCurlMulti (int $ index ):void {
150
+ $ response = $ this ->responseList [$ index ] ?? null ;
151
+
152
+ if ($ response ) {
153
+ $ response ->endDeferredResponse ($ this ->integrityList [$ index ]);
154
+
155
+ if ($ this ->deferredList [$ index ]) {
156
+ $ this ->deferredList [$ index ]->resolve ($ response );
157
+ }
158
+
159
+ $ this ->cleanupCompletedRequestData ($ index );
150
160
}
151
161
}
152
162
163
+ private function cleanupCompletedRequestData (int $ index ):void {
164
+ $ this ->responseList [$ index ] = null ;
165
+ $ this ->deferredList [$ index ] = null ;
166
+ }
167
+
168
+
153
169
private function writeHeader (
154
170
CurlHandle |CurlInterface $ ch ,
155
171
string $ rawHeader ,
0 commit comments