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?

6
  • See browserscope.org/?category=network for browser connection limits. Commented Aug 25, 2014 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? Commented Aug 25, 2014 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. Commented Aug 25, 2014 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. Commented Aug 28, 2014 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. Commented Aug 28, 2014 at 18:06

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I tested this with a dummy API that delays the response. Firefox concurrently opens many requests in parallel (I guess ca 100) and receives the response from all 100 calls at more or less the same time. The script is: ``` <script> const blob = new Blob(["fetch('hub.dummyapis.com/delay?seconds=5').then(r => console.log(r))"], {type: "text/javascript"}) for (let i = 0; i < 200; i++) new Worker(window.URL.createObjectURL(blob)) </script>```
One can also use a single web worker to perform a lot of http requests at the same time (in my Firefox ca 100): const workerSource = new Blob(["onmessage = ev => fetch('https://hub.dummyapis.com/delay?seconds=5').then(r => postMessage(r.status))"], {type: "text/javascript"}) const reqCount = 200; const worker = new Worker(window.URL.createObjectURL(workerSource)) for (let i = 0; i < reqCount; i++) worker.postMessage("") worker.onmessage = ev => console.log(new Date().toUTCString(), ev.data)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.