最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Maximum number of http requests using web workers - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Aug 25, 2014 at 15:35 mpickellmpickell 3413 silver badges7 bronze badges 6
  • See browserscope/?category=network for browser connection limits. – Halcyon 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? – mpickell 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. – idbehold 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. – mpickell 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. – mpickell Commented Aug 28, 2014 at 18:06
 |  Show 1 more ment

1 Answer 1

Reset to default 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./320/240';

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

}());

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

发布评论

评论列表(0)

  1. 暂无评论