1

If I try to make a lot of HTTP requests at the same time, interpreter eventually throws a net::ERR_INSUFFICIENT_RESOURCES error. For example,

'use strict';

require('angular');

const app = angular.module('app', []);

app.controller('MainController', ['$scope', '$http', function($scope, $http) {
  $scope.makeRequest = function() {
    $http.get('http://example.com').then(
      function success(response) {
        // ...
      }, function error(response) {
        // ...
      });
  };

  setInterval(function() {
    $scope.makeRequest();
  }, 1);
}]);

What kind of "resources" does it mean? Application uses about 150 Mb so I guess it's not related to memory. Does it have some internal "max. requests count" constant then?

How can I handle such situations then?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • Why do you need so many requests? This looks like a heartbeat of some sort, and they are usually more spaced out than this. EDIT: damn, I now looked at the username...pun wasn't intended but I definitely find it funny. – VLAZ Oct 27 '16 at 21:23
  • @vlaz I'm trying to write a web application for stress testing – FrozenHeart Oct 27 '16 at 21:24
  • Well two things to mention then - 1) this _might_ not do what you expect to begin with. It will not fire a request every millisecond as there is a minimum delay of 40ms enforced. So it's 40 times as slow as you might have thought. 2) the error you're hitting - it's an open bug, albeit quite rare bug that I have heard of but not experienced myself. Assuming it's the same, at least - Chrome has trouble with A LOT of requests done in a short span of time. I've seen it in relation to having a lot of embedded resources, though, but it might be the same issue. – VLAZ Oct 27 '16 at 21:31
  • @vlaz Where did you get this info about 40 ms delay? – FrozenHeart Oct 27 '16 at 21:40
  • https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout#Nested_timeouts_forced_to_%3E4ms although it seems I was misremembering - the minimum is 4ms, not 40. – VLAZ Oct 27 '16 at 21:42
  • Actually, it seems that [in Node there is no minimum delay](http://stackoverflow.com/questions/7221504/does-node-js-enforce-a-minimum-delay-for-settimeout). You don't seem to be working in a browser, so I guess it doesn't affect you. Turns out I knew even less than I anticipated. – VLAZ Oct 27 '16 at 21:45
  • @vlaz Yep, I'm using Electron actually, so it's Node.js under the hood. Do you have any other ideas then? – FrozenHeart Oct 27 '16 at 21:48
  • Dunno, really. That's my first "real" experience with that bug. Again, assuming it's the Chrome one (which I guess is just V8). You could try to swap the JS engine to Chakra. Not sure how easy/feasible it is for your application, I know it can be done, though. You might also try spawning more threads, and have each issue a request at a lower pace, although I don't know if they share whatever resource a single thread runs out of with that error message. You might be able to run several apps in parallel, assuming threads don't work. These are the only ideas I have right now. – VLAZ Oct 27 '16 at 21:52
  • @vlaz Threads? I thought that JavaScript is entirely single-threaded – FrozenHeart Oct 27 '16 at 21:59
  • HA! This one I do know off the top of my head. No, the misconception comes from the fact that browsers keep one thread for the UI loop. JavaScript does not really have anything that's forcing it to run in one thread, though. After all, WebWorkers do run in a separate thread. In server-side JS you do have a thread pool, although a large part of your code will still be ran in an event loop on a single one. You can still access and utilise more, though, but you'll have to lookup how to do it exactly. – VLAZ Oct 27 '16 at 22:07

0 Answers0