简体   繁体   English

如何限制angularjs中的$ http请求?

[英]How do I throttle $http requests in angularjs?

I'm building a UI for a data importer using angularjs. 我正在使用angularjs为数据导入器构建UI。 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. 角度应用程序将通过输入数据源(电子表格或其他)进行处理,并将GET / POST发送到API以在服务器上创建/更新记录并检索更改等。

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). 如果用户正在导入数千条记录,我可能不希望一次打开数千个ajax调用(而不是Angular能够在第一次完成之前发送所有请求)。 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. 我的想法是添加某种连接池,以便它可以被限制为10或50左右一次ajax调用。

Does angular already have a built-in means of throttling ajax calls? angular已经有一个内置的限制ajax调用的方法吗? 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. 我知道jquery有一些,但我希望尽可能避免jquery这个项目。

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. 在进一步调查中,我发现,根据这个问题这个页面 ,浏览器会自动将对同一服务器的http请求限制在6到13个并发请求之间,并且它们会排队其他请求,直到其他请求完成。 So, apparently, no action is needed, and I can just let my code fly. 所以,显然,不需要采取任何行动,我可以让我的代码飞起来。

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. 我看了angular-http-throttler (由nathancahill建议),但是(1)它似乎复制了浏览器自己已经在做什么,以及(2)它(当前)似乎没有处理错误情况的机制,因此,如果服务器没有响应,或者它是一个错误的请求,它不会减少请求计数,从而永久地堵塞它的队列。

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. 我查看了在javascript中创建固定长度队列的各种技术,它可以在队列点可用时触发回调,但javascript的非阻塞性质使得这样的队列变得棘手和脆弱......希望,我不需要那个方向(我没有处理那么多数据),浏览器的限制对我来说已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM