Skip to content

Commit 3dcc900

Browse files
committed
Merge branch 'egodsk-master'
2 parents b81cdda + b465170 commit 3dcc900

File tree

1 file changed

+104
-162
lines changed

1 file changed

+104
-162
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 104 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -211,112 +211,83 @@ private function wrapHandler(callable $handler): callable
211211
$this->lastRequest['request'] = $request;
212212

213213
// Send the request using the wrapped handler.
214-
$response = Core::proxy(
215-
$handler($request),
216-
function ($response) use ($connection, $transport, $request, $options) {
217-
218-
$this->lastRequest['response'] = $response;
219-
220-
if (isset($response['error']) === true) {
221-
if ($response['error'] instanceof ConnectException || $response['error'] instanceof RingException) {
222-
$this->log->warning("Curl exception encountered.");
223-
224-
$exception = $this->getCurlRetryException($request, $response);
225-
226-
$this->logRequestFail(
227-
$request['http_method'],
228-
$response['effective_url'],
229-
$request['body'],
230-
$request['headers'],
231-
(int) $response['status'],
232-
$response['body'],
233-
(float) $response['transfer_stats']['total_time'],
234-
$exception
235-
);
236-
237-
$node = $connection->getHost();
238-
$this->log->warning("Marking node $node dead.");
239-
$connection->markDead();
240-
241-
// If the transport has not been set, we are inside a Ping or Sniff,
242-
// so we don't want to retrigger retries anyway.
243-
//
244-
// TODO this could be handled better, but we are limited because connectionpools do not
245-
// have access to Transport. Architecturally, all of this needs to be refactored
246-
if (isset($transport) === true) {
247-
$transport->connectionPool->scheduleCheck();
248-
249-
$neverRetry = isset($request['client']['never_retry']) ? $request['client']['never_retry'] : false;
250-
$shouldRetry = $transport->shouldRetry($request);
251-
$shouldRetryText = ($shouldRetry) ? 'true' : 'false';
252-
253-
$this->log->warning("Retries left? $shouldRetryText");
254-
if ($shouldRetry && !$neverRetry) {
255-
return $transport->performRequest(
256-
$request['http_method'],
257-
$request['uri'],
258-
[],
259-
$request['body'],
260-
$options
261-
);
262-
}
214+
$response = Core::proxy($handler($request), function ($response) use ($connection, $transport, $request, $options) {
215+
216+
$this->lastRequest['response'] = $response;
217+
218+
if (isset($response['error']) === true) {
219+
if ($response['error'] instanceof ConnectException || $response['error'] instanceof RingException) {
220+
$this->log->warning("Curl exception encountered.");
221+
222+
$exception = $this->getCurlRetryException($request, $response);
223+
224+
$this->logRequestFail($request, $response, $exception);
225+
226+
$node = $connection->getHost();
227+
$this->log->warning("Marking node $node dead.");
228+
$connection->markDead();
229+
230+
// If the transport has not been set, we are inside a Ping or Sniff,
231+
// so we don't want to retrigger retries anyway.
232+
//
233+
// TODO this could be handled better, but we are limited because connectionpools do not
234+
// have access to Transport. Architecturally, all of this needs to be refactored
235+
if (isset($transport) === true) {
236+
$transport->connectionPool->scheduleCheck();
237+
238+
$neverRetry = isset($request['client']['never_retry']) ? $request['client']['never_retry'] : false;
239+
$shouldRetry = $transport->shouldRetry($request);
240+
$shouldRetryText = ($shouldRetry) ? 'true' : 'false';
241+
242+
$this->log->warning("Retries left? $shouldRetryText");
243+
if ($shouldRetry && !$neverRetry) {
244+
return $transport->performRequest(
245+
$request['http_method'],
246+
$request['uri'],
247+
[],
248+
$request['body'],
249+
$options
250+
);
263251
}
264-
265-
$this->log->warning("Out of retries, throwing exception from $node");
266-
// Only throw if we run out of retries
267-
throw $exception;
268-
} else {
269-
// Something went seriously wrong, bail
270-
$exception = new TransportException($response['error']->getMessage());
271-
$this->logRequestFail(
272-
$request['http_method'],
273-
$response['effective_url'],
274-
$request['body'],
275-
$request['headers'],
276-
(int) $response['status'],
277-
$response['body'],
278-
(float) $response['transfer_stats']['total_time'],
279-
$exception
280-
);
281-
throw $exception;
282252
}
253+
254+
$this->log->warning("Out of retries, throwing exception from $node");
255+
// Only throw if we run out of retries
256+
throw $exception;
283257
} else {
284-
$connection->markAlive();
258+
// Something went seriously wrong, bail
259+
$exception = new TransportException($response['error']->getMessage());
260+
$this->logRequestFail($request, $response, $exception);
261+
throw $exception;
262+
}
263+
} else {
264+
$connection->markAlive();
285265

286-
if (isset($response['body']) === true) {
287-
$response['body'] = stream_get_contents($response['body']);
288-
$this->lastRequest['response']['body'] = $response['body'];
289-
}
266+
if (isset($response['body']) === true) {
267+
$response['body'] = stream_get_contents($response['body']);
268+
$this->lastRequest['response']['body'] = $response['body'];
269+
}
290270

291-
if ($response['status'] >= 400 && $response['status'] < 500) {
292-
$ignore = $request['client']['ignore'] ?? [];
293-
// Skip 404 if succeeded true in the body (e.g. clear_scroll)
294-
$body = $response['body'] ?? '';
295-
if (strpos($body, '"succeeded":true') !== false) {
296-
$ignore[] = 404;
297-
}
298-
$this->process4xxError($request, $response, $ignore);
299-
} elseif ($response['status'] >= 500) {
300-
$ignore = $request['client']['ignore'] ?? [];
301-
$this->process5xxError($request, $response, $ignore);
271+
if ($response['status'] >= 400 && $response['status'] < 500) {
272+
$ignore = $request['client']['ignore'] ?? [];
273+
// Skip 404 if succeeded true in the body (e.g. clear_scroll)
274+
$body = $response['body'] ?? '';
275+
if (strpos($body, '"succeeded":true') !== false) {
276+
$ignore[] = 404;
302277
}
303-
304-
// No error, deserialize
305-
$response['body'] = $this->serializer->deserialize($response['body'], $response['transfer_stats']);
278+
$this->process4xxError($request, $response, $ignore);
279+
} elseif ($response['status'] >= 500) {
280+
$ignore = $request['client']['ignore'] ?? [];
281+
$this->process5xxError($request, $response, $ignore);
306282
}
307-
$this->logRequestSuccess(
308-
$request['http_method'],
309-
$response['effective_url'],
310-
$request['body'],
311-
$request['headers'],
312-
(int) $response['status'],
313-
(array) $response['body'],
314-
(float) $response['transfer_stats']['total_time']
315-
);
316-
317-
return isset($request['client']['verbose']) && $request['client']['verbose'] === true ? $response : $response['body'];
283+
284+
// No error, deserialize
285+
$response['body'] = $this->serializer->deserialize($response['body'], $response['transfer_stats']);
318286
}
319-
);
287+
$this->logRequestSuccess($request, $response);
288+
289+
return isset($request['client']['verbose']) && $request['client']['verbose'] === true ? $response : $response['body'];
290+
});
320291

321292
return $response;
322293
};
@@ -349,87 +320,76 @@ function (&$value, &$key) {
349320
/**
350321
* Log a successful request
351322
*
352-
* @param string $method
353-
* @param string $fullURI
354-
* @param string $body
355-
* @param array $headers
356-
* @param int $statusCode
357-
* @param array $response
358-
* @param float $duration
359-
*
323+
* @param array $request
324+
* @param array $response
360325
* @return void
361326
*/
362-
public function logRequestSuccess(string $method, string $fullURI, ?string $body, array $headers, ?int $statusCode, array $response, float $duration): void
327+
public function logRequestSuccess(array $request, array $response): void
363328
{
364-
$this->log->debug('Request Body', array($body));
329+
$this->log->debug('Request Body', array($request['body']));
365330
$this->log->info(
366331
'Request Success:',
367332
array(
368-
'method' => $method,
369-
'uri' => $fullURI,
370-
'headers' => $headers,
371-
'HTTP code' => $statusCode,
372-
'duration' => $duration,
333+
'method' => $request['http_method'],
334+
'uri' => $response['effective_url'],
335+
'headers' => $request['headers'],
336+
'HTTP code' => $response['status'],
337+
'duration' => $response['transfer_stats']['total_time'],
373338
)
374339
);
375-
$this->log->debug('Response', array($response));
340+
$this->log->debug('Response', array($response['body']));
376341

377342
// Build the curl command for Trace.
378-
$curlCommand = $this->buildCurlCommand($method, $fullURI, $body);
343+
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
379344
$this->trace->info($curlCommand);
380345
$this->trace->debug(
381346
'Response:',
382347
array(
383-
'response' => $response,
384-
'method' => $method,
385-
'uri' => $fullURI,
386-
'HTTP code' => $statusCode,
387-
'duration' => $duration,
348+
'response' => $response['body'],
349+
'method' => $request['http_method'],
350+
'uri' => $response['effective_url'],
351+
'HTTP code' => $response['status'],
352+
'duration' => $response['transfer_stats']['total_time'],
388353
)
389354
);
390355
}
391356

392357
/**
393-
* Log a a failed request
358+
* Log a failed request
394359
*
395-
* @param string $method
396-
* @param string $fullURI
397-
* @param string $body
398-
* @param array $headers
399-
* @param null|int $statusCode
400-
* @param null|string $response
401-
* @param float $duration
402-
* @param \Exception $exception
360+
* @param array $request
361+
* @param array $response
362+
* @param \Exception $exception
403363
*
404364
* @return void
405365
*/
406-
public function logRequestFail(string $method, string $fullURI, ?string $body, array $headers, ?int $statusCode, ?string $response, float $duration, \Exception $exception): void
366+
public function logRequestFail(array $request, array $response, \Exception $exception): void
407367
{
408-
$this->log->debug('Request Body', array($body));
368+
$this->log->debug('Request Body', array($request['body']));
409369
$this->log->warning(
410370
'Request Failure:',
411371
array(
412-
'method' => $method,
413-
'uri' => $fullURI,
414-
'headers' => $headers,
415-
'HTTP code' => $statusCode,
416-
'duration' => $duration,
372+
'method' => $request['http_method'],
373+
'uri' => $response['effective_url'],
374+
'headers' => $request['headers'],
375+
'HTTP code' => $response['status'],
376+
'duration' => $response['transfer_stats']['total_time'],
417377
'error' => $exception->getMessage(),
418378
)
419379
);
420-
$this->log->warning('Response', array($response));
380+
$this->log->warning('Response', array($response['body']));
421381

422382
// Build the curl command for Trace.
423-
$curlCommand = $this->buildCurlCommand($method, $fullURI, $body);
383+
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
424384
$this->trace->info($curlCommand);
425385
$this->trace->debug(
426386
'Response:',
427387
array(
428388
'response' => $response,
429-
'method' => $method,
430-
'uri' => $fullURI,
431-
'HTTP code' => $statusCode,
432-
'duration' => $duration,
389+
'method' => $request['http_method'],
390+
'uri' => $response['effective_url'],
391+
'HTTP code' => $response['status'],
392+
'duration' => $response['transfer_stats']['total_time'],
433393
)
434394
);
435395
}
@@ -595,16 +555,7 @@ private function process4xxError(array $request, array $response, array $ignore)
595555
$exception = new BadRequest400Exception($responseBody, $statusCode);
596556
}
597557

598-
$this->logRequestFail(
599-
$request['http_method'],
600-
$response['effective_url'],
601-
$request['body'],
602-
$request['headers'],
603-
(int) $response['status'],
604-
$response['body'],
605-
(float) $response['transfer_stats']['total_time'],
606-
$exception
607-
);
558+
$this->logRequestFail($request, $response, $exception);
608559

609560
throw $exception;
610561
}
@@ -637,16 +588,7 @@ private function process5xxError(array $request, array $response, array $ignore)
637588
$exception = new ServerErrorResponseException($responseBody, $statusCode);
638589
}
639590

640-
$this->logRequestFail(
641-
$request['http_method'],
642-
$response['effective_url'],
643-
$request['body'],
644-
$request['headers'],
645-
(int) $response['status'],
646-
$response['body'],
647-
(float) $response['transfer_stats']['total_time'],
648-
$exception
649-
);
591+
$this->logRequestFail($request, $response, $exception);
650592

651593
throw $exception;
652594
}

0 commit comments

Comments
 (0)