Cache and reuse the Guzzle client for multiple requests

This commit is contained in:
Kijin Sung 2023-04-23 00:26:27 +09:00
parent 866d704d0d
commit d731198aa6

View file

@ -12,6 +12,19 @@ class HTTP
*/
public const DEFAULT_TIMEOUT = 3;
/**
* Cache the Guzzle client instance here.
*/
protected static $_client = null;
/**
* Reset the Guzzle client instance.
*/
public static function resetClient(): void
{
self::$_client = null;
}
/**
* Make a GET request.
*
@ -160,9 +173,12 @@ class HTTP
}
// Send the request.
$guzzle = new \GuzzleHttp\Client();
if (!self::$_client)
{
self::$_client = new \GuzzleHttp\Client();
}
$start_time = microtime(true);
$response = $guzzle->request($method, $url, $settings);
$response = self::$_client->request($method, $url, $settings);
$status_code = $response->getStatusCode() ?: 0;
// Measure elapsed time and add a debug entry.