我应该怎么做才能完全耗尽我的 HTTP 连接池?

问题描述 投票:0回答:1

我想通过 JavaScript 完全填充 HTTP 连接池。

for (let i = 0; i < 255; i++) {
            fetch(`http://testurl`, {
                headers: new Headers({'Connection': 'keep-alive'}),
                keepalive: true
            });
        }

这个方法我试过了,但是不行。我该怎么办?

我试过了

<script>
    async function test() {
        for (let i = 0; i < 255; i++) {
            fetch(`https://httpbin.org/delay/0`, {
                headers: new Headers({'Connection': 'keep-alive'}),
                keepalive: true
            });
        }
    }
    async function exploit() {
        await test();
        fetch(`https://httpbin.org/delay/0`, {
            headers: new Headers({'Connection': 'keep-alive'}),
            keepalive: true
        });
    }
    exploit()
</script>

但是,所有请求都会按顺序成功。我想填满HTTP连接池并导致后续请求失败。

javascript http connection
1个回答
0
投票

我认为您正在寻找并行化所有调用的开始,如下所示的内容会起作用:

Promise
  .all(Array(255)
    .map(it => 
      fetch(`http://testurl`, {
        headers: new Headers({'Connection': 'keep-alive'}),
        keepalive: true
      })
    )
  )
© www.soinside.com 2019 - 2024. All rights reserved.