İçeriğe geç

HTTP istemci yaşam süresi (ARCH7083)

Anahtar php.performance.http-client-lifetime · Önem error · Kategori performance

Her istek için yeni bir HTTP istemcisi oluşturmak yeni bağlantı havuzları açar ve soket tükenmesine yol açar. İstemciyi bir kez oluşturup yeniden kullanın.

<?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();
}
}

HTTP istemcisini bir kez oluşturup (tercihen DI ile enjekte ederek) paylaşılan bir alan olarak yeniden kullanın.