-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Description
Is your feature request related to a problem? Please describe.
Problem 1
Assume the server has a limited count of concurrent streams, e.g. 10. We want to make 100 requests. This code:
const session = http2.connect(...);
const requests = [];
for (let i = 0; i < 100; i++) {
requests.push(session.request({}, {endStream: false}));
}will fail, because the server can handle only 10 requests at the same time. To fix this, we listen on the remoteSettings event.
const session = http2.connect(...);
const requests = [];
session.once('remoteSettings', () => {
const {maxConcurrentStreams} = session.remoteSettings;
// 1. Do Math.min(maxConcurrentStreams, 100) requests
// 2. When one finishes, start another.
});But what if the server decides to close the session without sending us the settings? Then the code will never execute. To fix this, we add the close event handler. Unfortunately, we cannot tell if the code was executed or not just by listening on close. So we need to add a flag:
const session = http2.connect(...);
const requests = [];
let receivedSettings = false;
session.once('remoteSettings', () => {
const {maxConcurrentStreams} = session.remoteSettings;
// 1. Do Math.min(maxConcurrentStreams, 100) requests
// 2. When one finishes, start another.
});
session.once('close', () => {
if (!receivedSettings) {
emitter.emit('error', new Error('Session closed unexpectedly'));
}
});The flag can be implemented by Node.js - and that would be useful.
Problem 2
So the session is already running, and there are some requests pending. How do we know how many requests we can do? Well, we don't. There's no property that tells us how many streams there are.
Describe the solution you'd like
session.currentStreamCountsession.receivedRemoteSettings
Describe alternatives you've considered
As of now I'm applying a workaround to calculate the properties above on my own.