如何在angularjs中限制$ http请求? [英] How do I throttle $http requests in angularjs?

查看:58
本文介绍了如何在angularjs中限制$ http请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angularjs为数据导入器构建UI.Angular应用程序将处理输入数据源(电子表格等)并将GET/POST发送到API,以在服务器上创建/更新记录并检索更改等.

I'm building a UI for a data importer using angularjs. The angular app is going to be crunching through the input data source (a spreadsheet, or whatever) and sending GETs/POSTs to an API to create/update records on the server and retrieve changes, etc.

如果用户正在导入成千上万的记录,我可能不想一次打开成千上万的ajax调用(不是Angular能够在第一次完成之前发送所有请求).我的想法是添加某种连接池,以便可以将其立即限制为仅10或50个左右的ajax调用.

If a user is importing thousands of records, I probably don't want to be opening up thousands of ajax calls at once (not that Angular would be able to get all of the requests sent out before the first finished). My thought was to add some sort of connection pool so that it could be throttled to just 10 or 50 or so ajax calls at once.

angular是否已经具有限制ajax调用的内置方法?我知道我可以轻松制造一个轮子,但是如果那里已经有一些光滑的东西,我不想重新发明轮子.有人可以为此推荐任何工具/插件吗?我知道有一些jquery,但我希望这个项目尽可能避免使用jquery.

Does angular already have a built-in means of throttling ajax calls? I know I could build one without too much trouble, but I don't want to re-invent the wheel if there's already something slick out there. Can anyone recommend any tools/plugins for that? I know there are a few for jquery, but I'm hoping to avoid jquery, as much as possible, for this project.

推荐答案

在进一步调查中,我发现根据此页面,浏览器会自动将对同一服务器的http请求限制在6到13个并发请求之间,然后将其他请求排队,直到其他请求完成为止.因此,显然,无需采取任何措施,我只需让代码飞起来即可.

In investigating this further, I found that, according to this question and this page, browsers automatically throttle http requests to the same server to somewhere between 6 and 13 concurrent requests, and they queue up additional requests until other requests complete. So, apparently, no action is needed, and I can just let my code fly.

我看了 angular-http-throttler (由nathancahill建议)(1)似乎复制了浏览器自己已经在做的事情,(2)它似乎没有(目前)具有处理错误情况的机制,因此,如果服务器没有响应,或者服务器是否响应了,错误的请求,它不会减少请求数,因此会永久阻塞请求队列.

I looked at angular-http-throttler (suggested by nathancahill), but (1) it appears to duplicate what browsers are already doing by themselves, and (2) it doesn't (currently) appear to have a mechanism for handling error cases, so if the server doesn't respond, or if it was a bad request, it doesn't decrement the request count, thus permanently clogging up it's queue.

如果处理大量数据,一次让浏览器排队太多请求仍然会导致内存/性能问题.我研究了各种在javascript中创建固定长度队列的技术,当队列位置可用时,它可以触发回调,但是javascript的非阻塞性质使此类队列棘手&.脆弱...希望,我不需要朝这个方向发展(我不需要处理太多数据),浏览器的节流功能对我来说已经足够了.

Letting the browser queue up too many requests at once can still cause memory/performance issues if dealing with very large amounts of data. I looked at various techniques for creating a fixed-length queue in javascript, where it can fire off a callback when a queue spot becomes available, but the non-blocking nature of javascript makes such a queue tricky & fragile... Hopefully, I won't need to go that direction (I'm not dealing with THAT much data), and the browser's throttling will be sufficient for me.

这篇关于如何在angularjs中限制$ http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆