Ir al contenido

HTTP client lifetime (ARCH7083)

Esta página aún no está disponible en tu idioma.

Key php.performance.http-client-lifetime · Severity error · Category performance

Creating a new HTTP client per request opens fresh connection pools and leads to socket exhaustion. Build the client once and reuse it.

<?php
class ApiService
{
public function fetch(string $path): string
{
$client = new \GuzzleHttp\Client();
return (string) $client->get($path)->getBody();
}
}
<?php
class ApiService
{
private \GuzzleHttp\Client $client;
public function __construct(\GuzzleHttp\Client $client)
{
$this->client = $client;
}
public function fetch(string $path): string
{
return (string) $this->client->get($path)->getBody();
}
}

Construct the HTTP client once (ideally injected via DI) and reuse it as a shared field.