axios Confusing timeout property

46qrfjad  于 9个月前  发布在  iOS
关注(0)|答案(2)|浏览(203)

需要改进的部分

Request Config 中的 timeout 属性

建议的改进

timeout 属性的描述说:
timeout 指定请求超时的毫秒数。如果请求花费的时间超过 timeout,请求将被中止。
我认为在 Node.js 中这是不正确的。Axios 在这里调用了 request.setTimeout(),并在内部调用了 socket.setTimeout()。API 文档解释如下:
设置套接字在套接字上的 timeout 毫秒的不活动后超时
https://nodejs.org/api/net.html#socketsettimeouttimeout-callback
这意味着套接字在一段时间内没有发送或接收数据时会发出 timeout 事件。在请求负载巨大或网络速度慢的情况下,请求可能会超过指定的 timeout
这是重现代码。服务器发送的数据速度很慢,大约需要 10 秒,这比 timeout 更长。

  1. import http from 'http';
  2. import Axios from 'axios';
  3. import timer from 'timers/promises';
  4. (async () => {
  5. const server = http.createServer((req, res) => {
  6. (async () => {
  7. for (let i = 0; i < 100; i++) {
  8. res.write('.');
  9. await timer.setTimeout(100);
  10. }
  11. res.end();
  12. })();
  13. });
  14. await new Promise((resolve) => { server.listen(3000, resolve) });
  15. const startAt = Date.now();
  16. const axios = await Axios.get('http://127.0.0.1:3000', { "timeout": 5000 });
  17. const endAt = Date.now();
  18. console.log(`Received ${axios.data.length} bytes in ${endAt - startAt} ms`);
  19. server.close();
  20. })();

输出:

  1. $ node index.mjs
  2. Received 100 bytes in 10035 ms

相关文件

  • 无响应*
0h4hbjxa

0h4hbjxa1#

我描述了GET请求可能会超过超时时间。另一方面,我发现当POST请求的负载过大时,如果请求花费的时间超过了超时时间,就会抛出一个超时错误。这种行为取决于axios内部使用的follow-redirects,它与标准的socket.setTimeout()不同。GET和POST之间的这种差异是出乎意料的,我在follow-redirects仓库中发现了一些问题。

我认为这个问题不仅仅是文档问题。最好你能修复axios(或者follow-redirects)。

wj8zmpe1

wj8zmpe12#

你能把这个问题分配给我吗?

相关问题