@@ -70,8 +70,8 @@ class Connection implements ConnectionInterface
70
70
protected $ path ;
71
71
72
72
/**
73
- * @var int
74
- */
73
+ * @var int
74
+ */
75
75
protected $ port ;
76
76
77
77
/**
@@ -143,9 +143,10 @@ public function __construct(
143
143
144
144
// Only Set the Basic if API Key is not set and setBasicAuthentication was not called prior
145
145
if (isset ($ connectionParams ['client ' ]['headers ' ]['Authorization ' ]) === false
146
- && isset ($ connectionParams ['client ' ]['curl ' ][CURLOPT_HTTPAUTH ]) === false
147
- && isset ($ hostDetails ['user ' ])
148
- && isset ($ hostDetails ['pass ' ])) {
146
+ && isset ($ connectionParams ['client ' ]['curl ' ][CURLOPT_HTTPAUTH ]) === false
147
+ && isset ($ hostDetails ['user ' ])
148
+ && isset ($ hostDetails ['pass ' ])
149
+ ) {
149
150
$ connectionParams ['client ' ]['curl ' ][CURLOPT_HTTPAUTH ] = CURLAUTH_BASIC ;
150
151
$ connectionParams ['client ' ]['curl ' ][CURLOPT_USERPWD ] = $ hostDetails ['user ' ].': ' .$ hostDetails ['pass ' ];
151
152
}
@@ -252,86 +253,89 @@ private function wrapHandler(callable $handler): callable
252
253
$ this ->lastRequest ['request ' ] = $ request ;
253
254
254
255
// Send the request using the wrapped handler.
255
- $ response = Core::proxy ($ handler ($ request ), function ($ response ) use ($ connection , $ transport , $ request , $ options ) {
256
-
257
- $ this ->lastRequest ['response ' ] = $ response ;
258
-
259
- if (isset ($ response ['error ' ]) === true ) {
260
- if ($ response ['error ' ] instanceof ConnectException || $ response ['error ' ] instanceof RingException) {
261
- $ this ->log ->warning ("Curl exception encountered. " );
262
-
263
- $ exception = $ this ->getCurlRetryException ($ request , $ response );
264
-
265
- $ this ->logRequestFail ($ request , $ response , $ exception );
266
-
267
- $ node = $ connection ->getHost ();
268
- $ this ->log ->warning ("Marking node $ node dead. " );
269
- $ connection ->markDead ();
270
-
271
- // If the transport has not been set, we are inside a Ping or Sniff,
272
- // so we don't want to retrigger retries anyway.
273
- //
274
- // TODO this could be handled better, but we are limited because connectionpools do not
275
- // have access to Transport. Architecturally, all of this needs to be refactored
276
- if (isset ($ transport ) === true ) {
277
- $ transport ->connectionPool ->scheduleCheck ();
278
-
279
- $ neverRetry = isset ($ request ['client ' ]['never_retry ' ]) ? $ request ['client ' ]['never_retry ' ] : false ;
280
- $ shouldRetry = $ transport ->shouldRetry ($ request );
281
- $ shouldRetryText = ($ shouldRetry ) ? 'true ' : 'false ' ;
282
-
283
- $ this ->log ->warning ("Retries left? $ shouldRetryText " );
284
- if ($ shouldRetry && !$ neverRetry ) {
285
- return $ transport ->performRequest (
286
- $ request ['http_method ' ],
287
- $ request ['uri ' ],
288
- [],
289
- $ request ['body ' ],
290
- $ options
291
- );
256
+ $ response = Core::proxy (
257
+ $ handler ($ request ),
258
+ function ($ response ) use ($ connection , $ transport , $ request , $ options ) {
259
+
260
+ $ this ->lastRequest ['response ' ] = $ response ;
261
+
262
+ if (isset ($ response ['error ' ]) === true ) {
263
+ if ($ response ['error ' ] instanceof ConnectException || $ response ['error ' ] instanceof RingException) {
264
+ $ this ->log ->warning ("Curl exception encountered. " );
265
+
266
+ $ exception = $ this ->getCurlRetryException ($ request , $ response );
267
+
268
+ $ this ->logRequestFail ($ request , $ response , $ exception );
269
+
270
+ $ node = $ connection ->getHost ();
271
+ $ this ->log ->warning ("Marking node $ node dead. " );
272
+ $ connection ->markDead ();
273
+
274
+ // If the transport has not been set, we are inside a Ping or Sniff,
275
+ // so we don't want to retrigger retries anyway.
276
+ //
277
+ // TODO this could be handled better, but we are limited because connectionpools do not
278
+ // have access to Transport. Architecturally, all of this needs to be refactored
279
+ if (isset ($ transport ) === true ) {
280
+ $ transport ->connectionPool ->scheduleCheck ();
281
+
282
+ $ neverRetry = isset ($ request ['client ' ]['never_retry ' ]) ? $ request ['client ' ]['never_retry ' ] : false ;
283
+ $ shouldRetry = $ transport ->shouldRetry ($ request );
284
+ $ shouldRetryText = ($ shouldRetry ) ? 'true ' : 'false ' ;
285
+
286
+ $ this ->log ->warning ("Retries left? $ shouldRetryText " );
287
+ if ($ shouldRetry && !$ neverRetry ) {
288
+ return $ transport ->performRequest (
289
+ $ request ['http_method ' ],
290
+ $ request ['uri ' ],
291
+ [],
292
+ $ request ['body ' ],
293
+ $ options
294
+ );
295
+ }
292
296
}
293
- }
294
297
295
- $ this ->log ->warning ("Out of retries, throwing exception from $ node " );
296
- // Only throw if we run out of retries
297
- throw $ exception ;
298
+ $ this ->log ->warning ("Out of retries, throwing exception from $ node " );
299
+ // Only throw if we run out of retries
300
+ throw $ exception ;
301
+ } else {
302
+ // Something went seriously wrong, bail
303
+ $ exception = new TransportException ($ response ['error ' ]->getMessage ());
304
+ $ this ->logRequestFail ($ request , $ response , $ exception );
305
+ throw $ exception ;
306
+ }
298
307
} else {
299
- // Something went seriously wrong, bail
300
- $ exception = new TransportException ($ response ['error ' ]->getMessage ());
301
- $ this ->logRequestFail ($ request , $ response , $ exception );
302
- throw $ exception ;
303
- }
304
- } else {
305
- $ connection ->markAlive ();
308
+ $ connection ->markAlive ();
306
309
307
- if (isset ($ response ['headers ' ]['Warning ' ])) {
308
- $ this ->logWarning ($ request , $ response );
309
- }
310
- if (isset ($ response ['body ' ]) === true ) {
311
- $ response ['body ' ] = stream_get_contents ($ response ['body ' ]);
312
- $ this ->lastRequest ['response ' ]['body ' ] = $ response ['body ' ];
313
- }
310
+ if (isset ($ response ['headers ' ]['Warning ' ])) {
311
+ $ this ->logWarning ($ request , $ response );
312
+ }
313
+ if (isset ($ response ['body ' ]) === true ) {
314
+ $ response ['body ' ] = stream_get_contents ($ response ['body ' ]);
315
+ $ this ->lastRequest ['response ' ]['body ' ] = $ response ['body ' ];
316
+ }
314
317
315
- if ($ response ['status ' ] >= 400 && $ response ['status ' ] < 500 ) {
316
- $ ignore = $ request ['client ' ]['ignore ' ] ?? [];
317
- // Skip 404 if succeeded true in the body (e.g. clear_scroll)
318
- $ body = $ response ['body ' ] ?? '' ;
319
- if (strpos ($ body , '"succeeded":true ' ) !== false ) {
320
- $ ignore [] = 404 ;
318
+ if ($ response ['status ' ] >= 400 && $ response ['status ' ] < 500 ) {
319
+ $ ignore = $ request ['client ' ]['ignore ' ] ?? [];
320
+ // Skip 404 if succeeded true in the body (e.g. clear_scroll)
321
+ $ body = $ response ['body ' ] ?? '' ;
322
+ if (strpos ($ body , '"succeeded":true ' ) !== false ) {
323
+ $ ignore [] = 404 ;
324
+ }
325
+ $ this ->process4xxError ($ request , $ response , $ ignore );
326
+ } elseif ($ response ['status ' ] >= 500 ) {
327
+ $ ignore = $ request ['client ' ]['ignore ' ] ?? [];
328
+ $ this ->process5xxError ($ request , $ response , $ ignore );
321
329
}
322
- $ this ->process4xxError ($ request , $ response , $ ignore );
323
- } elseif ($ response ['status ' ] >= 500 ) {
324
- $ ignore = $ request ['client ' ]['ignore ' ] ?? [];
325
- $ this ->process5xxError ($ request , $ response , $ ignore );
330
+
331
+ // No error, deserialize
332
+ $ response ['body ' ] = $ this ->serializer ->deserialize ($ response ['body ' ], $ response ['transfer_stats ' ]);
326
333
}
334
+ $ this ->logRequestSuccess ($ request , $ response );
327
335
328
- // No error, deserialize
329
- $ response ['body ' ] = $ this ->serializer ->deserialize ($ response ['body ' ], $ response ['transfer_stats ' ]);
336
+ return isset ($ request ['client ' ]['verbose ' ]) && $ request ['client ' ]['verbose ' ] === true ? $ response : $ response ['body ' ];
330
337
}
331
- $ this ->logRequestSuccess ($ request , $ response );
332
-
333
- return isset ($ request ['client ' ]['verbose ' ]) && $ request ['client ' ]['verbose ' ] === true ? $ response : $ response ['body ' ];
334
- });
338
+ );
335
339
336
340
return $ response ;
337
341
};
@@ -374,8 +378,8 @@ public function logWarning(array $request, array $response): void
374
378
/**
375
379
* Log a successful request
376
380
*
377
- * @param array $request
378
- * @param array $response
381
+ * @param array $request
382
+ * @param array $response
379
383
* @return void
380
384
*/
381
385
public function logRequestSuccess (array $ request , array $ response ): void
@@ -412,8 +416,8 @@ public function logRequestSuccess(array $request, array $response): void
412
416
/**
413
417
* Log a failed request
414
418
*
415
- * @param array $request
416
- * @param array $response
419
+ * @param array $request
420
+ * @param array $response
417
421
* @param \Exception $exception
418
422
*
419
423
* @return void
@@ -571,7 +575,7 @@ protected function getCurlRetryException(array $request, array $response): Elast
571
575
* Get the OS version using php_uname if available
572
576
* otherwise it returns an empty string
573
577
*
574
- * @see https://github.com/elastic/elasticsearch-php/issues/922
578
+ * @see https://github.com/elastic/elasticsearch-php/issues/922
575
579
*/
576
580
private function getOSVersion (): string
577
581
{
0 commit comments