之前调用一个三方的 WEB API,大量的请求超时,导致 PHP 进程被占用完。整个网站一直报 504。

其中一个优化措施就是对三方 API 调用设置超时时间。

use GuzzleHttp\Client;
        
$client = new Client();
$url = 'https://www.sunzhongwei.com';
try {
    $res = $client->request('GET', $url, ['timeout' => 1.5]);
    $res = $res->getBody();
} catch(\Throwable $e) {
    Log::info('Fail to call api‘);
}

timeout 默认值是 0, 即一直等待,这非常危险。所以这里改成了 1.5 秒。

相关文章:

  • 2021-07-28
  • 2021-05-17
  • 2022-12-23
  • 2021-06-03
  • 2021-07-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
  • 2022-02-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案