合理数量的并发异步 Ajax 请求

发布于 2024-09-09 06:49:58 字数 222 浏览 1 评论 0原文

我想知道一般允许多少个同时异步 ajax 请求的共识是什么。

我问的原因是我正在开发个人网络应用程序。在大多数情况下,我将我的要求减少到一个。然而,在某些情况下,我会同时发送最多 4 个请求。这会导致一点延迟,因为浏览器一次只能处理 2 个。

目前,延迟在可用性方面不是问题。如果有的话,我还需要一段时间才能担心可扩展性。但我会尽力遵循合理的最佳实践。你有什么想法? 4 个请求是一个合理的数字吗?

I'm wondering what the consensus is on how many simultaneous asynchronous ajax requests is generally allowable.

The reason I ask, is I'm working on a personal web app. For the most part I keep my requests down to one. However there are a few situations where I send up to 4 requests simultaneously. This causes a bit of delay, as the browser will only handle 2 at a time.

The delay is not a problem in terms of usability, for now. And it'll be awhile before I have to worry about scalability, if ever. But I am trying to adhere to best practices, as much as is reasonable. What are your thoughts? Is 4 requests a reasonable number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

下雨或天晴 2024-09-16 06:49:58

我很确定浏览器无论如何都会限制您可以拥有的连接数量。

如果您使用的是 Firefox,请输入 about:config 并查找 network.http.max-connections-per-server,这将告诉您最大连接数。我几乎肯定这也将是 AJAX 连接的限制。我认为 IE 仅限于 2。我不确定 Chrome 或 Opera。

编辑:

在 Firefox 23 中,名为 network.http.max-connections-per-server 的首选项不存在,但有一个 network.http.max-persistent-connections-per -server,默认值为 6。

I'm pretty sure the browser limits the number of connections you can have anyway.

If you have Firefox, type in about:config and look for network.http.max-connections-per-server and that will tell you your maximum. I'm almost positive that this will be the limit for AJAX connections as well. I think IE is limited to 2. I'm not sure about Chrome or Opera.

Edit:

In Firefox 23 the preference with name network.http.max-connections-per-server doesn't exist, but there is a network.http.max-persistent-connections-per-server and the default value is 6.

や三分注定 2024-09-16 06:49:58

这实际上取决于它是否能正常工作。如果应用程序的逻辑构建为 4 个同时请求有意义,请这样做。如果逻辑不会因将多个请求打包到一个请求中而受到干扰,则可以这样做,但前提是它不会使代码变得更加复杂。保持简单直接,直到出现问题,然后才可以开始优化。

但你可以问问自己,应用程序的设计是否可以改进,不再需要多个请求。

还要在连接速度非常慢的情况下检查它。同时的 http 请求不一定以正确的顺序在服务器上执行,它们也可能以不同的顺序返回。这可能会带来只有在较慢的线路上才会遇到的问题。

That really depends on if it works like that properly. If the logic of the application is built that 4 simultaneos requests make sense, do it like this. If the logic isn't disturbed by packing multiple requests into one request you can do that, but only if it won't make the code more complicated. Keep it as simple and straight forward until you have problems, then you can start to optimize.

But you can ask yourself if the design of the app can be improved that there is no need for multiple requests.

Also check it on a really slow connection. Simultaneous http requests are not necessarily executed on the server in the proper order and they might also return in a different order. That might give problems you'll experience only on slower lines.

贪恋 2024-09-16 06:49:58

在不了解一些细节的情况下很难回答。如果您只是触发请求并忘记它们,那么 4 个请求就可以了,20 个请求也可以 - 只要用户体验不会因性能缓慢而受到损害。但如果您必须从服务收集回信息,那么协调这些响应可能会变得很棘手。这可能是值得考虑的事情。

克里斯蒂安之前的回答有一个很好的观点 - 在慢速连接上检查它。 Fiddler 可以提供帮助,因为它允许您通过模拟不同的连接速度(56K 及以上)来测试慢速连接。

您还可以考虑向控制服务触发一个可能包含一条或多条消息的异步请求,然后控制服务可以将消息传递给适当的服务,收集结果,然后返回给客户端。触发多个异步请求然后在不同时间返回可能会给用户带来不稳定的体验,因为每个响应随后在不同时间呈现在页面上。

It's tough to answer without knowing some details. If you're just firing the requests and forgetting about them, then 4 requests could be fine, as could 20 - as long as the user experience isn't harmed by slow performance. But if you have to collect information back from the service, then coordinating those responses could get tricky. That may be something to consider.

The previous answer from Christian has a good point - check it on a slow connection. Fiddler can help with that as it allows you to test slow connections by simulating different connection speeds (56K and up).

You could also consider firing a single async request that could contain one or more messages to a controlling service, which could then hand the messages off to the appropriate services, collect the results and then return back to the client. Having multiple async requests being fired and then returning at different times could present a choppy experience for the user as each response is then rendered on the page at different times.

此刻的回忆 2024-09-16 06:49:58

根据我的经验,1 是最好的数字,但我同意在极少数情况下可能需要同时进行呼叫。

我记得,IE 是唯一仍将连接数限制为 2 个的浏览器。这会导致您的请求排队,如果您的前 2 个请求花费的时间超过预期或超时,则其他两个请求将自动失败。在某些情况下,您还会在 IE 中看到烦人的“允许脚本继续”对话框。

如果您的用户在所有 4 个请求返回之前无法真正执行任何操作(特别是 IE 的 JavaScript 性能陷入困境),我将创建一个包含所有请求数据的传输对象,然后创建一个可以解析和委托的返回传输对象返回。

In my experience 1 is the best number, but I'll agree there may be some rare situations that might require simultaneous calls.

If I recall, IE is the only browser that still limits connections to 2. This causes your requests to be queue, and if your first 2 requests take longer than expected or timeout, the other two requests will automatically fail. In some cases you also get the annoying "allow script to continue" dialog in IE.

If your user can't really do anything until all 4 requests come back (especially with IE's bogged down JavaScript performance) I would create a transport object that contains the data for all requests and then a returning transport object that can be parsed and delegated on return.

温柔戏命师 2024-09-16 06:49:58

我不是网络方面的专家,但对于中小型应用程序来说,四个可能不会有太大问题,但是,它越大,服务器负载就越高,最终可能会导致问题。这确实不能回答您的问题,但这是一个建议。如果延迟不是问题为什么不使用队列。

var request = []//a queue of the requests to be sent to the server

request[request.length] = //whatever you want to send to the server
startSend();

function startSend(){//if nothing is in the queue go ahead and send this one
  if(request.length===1){
    send();
  }
}

function send(){//the ajax call to the server using the first request in queue
  var sendData = request[0];
  //code to send the data
  //then when you get the response (I can't remember exactly the code for it)
  //send it to a function to process the data
}

function process(data){
  request.splice(0,1);
  if(request.length>0){//check to see if you need to do another ajax call
    send();
  }
  //process data
}

这可能不是最好的方法,但您可以将其修改为执行 2 个请求,而不是仅执行一个请求。另外,也许您可​​以修改它,将队列中的请求作为一个请求发送。然后服务器将它们分开并处理每一个并将数据发送回。一次全部完成,甚至在获取数据时完成,因为服务器可以多次刷新数据。您只需确保正确解析响应文本即可。

I'm not an expert in networking, but probably four wouldn't be much of a problem for a small to medium application, however, the larger it gets the higher the server load which could eventually cause problems. This really doesn't answer your questions, but here is a suggestion. If delay is not a problem why don't you use an queue.

var request = []//a queue of the requests to be sent to the server

request[request.length] = //whatever you want to send to the server
startSend();

function startSend(){//if nothing is in the queue go ahead and send this one
  if(request.length===1){
    send();
  }
}

function send(){//the ajax call to the server using the first request in queue
  var sendData = request[0];
  //code to send the data
  //then when you get the response (I can't remember exactly the code for it)
  //send it to a function to process the data
}

function process(data){
  request.splice(0,1);
  if(request.length>0){//check to see if you need to do another ajax call
    send();
  }
  //process data
}

This is probably isn't the best way to do it, but that's the idea you could probably modify it to do 2 requests instead of just one. Also, maybe you could modify it to send as many requests as their are in the queue as one request. Then the server splits them up and processes each one and sends the data back. All at once or even as it gets it since the server can flush the data several times. You just have to make sure your are parsing the response text correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文