Repository navigation
Expand file tree
/
Copy pathGuzzleHttpClient.php
More file actions
639 lines (573 loc) · 25.1 KB
/
Copy pathGuzzleHttpClient.php
File metadata and controls
639 lines (573 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
declare(strict_types=1);
namespace GetStream\Http;
use GetStream\Exceptions\StreamApiException;
use GetStream\Exceptions\StreamException;
use GetStream\Exceptions\StreamRateLimitException;
use GetStream\Exceptions\StreamTransportException;
use GetStream\StreamResponse;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\HandlerStack;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Guzzle HTTP client implementation.
*/
class GuzzleHttpClient implements HttpClientInterface
{
private GuzzleClient $client;
/** @var PoolConfig Effective pool configuration (kept for diagnostics). */
private PoolConfig $pool;
private LoggerInterface $logger;
/** Whether request/response bodies are included (key-redacted) in log events. */
private bool $logBodies;
private RetryConfig $retry;
/**
* Create a new GuzzleHttpClient.
*
* @param array $config Guzzle client configuration (wins over $pool defaults)
* @param int $maxRetries Deprecated, ignored: retry behavior is now governed
* entirely by $retry. Kept only for positional-call
* compatibility.
* @param PoolConfig|null $pool Connection pool configuration. When null, spec defaults apply.
* @param LoggerInterface|null $logger PSR-3 logger for structured request/response events. Defaults to NullLogger.
* @param bool $logBodies When true, include (key-redacted) request/response bodies in log events.
* @param RetryConfig|null $retry Opt-in auto-retry policy. When null, retries are disabled
* and the client performs exactly one attempt.
*/
// @phpstan-ignore-next-line constructor.unusedParameter (deprecated, kept for positional-call compatibility)
public function __construct(
array $config = [],
int $maxRetries = 3,
?PoolConfig $pool = null,
?LoggerInterface $logger = null,
bool $logBodies = false,
?RetryConfig $retry = null,
) {
$pool = $pool ?? new PoolConfig();
$this->logger = $logger ?? new NullLogger();
$this->logBodies = $logBodies;
$this->retry = $retry ?? new RetryConfig();
// Persistent multi handle: routes every request (sync and async) through one
// libcurl multi handle that holds a connection pool for the lifetime of this
// GuzzleHttpClient instance. CURLMOPT_MAX_HOST_CONNECTIONS is a real per-host
// concurrency cap inside the multi handle; CURLMOPT_MAXCONNECTS sizes the
// multi handle's overall connection cache. Pooling takes effect only when this
// instance is reused across requests (long-running runtimes: Swoole, RoadRunner,
// ReactPHP, CLI daemons). Under PHP-FPM the PHP process exits per request and
// the multi handle dies with it; the per-call request/connect timeouts still
// apply but there is no cross-request connection reuse.
//
// Guzzle 7 accepts CURLMOPT_MAX_HOST_CONNECTIONS in `options`. Guzzle 8 rejects
// it and requires the named max_host_connections / max_total_connections keys.
try {
$multi = new CurlMultiHandler([
'options' => [
CURLMOPT_MAX_HOST_CONNECTIONS => $pool->maxConnsPerHost,
CURLMOPT_MAXCONNECTS => $pool->maxConnsPerHost,
],
]);
} catch (\InvalidArgumentException) {
$multi = new CurlMultiHandler([
'max_host_connections' => $pool->maxConnsPerHost,
'max_total_connections' => $pool->maxConnsPerHost,
'options' => [
CURLMOPT_MAXCONNECTS => $pool->maxConnsPerHost,
],
]);
}
$defaultHandler = HandlerStack::create($multi);
$curlOptions = [
CURLOPT_FORBID_REUSE => 0, // KeepAlive invariant: allow connection reuse.
];
// CURLOPT_MAXLIFETIME_CONN (libcurl >= 7.80.0) caps how long a pooled connection
// can be reused since it was created. We use it to recycle connections ahead of
// the upstream load balancer's idle close window. If the constant is not present
// on this PHP build, pooling still works without active lifetime capping.
if (defined('CURLOPT_MAXLIFETIME_CONN')) {
$curlOptions[\constant('CURLOPT_MAXLIFETIME_CONN')] = $pool->idleTimeout;
}
$defaultConfig = [
'timeout' => $pool->requestTimeout,
'connect_timeout' => $pool->connectTimeout,
'http_errors' => false, // We handle errors ourselves.
'handler' => $defaultHandler,
'curl' => $curlOptions,
];
// User-supplied $config wins. array_replace_recursive lets callers override the
// handler (used by tests with MockHandler) or extend the curl options without
// wiping our defaults.
$merged = array_replace_recursive($defaultConfig, $config);
$this->client = new GuzzleClient($merged);
$this->pool = $pool;
}
/** Return the effective PoolConfig (for diagnostics / logging). */
public function getPoolConfig(): PoolConfig
{
return $this->pool;
}
private static function elapsedMs(int $startNs): int
{
return (int) ((hrtime(true) - $startNs) / 1e6);
}
/** Emit the `http.request.sent` DEBUG event for one HTTP attempt. */
private function emitRequestSent(string $method, string $path, string $query, array $requestOptions): void
{
$context = [
'http.request.method' => $method,
'url.path' => $path,
'url.query' => LogRedaction::redactQuery($query),
];
if ($this->logBodies && isset($requestOptions['json'])) {
$encoded = json_encode($requestOptions['json']);
if ($encoded !== false) {
$context['http.request.body'] = LogRedaction::redactJsonBody($encoded);
}
}
$this->logger->debug('http.request.sent', $context);
}
/**
* Emit the `http.response.received` DEBUG event for one HTTP attempt
* (any status code, including 4xx/5xx: those are data, not a transport
* failure). Reads the response body exactly once and returns it so the
* caller can reuse it instead of consuming the stream a second time.
*/
private function emitResponseReceived(ResponseInterface $response, int $durationMs): string
{
$size = $response->getBody()->getSize();
$rawBody = (string) $response->getBody();
$context = [
'http.response.status_code' => $response->getStatusCode(),
'http.response.body.size' => $size ?? strlen($rawBody),
'duration_ms' => $durationMs,
];
if ($this->logBodies) {
$contentType = $response->getHeaderLine('Content-Type');
$context['http.response.body'] = str_contains($contentType, 'application/json')
? LogRedaction::redactJsonBody($rawBody)
: $rawBody;
}
$this->logger->debug('http.response.received', $context);
return $rawBody;
}
/** Emit the `http.request.failed` ERROR event: transport failure, no HTTP response received. */
private function emitRequestFailed(
string $method,
string $path,
string $query,
StreamTransportException $transportException,
int $durationMs,
): void {
$this->logger->error('http.request.failed', [
'http.request.method' => $method,
'url.path' => $path,
'url.query' => LogRedaction::redactQuery($query),
'error.type' => $transportException->getErrorType(),
'error.message' => LogRedaction::redactMessage($transportException->getMessage()),
'duration_ms' => $durationMs,
]);
}
/**
* Emit the `http.request.failed` DEBUG event when a failed attempt is about
* to be retried (as opposed to the ERROR-level event emitted for a
* transport failure that surfaces to the caller unchanged).
*/
private function emitRetryAttempt(
string $method,
string $path,
string $query,
StreamRateLimitException | StreamTransportException $e,
int $durationMs,
int $retryAttempt,
): void {
$context = [
'http.request.method' => $method,
'url.path' => $path,
'url.query' => LogRedaction::redactQuery($query),
'error.message' => LogRedaction::redactMessage($e->getMessage()),
'duration_ms' => $durationMs,
'retry.attempt' => $retryAttempt,
];
// `error.type` is a closed transport-failure enum (connection_reset |
// timeout | dns_failure | tls_handshake_failed | unknown); a 429 is a
// received response, not a transport failure, so it has no value in
// that enum and the field is omitted rather than invented.
if ($e instanceof StreamTransportException) {
$context['error.type'] = $e->getErrorType();
}
$this->logger->debug('http.request.failed', $context);
}
/**
* Make an HTTP request.
*
* @param string $method HTTP method
* @param string $url Full URL to request
* @param array $headers Request headers
* @param mixed $body Request body
* @param array $options Per-call Guzzle option overrides (e.g. ['timeout' => 2])
*
* @return StreamResponse<mixed>
*
* @throws StreamException
*/
public function request(
string $method,
string $url,
array $headers = [],
mixed $body = null,
array $options = []
): StreamResponse {
$requestOptions = [
'headers' => $headers,
];
// Per-call overrides. 'timeout' is the canonical key; any other valid
// Guzzle key is also forwarded.
foreach ($options as $key => $value) {
$requestOptions[$key] = $value;
}
// Add body if provided
if ($body !== null) {
// Check if this is multipart form data (array of arrays with 'name' and 'contents')
if (is_array($body) && !empty($body) && isset($body[0]) && is_array($body[0]) && isset($body[0]['name'])) {
// This is multipart form data
$requestOptions['multipart'] = $body;
} elseif (is_array($body) || is_object($body)) {
$requestOptions['json'] = $body;
} else {
$requestOptions['body'] = $body;
}
}
$parsedPath = parse_url($url, PHP_URL_PATH);
$path = is_string($parsedPath) ? $parsedPath : $url;
$parsedQuery = parse_url($url, PHP_URL_QUERY);
$query = is_string($parsedQuery) ? $parsedQuery : '';
// Opt-in retry policy: disabled (default) means exactly one attempt.
// When enabled, only GET/HEAD requests failing with 429 (recoverable)
// or a transport error are retried; see shouldRetry()/retryDelay().
for ($attempt = 0;; $attempt++) {
$this->emitRequestSent($method, $path, $query, $requestOptions);
$start = hrtime(true);
try {
try {
$response = $this->client->request($method, $url, $requestOptions);
} catch (ClientException | ServerException $e) {
// Reachable only if a caller flipped `http_errors` back to true.
$durationMs = self::elapsedMs($start);
$rawBody = $this->emitResponseReceived($e->getResponse(), $durationMs);
return $this->createStreamResponse($e->getResponse(), $e, $rawBody);
} catch (NetworkExceptionInterface $e) {
// Guzzle 7: ConnectException. Guzzle 8: NetworkException and subclasses
// (ConnectException, NetworkTimeoutException, …) for no-response failures.
throw new StreamTransportException($e->getMessage(), self::mapNetworkErrorType($e), $e);
} catch (GuzzleException $e) {
// Guzzle 8 reports a timeout after the response headers arrived as
// ResponseTimeoutException, which is not a NetworkExceptionInterface.
// Guzzle 7 reports the same failure as ConnectException (timeout above).
$errorType = class_exists(\GuzzleHttp\Exception\ResponseTimeoutException::class)
&& $e instanceof \GuzzleHttp\Exception\ResponseTimeoutException
? StreamTransportException::ERROR_TYPE_TIMEOUT
: StreamTransportException::ERROR_TYPE_UNKNOWN;
throw new StreamTransportException($e->getMessage(), $errorType, $e);
}
$rawBody = $this->emitResponseReceived($response, self::elapsedMs($start));
return $this->createStreamResponse($response, null, $rawBody);
} catch (StreamRateLimitException | StreamTransportException $e) {
$durationMs = self::elapsedMs($start);
if (!$this->shouldRetry($e, $method, $attempt)) {
if ($e instanceof StreamTransportException) {
$this->emitRequestFailed($method, $path, $query, $e, $durationMs);
}
throw $e;
}
$this->emitRetryAttempt($method, $path, $query, $e, $durationMs, $attempt + 1);
$this->sleepSeconds($this->retryDelay($e, $attempt));
}
}
}
/**
* Decide whether a failed attempt is eligible for retry: retries are
* opt-in (disabled unless a RetryConfig with enabled=true was supplied),
* apply only to GET/HEAD, never to a 429 marked `unrecoverable`, and stop
* once the configured `maxAttempts` is reached.
*/
private function shouldRetry(StreamRateLimitException | StreamTransportException $e, string $method, int $attempt): bool
{
if (!$this->retry->enabled) {
return false;
}
if (!in_array(strtoupper($method), ['GET', 'HEAD'], true)) {
return false;
}
if ($attempt + 1 >= $this->retry->maxAttempts) {
return false;
}
if ($e instanceof StreamRateLimitException) {
return !$e->isUnrecoverable();
}
return true;
}
/**
* Backoff before the next retry: honors a parsed `Retry-After` (no
* jitter) when present and positive, otherwise full jitter over an
* exponential ceiling, both capped at `maxBackoff`.
*/
private function retryDelay(StreamRateLimitException | StreamTransportException $e, int $attempt): float
{
if ($e instanceof StreamRateLimitException) {
$retryAfter = $e->getRetryAfter();
if ($retryAfter !== null && $retryAfter > 0) {
return min((float) $retryAfter, $this->retry->maxBackoff);
}
}
$ceiling = min($this->retry->maxBackoff, 1.0 * (2 ** $attempt));
return $ceiling <= 0 ? 0.0 : mt_rand(0, (int) round($ceiling * 1000)) / 1000.0;
}
/** Overridable so tests can record the delay instead of actually sleeping. */
protected function sleepSeconds(float $seconds): void
{
if ($seconds > 0) {
usleep((int) round($seconds * 1_000_000));
}
}
/**
* Create a StreamResponse from a Guzzle response. Throws a structured
* StreamApiException (or rate-limit subclass) for any 4xx/5xx response.
*
* @param \Throwable|null $previous Cause-chain link when the caller already
* caught a Guzzle exception that exposed
* the response.
* @param string|null $rawBody Body already read by the caller (e.g. for logging).
* The response body stream can only be consumed once;
* pass it through instead of re-reading it here.
*
* @return StreamResponse<mixed>
*/
private function createStreamResponse(ResponseInterface $response, ?\Throwable $previous = null, ?string $rawBody = null): StreamResponse
{
$statusCode = $response->getStatusCode();
$rawBody ??= $response->getBody()->getContents();
// Convert headers to lowercase keys
$headers = [];
foreach ($response->getHeaders() as $name => $values) {
$headers[strtolower($name)] = implode(', ', $values);
}
$data = null;
$jsonParseError = null;
if (!empty($rawBody)) {
$contentType = $headers['content-type'] ?? '';
if (str_contains($contentType, 'application/json')) {
$data = json_decode($rawBody, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$jsonParseError = new \JsonException(json_last_error_msg(), json_last_error());
$data = null;
}
} else {
$data = $rawBody;
}
}
$streamResponse = new StreamResponse($statusCode, $headers, $data, $rawBody);
if ($streamResponse->isSuccessful()) {
// Success path: a JSON-content-type body that failed to parse is a
// bug in either the server or our parser. Surface it.
if ($jsonParseError !== null) {
throw new StreamException(
'Failed to parse JSON response: ' . $jsonParseError->getMessage(),
0,
$jsonParseError,
);
}
return $streamResponse;
}
throw $this->buildApiException($statusCode, $rawBody, $headers, $data, $jsonParseError ?? $previous);
}
/**
* Build a StreamApiException (or StreamRateLimitException for 429) from a
* non-2xx response. Falls back to a sentinel-message StreamApiException
* when the body is not a valid APIError envelope.
*/
private function buildApiException(
int $statusCode,
string $rawBody,
array $headers,
mixed $data,
?\Throwable $previous,
): StreamApiException {
$message = 'API request failed';
$code = 0;
$exceptionFields = [];
$unrecoverable = false;
$moreInfo = null;
$details = null;
$parsedEnvelope = false;
// Best-effort fallback parse when Content-Type wasn't JSON.
if (!is_array($data) && !empty($rawBody)) {
$fallback = json_decode($rawBody, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($fallback)) {
$data = $fallback;
} elseif ($previous === null) {
$previous = new \JsonException(json_last_error_msg(), json_last_error());
}
}
if (is_array($data)) {
$parsedEnvelope = true;
if (isset($data['message']) && is_string($data['message'])) {
$message = $data['message'];
} elseif (isset($data['error']) && is_string($data['error'])) {
$message = $data['error'];
}
if (isset($data['code']) && is_int($data['code'])) {
$code = $data['code'];
}
$exceptionFields = self::normalizeExceptionFields($data['exception_fields'] ?? []);
$unrecoverable = (bool) ($data['unrecoverable'] ?? false);
if (isset($data['more_info']) && is_string($data['more_info'])) {
$moreInfo = $data['more_info'];
}
$details = $data['details'] ?? null;
}
if (!$parsedEnvelope) {
// HTTP layer succeeded, body is unparseable as APIError.
$message = sprintf('failed to parse error response: unexpected server response code %d', $statusCode);
}
if ($statusCode === 429) {
return new StreamRateLimitException(
$message,
$statusCode,
$code,
$exceptionFields,
$unrecoverable,
$rawBody,
$moreInfo,
$details,
self::parseRetryAfter($headers['retry-after'] ?? null),
$previous,
);
}
return new StreamApiException(
$message,
$statusCode,
$code,
$exceptionFields,
$unrecoverable,
$rawBody,
$moreInfo,
$details,
$previous,
);
}
/**
* @return array<string, string>
*/
private static function normalizeExceptionFields(mixed $raw): array
{
if (!is_array($raw)) {
return [];
}
$out = [];
foreach ($raw as $key => $value) {
if (!is_string($key)) {
continue;
}
if (is_string($value)) {
$out[$key] = $value;
} elseif (is_scalar($value)) {
$out[$key] = (string) $value;
}
}
return $out;
}
/**
* Parse `Retry-After` header (RFC 7231 §7.1.3): integer seconds or
* HTTP-date. Returns `null` when header is absent or unparseable.
* HTTP-date deltas are clamped to >= 0.
*/
private static function parseRetryAfter(?string $header): ?int
{
if ($header === null) {
return null;
}
$trimmed = trim($header);
if ($trimmed === '') {
return null;
}
if (ctype_digit($trimmed)) {
return (int) $trimmed;
}
$timestamp = strtotime($trimmed);
if ($timestamp === false) {
return null;
}
$delta = $timestamp - time();
return $delta < 0 ? 0 : $delta;
}
/**
* Map a no-response network failure to a canonical transport-error type.
*
* Guzzle 8 removed ConnectException::getHandlerContext(). Prefer curl errno from
* the handler context when present (Guzzle 7), then from the message ("cURL error N"),
* then Guzzle 8 timeout subclasses, then message inspection.
*/
private static function mapNetworkErrorType(\Throwable $e): string
{
$errno = 0;
if (method_exists($e, 'getHandlerContext')) {
/** @var mixed $ctx */
$ctx = $e->getHandlerContext();
if (is_array($ctx) && isset($ctx['errno'])) {
$errno = (int) $ctx['errno'];
}
}
if ($errno === 0 && preg_match('/curl error (\d+)/i', $e->getMessage(), $matches) === 1) {
$errno = (int) $matches[1];
}
switch ($errno) {
case 6: // CURLE_COULDNT_RESOLVE_HOST
return StreamTransportException::ERROR_TYPE_DNS_FAILURE;
case 7: // CURLE_COULDNT_CONNECT
case 55: // CURLE_SEND_ERROR
case 56: // CURLE_RECV_ERROR
return StreamTransportException::ERROR_TYPE_CONNECTION_RESET;
case 28: // CURLE_OPERATION_TIMEDOUT
return StreamTransportException::ERROR_TYPE_TIMEOUT;
case 35: // CURLE_SSL_CONNECT_ERROR
case 51: // CURLE_PEER_FAILED_VERIFICATION (legacy)
case 60: // CURLE_PEER_FAILED_VERIFICATION
case 77: // CURLE_SSL_CACERT_BADFILE
return StreamTransportException::ERROR_TYPE_TLS_HANDSHAKE_FAILED;
}
// Guzzle 8 timeout subclasses (absent on Guzzle 7).
if (
(class_exists(\GuzzleHttp\Exception\ConnectTimeoutException::class) && $e instanceof \GuzzleHttp\Exception\ConnectTimeoutException)
|| (class_exists(\GuzzleHttp\Exception\NetworkTimeoutException::class) && $e instanceof \GuzzleHttp\Exception\NetworkTimeoutException)
) {
return StreamTransportException::ERROR_TYPE_TIMEOUT;
}
$msg = strtolower($e->getMessage());
if (str_contains($msg, 'could not resolve') || str_contains($msg, 'name or service not known')) {
return StreamTransportException::ERROR_TYPE_DNS_FAILURE;
}
if (str_contains($msg, 'timed out') || str_contains($msg, 'timeout')) {
return StreamTransportException::ERROR_TYPE_TIMEOUT;
}
if (str_contains($msg, 'ssl') || str_contains($msg, 'tls') || str_contains($msg, 'certificate')) {
return StreamTransportException::ERROR_TYPE_TLS_HANDSHAKE_FAILED;
}
if (
str_contains($msg, 'refused')
|| str_contains($msg, 'reset')
|| str_contains($msg, 'closed')
|| str_contains($msg, 'failed to connect')
|| str_contains($msg, 'could not connect')
|| str_contains($msg, "couldn't connect")
) {
return StreamTransportException::ERROR_TYPE_CONNECTION_RESET;
}
return StreamTransportException::ERROR_TYPE_UNKNOWN;
}
}