Angularjs 多个 HTTP POST 请求导致 net::ERR_INSUFFICIENT_RESOURCES



在我的应用程序中,我正在文本区域字段表单中获取用户提供的主机列表,并使用HTTP POST to API将它们插入我的数据库。

一切正常,直到列表超过2.5k主机,当我收到net::ERR_INSUFFICIENT_RESOURCES错误。我读到它与一些 Chrome 限制有关。

如何克服此限制?我试图将列表拆分为束并在它们之间引入一些延迟,但它不起作用(似乎所有束都在同一时间异步启动(。

控制器:

AddHostsController.$inject = ['$scope', 'Authentication', 'App', 'Hosts', '$filter', '$timeout'];
function AddHostsController($scope, Authentication, App, Hosts, $filter, $timeout) {
var vm = this;
vm.submit = submit;
...some other staff...
function submit() {
var fulllist = [];
vm.firstbunch = [];
vm.secondbunch = [];
fulllist = vm.host_list.split('n');
vm.firstbunch = fulllist.slice(0,1500);
vm.secondbunch = fulllist.slice(1500,);
$timeout(function() { vm.firstbunch.forEach(submitHost);}, 2000)
.then(firstBunchSuccessFn, firstBunchErrorFn);
function firstBunchSuccessFn(){
vm.secondbunch.forEach(submitHost);
}
function firstBunchErrorFn(){
console.log("Something went wrong!");
}
function submitHost(value, index, array){
App.addhosts(...some args..., value).then(addHostsSuccessFn, addHostsErrorFn);
function addHostsSuccessFn(response) {
}
function addHostsErrorFn(response) {
console.error('Failure!');
}
}
}

服务:

.factory('App', App);
App.$inject = ['$http'];
function App($http) {
var App = {
addhosts: addhosts,
};
return App;
function addhosts(...some other.., value) {
return $http.post('/api/v1/hosts/', {
...some other...
value: value
});
}

与其并行执行请求,不如将它们链接起来:

var configArr = [/* Array of config objects */];
var resultsArrPromise = configArr.reduce( reducerFn, $q.when([]) );
responseArrPromise
.then(function (responseArr) {
responseArr.forEach( response => console.log(response.data) );
}).catch(function (errorResponse) {
console.log(errorResponse);
}); 
function reducerFn(acc, config) {
var accArrPromise = acc.then(function(responseArr) {
var httpPromise = $http(config);
return $q.all( [...responseArr, httpPromise] );
});
return accArrPromise;
}

化简器以空数组的承诺开始。化简器的每次迭代都将另一个 HTTP 承诺链接到响应数组。结果是一个承诺,该承诺解析为一系列响应。通过链接 HTTP 请求,它们将按顺序执行,而不是并行执行。

最新更新