API requests are slow? This time the pot is really not at the back end

question

During the development process, we found that the backend API request was very slow, so we complained to the backend.

"Why is the API so slow? It takes more than ten seconds to request an interface."

Moreover, this situation occurs occasionally. Front-end development students said that it sometimes occurs, but it is not necessary.

However, after an operation, the back-end students found that there was no problem with the interface. They tried it through the postman tool and the test environment, and found that the speed of the interface request was no problem.

"That feels like a front-end problem"?

Let's sort out the problem as follows:

  • Backend API requests are particularly slow and sporadic.
  • There is no reproduction in the test environment.
  • The postman tool request did not reproduce.

problem solving process

Where's the time?

The first question, what is the time spent on the API used for?

We open the Chrome debugger. You can see the time-consuming of each interface in the network.

Hover to the Waterful of your time-consuming interface, and you can see the specific time-consuming of the interface.

It can be seen that the time-consuming mainly lies in the waiting time from when Stalledthe browser obtains the instruction to issue this request to the time when the request can be issued , which is generally the time for proxy negotiation and waiting for the release of the reusable TCP connection, excluding DNS query , establishing a TCP connection, etc.

Therefore, the API has been waiting for the instructions sent by the browser to it. Taking the screenshot above as an example, it has waited for 23.84S, and its request and response time is very fast (a few hundred milliseconds at most, which is what the backend said interface is not slow).

So what exactly is the API waiting for the browser to do?

What is blocking the request?

After positioning, we found that Server-Sent Events (hereinafter referred to as SSE) is used in our project. Like WebSocket, it pushes information from the server to the browser. But the difference is that it uses the HTTP protocol.

当不通过 HTTP / 2 使用时,SSE 会受到最大连接数的限制,限制为 6 次。此限制是针对每个浏览器 + 域的,因此这意味着您可以跨所有选项卡打开 6 个 SSE 连接到 www.example1.com,并打开 6 个 SSE 连接到 www.example2.com。这一点可以通过以下这个 demo 复现。

复制问题的步骤:

结果是,第 6 次之后,SSE 请求一直无法响应,打开新的标签到同一个地址的时候,浏览器也无法访问。

效果图如下:

该问题在 ChromeFirefox 中被标记为“无法解决”。

至于偶现,是因为前端开发者有时候用 Chrome 会打开了多个选项卡,每个选项卡都是同一个本地开发地址,就会导致达到 SSE 的最大连接数的限制,而它的执行时间会很长,也就会阻塞其他的请求,一致在等待 SSE 执行完。

所以解决的方法是什么?

解决方案

简单粗暴的两个方法

  • 不要打开太多个选项卡。这样就不会达到它的限制数。(因为我们一个选项卡只请求一个 SSE)。
  • 开发环境下,关闭该功能。

使用 HTTP / 2

使用 HTTP / 2 时,HTTP 同一时间内的最大连接数由服务器和客户端之间协商(默认为 100)

这解释了为什么我们 test 环境没有问题,因为 test 环境用的是 HTTP / 2。而在开发环境中,我们使用的是 HTTP 1.1 就会出现这个问题。

那如何在开发环境中使用 HTTP / 2 呢?

我们现在在开发环境,大部分还是使用 webpack-dev-server 起一个本地服务,快速开发应用程序。在文档中,我们找到 server 选项,允许设置服务器和配置项(默认为 'http')。

只需要加上这一行代码即可。

devServer: {
+  server: 'spdy',
  port: PORT,
}
复制代码

看看效果,是成功了的。

原理使用 spdy 使用自签名证书通过 HTTP/2 提供服务。需要注意的一点是:

该配置项在 Node 15.0.0 及以上的版本会被忽略,因为 spdy 在这些版本中不会正常工作。一旦 Express 支持 Node 内建 HTTP/2,dev server 会进行迁移。

总结归纳

原本这个问题认为跟前端无关,没想到最后吃瓜吃到自己头上。提升相关技能的知识储备以及思考问题的方式,可能会方便我们定位到此类问题。

充分利用好浏览器的调试工具,对一个问题可以从多个角度出发进行思考。比如一开始,没想到本地也可以开启 HTTP / 2。后来偶然间想搜下是否有此类方案,结果还真有!

我正在参与掘金技术社区创作者签约计划招募活动,点击链接报名投稿

Guess you like

Origin juejin.im/post/7119074496610304031