跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 有个前端并发数的问题

发表于

前文

浏览器能够并行发送请求,但是每次并行发送请求的个数是有限制的,以 chrome 浏览器为例子:

同一个域名下,同一个 GET 请求的并发数是 1 ,也就是说只有上一个请求结束,才会执行下一个请求,否则置入队列等待发送。 同一个域名下,不同 GET/POST 请求的并发数是 6 。当发送的请求数量达到 6 个,并且都没有得到响应时,后面的请求会置入队列等待发送。

测试

    useEffect(
        () => {
            for (let i = 0; i < 10; i++) {
                requestDelay();
            }
            console.log(randoms)
        },
        [])

    const SLEEP_MS = 1000;

    useEffect(() => {
        const interval = setInterval(() => {
            requestInstant();

        }, SLEEP_MS);

        return () => clearInterval(interval); // This represents the unmount function, in which you need to clear your interval to prevent memory leaks.
    }, [])

requestDelay(); 这个方法在后端会无限超时(sleep 10000000 秒)。 requestInstant(); 这个方法会立即返回。

当前的表象是:

第一秒:requestDelay()已经发送出网络请求,后端已经收到。后续的 requestDelay()没有向后端发送请求,目测已经进入队列。
第二秒到第十几秒:每秒都会 requestInstant()请求到后端,且立即返回。
然后就是发送了五次 requestDelay()请求,后端已收到。
后续的 requestInstant()请求已经卡住,一直没有向后端发送请求。

疑问:我一直以为从第二秒开始就一直会每秒请求 requestInstant(),且立即返回。不知道为什么会后续又发送 5 次的 requestDelay()请求?老哥们帮忙解释下。

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.