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.
Hatalı
Bölüm başlığı “Hatalı”<?phpclass ApiService{ public function fetch(string $path): string { $client = new \GuzzleHttp\Client(); return (string) $client->get($path)->getBody(); }}<?phpclass 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(); }}Düzeltme
Bölüm başlığı “Düzeltme”HTTP istemcisini bir kez oluşturup (tercihen DI ile enjekte ederek) paylaşılan bir alan olarak yeniden kullanın.