21

I know there is a limit on the number of concurrent http connections that can happen at a time in a browser...

Is this a total, overall browser limit? Are web-workers subject to this limit as well, or are they treated differently because they are different threads?

mpickell
  • 341
  • 3
  • 7
  • See http://www.browserscope.org/?category=network for browser connection limits. – Halcyon Aug 25 '14 at 15:40
  • So this page suggests that there is a limit of X connections per *hostname* with a max *browser* limit of Y.. Assuming a web-worker is considered as being under the same *hostname* of the application that spawned it, am I correct to say that web workers are subject to the hostname limit then along with the rest of the application? So if I have X webworkers all using XHR requests, then i could still block the UI? – mpickell Aug 25 '14 at 17:47
  • 1
    @mpickell you cannot block the UI thread with a WebWorker thread (that's kind of the point of them). But yes, you could probably block the UI thread by having `X` WebWorkers making XHRs to the same host **and** attempting to make a synchronous XHR from the main UI thread (which should never be done). Also note that the first part isn't even a requirement, you can block the UI thread just by making any synchronous XHR from the main thread. – idbehold Aug 25 '14 at 20:31
  • 4
    @idbehold The web workers count against the max browser connection limit (or hostname limit), correct? I am creating a web worker that makes an XHR async call to get some data, process it, and return the result to the UI thread. I am creating a *pool* of instances of this worker and can set the pool to as many as I want... but I don't want the web workers to grab all possible connections and end up blocking the UI if it needs to make an XHR connection. – mpickell Aug 28 '14 at 18:01
  • 4
    (ran out of space..) My understanding is that once the connections are all used, anything else attempting to get an http connection will block the calling thread until a connection is available. So in order to set my worker pool to the right number, I am trying to figure out if counts against the overall connection count... I have not yet encountered the problem actually blocking the UI, and the pool count can be configurable.. but i'm trying to understand it better. I don't want a UI XHR to be queued at the end of all of my workers and end up blocked. – mpickell Aug 28 '14 at 18:06
  • why dont you take a safe number of webworkers, and queue the work, where webworkers handle the tasks over multiple workers. multithreading is great, but when you use 20 workers, that all use the same connection for downloading it doesnt matter anymore, you could just use 5 and finish the tasks at the same time. – Zezura Aug 05 '16 at 19:12

1 Answers1

3

TLDR: Workers (in Chrome and Firefox at least) appear to adhere to a global max number of connections per host name.

I used this code to create a number of workers...

/* jshint esversion: 6 */

(function() {

    'use strict';

    const iWorkerCount = 20;

    for(let i = 0; i < iWorkerCount; i++) {
        const oWorker = new Worker('/js/worker.js');
    }

}());

... and then each worker made a remote request to an image service...

/* jshint esversion: 6 */

(function() {

    'use strict';

    const sUrl = 'https://loremflickr.com/320/240';

    fetch(sUrl).then(sResponse => {
        console.log(sResponse);
    });

}());

There's an initial batch of requests that complete at the same time and then the remaining requests trickle in shortly once the number max requests dips

michael
  • 4,427
  • 6
  • 38
  • 57