Skip to content

Commit 68a7f70

Browse files
committed
Fixed merge conflict
2 parents b81cdda + e71868c commit 68a7f70

File tree

1 file changed

+83
-115
lines changed

1 file changed

+83
-115
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 83 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -211,55 +211,43 @@ 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
}
264252

265253
$this->log->warning("Out of retries, throwing exception from $node");
@@ -281,7 +269,13 @@ function ($response) use ($connection, $transport, $request, $options) {
281269
throw $exception;
282270
}
283271
} else {
284-
$connection->markAlive();
272+
// Something went seriously wrong, bail
273+
$exception = new TransportException($response['error']->getMessage());
274+
$this->logRequestFail($request, $response, $exception);
275+
throw $exception;
276+
}
277+
} else {
278+
$connection->markAlive();
285279

286280
if (isset($response['body']) === true) {
287281
$response['body'] = stream_get_contents($response['body']);
@@ -316,7 +310,10 @@ function ($response) use ($connection, $transport, $request, $options) {
316310

317311
return isset($request['client']['verbose']) && $request['client']['verbose'] === true ? $response : $response['body'];
318312
}
319-
);
313+
$this->logRequestSuccess($request, $response);
314+
315+
return isset($request['client']['verbose']) && $request['client']['verbose'] === true ? $response : $response['body'];
316+
});
320317

321318
return $response;
322319
};
@@ -349,87 +346,76 @@ function (&$value, &$key) {
349346
/**
350347
* Log a successful request
351348
*
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-
*
349+
* @param array $request
350+
* @param array $response
360351
* @return void
361352
*/
362-
public function logRequestSuccess(string $method, string $fullURI, ?string $body, array $headers, ?int $statusCode, array $response, float $duration): void
353+
public function logRequestSuccess($request, $response)
363354
{
364-
$this->log->debug('Request Body', array($body));
355+
$this->log->debug('Request Body', array($request['body']));
365356
$this->log->info(
366357
'Request Success:',
367358
array(
368-
'method' => $method,
369-
'uri' => $fullURI,
370-
'headers' => $headers,
371-
'HTTP code' => $statusCode,
372-
'duration' => $duration,
359+
'method' => $request['http_method'],
360+
'uri' => $response['effective_url'],
361+
'headers' => $request['headers'],
362+
'HTTP code' => $response['status'],
363+
'duration' => $response['transfer_stats']['total_time'],
373364
)
374365
);
375-
$this->log->debug('Response', array($response));
366+
$this->log->debug('Response', array($response['body']));
376367

377368
// Build the curl command for Trace.
378-
$curlCommand = $this->buildCurlCommand($method, $fullURI, $body);
369+
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
379370
$this->trace->info($curlCommand);
380371
$this->trace->debug(
381372
'Response:',
382373
array(
383-
'response' => $response,
384-
'method' => $method,
385-
'uri' => $fullURI,
386-
'HTTP code' => $statusCode,
387-
'duration' => $duration,
374+
'response' => $response['body'],
375+
'method' => $request['http_method'],
376+
'uri' => $response['effective_url'],
377+
'HTTP code' => $response['status'],
378+
'duration' => $response['transfer_stats']['total_time'],
388379
)
389380
);
390381
}
391382

392383
/**
393-
* Log a a failed request
384+
* Log a failed request
394385
*
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
386+
* @param array $request
387+
* @param array $response
388+
* @param \Exception $exception
403389
*
404390
* @return void
405391
*/
406-
public function logRequestFail(string $method, string $fullURI, ?string $body, array $headers, ?int $statusCode, ?string $response, float $duration, \Exception $exception): void
392+
public function logRequestFail($request, $response, \Exception $exception)
407393
{
408-
$this->log->debug('Request Body', array($body));
394+
$this->log->debug('Request Body', array($request['body']));
409395
$this->log->warning(
410396
'Request Failure:',
411397
array(
412-
'method' => $method,
413-
'uri' => $fullURI,
414-
'headers' => $headers,
415-
'HTTP code' => $statusCode,
416-
'duration' => $duration,
398+
'method' => $request['http_method'],
399+
'uri' => $response['effective_url'],
400+
'headers' => $request['headers'],
401+
'HTTP code' => $response['status'],
402+
'duration' => $response['transfer_stats']['total_time'],
417403
'error' => $exception->getMessage(),
418404
)
419405
);
420-
$this->log->warning('Response', array($response));
406+
$this->log->warning('Response', array($response['body']));
421407

422408
// Build the curl command for Trace.
423-
$curlCommand = $this->buildCurlCommand($method, $fullURI, $body);
409+
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
424410
$this->trace->info($curlCommand);
425411
$this->trace->debug(
426412
'Response:',
427413
array(
428414
'response' => $response,
429-
'method' => $method,
430-
'uri' => $fullURI,
431-
'HTTP code' => $statusCode,
432-
'duration' => $duration,
415+
'method' => $request['http_method'],
416+
'uri' => $response['effective_url'],
417+
'HTTP code' => $response['status'],
418+
'duration' => $response['transfer_stats']['total_time'],
433419
)
434420
);
435421
}
@@ -595,16 +581,7 @@ private function process4xxError(array $request, array $response, array $ignore)
595581
$exception = new BadRequest400Exception($responseBody, $statusCode);
596582
}
597583

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-
);
584+
$this->logRequestFail($request, $response, $exception);
608585

609586
throw $exception;
610587
}
@@ -637,16 +614,7 @@ private function process5xxError(array $request, array $response, array $ignore)
637614
$exception = new ServerErrorResponseException($responseBody, $statusCode);
638615
}
639616

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-
);
617+
$this->logRequestFail($request, $response, $exception);
650618

651619
throw $exception;
652620
}

0 commit comments

Comments
 (0)