admin管理员组文章数量:1125717
I am trying to test for timeout scenarios for my API requests using WP_Http::request()
by passing a very short timeout
argument (a minimal fraction of a second). After my API requests continued to succeed regardless of my extremely low setting, I noticed that my timeout was being set to a thousand milliseconds (one second) even though I had set it much lower for testing.
Is there a way I can get around this and pass a timeout less than a second?
Simplest example:
$http = new WP_Http;
$response = $http->request(
'YOUR_URL',
array(
'timeout' => 0.00001,
),
);
(timeout
is set small to test for a real-world timeout scenario, but it doesn't fail as expected unless over 1,000 ms)
I am trying to test for timeout scenarios for my API requests using WP_Http::request()
by passing a very short timeout
argument (a minimal fraction of a second). After my API requests continued to succeed regardless of my extremely low setting, I noticed that my timeout was being set to a thousand milliseconds (one second) even though I had set it much lower for testing.
Is there a way I can get around this and pass a timeout less than a second?
Simplest example:
$http = new WP_Http;
$response = $http->request(
'YOUR_URL',
array(
'timeout' => 0.00001,
),
);
(timeout
is set small to test for a real-world timeout scenario, but it doesn't fail as expected unless over 1,000 ms)
- can you edit your question to include the code you used? As well as how you confirmed the thousand milliseconds? – Tom J Nowell ♦ Commented Feb 29, 2024 at 19:20
1 Answer
Reset to default 2I suspect this is the cause in the Requests library that powers WP_HTTP:
- Transfer & connect timeouts, in seconds & milliseconds
cURL is unable to handle timeouts under a second in DNS lookups, so we round those up to ensure 1-999ms isn't counted as an instant failure.
The code looks to be here:
https://github.com/WordPress/Requests/blob/90db63ab54449c071fc2192a4fc3487cafbf67d0/src/Transport/Curl.php#L448-L460
if (is_int($timeout) || $this->version < self::CURL_7_16_2) {
curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));
} else {
// phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_timeout_msFound
curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
}
If your goal is to test that a response time is within a certain limit by using the timeout so it fails when it takes too long, as clever as it sounds there are other things that interfere. Instead timing the request at the API server side, and at the client side, then using the time taken to implement your own timeout test would be more reliable ( and give you the opportunity to add more logic, e.g. a warning threshold ).
https://github.com/WordPress/Requests/issues/58
It's also possible your server is using an old version of curl that does not support microsecond based timeouts
本文标签: http apiwphttp remote request not respecting timeout
版权声明:本文标题:http api - wp_http remote request not respecting timeout 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736632884a1945815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论