help icon indicating copy to clipboard operation
help copied to clipboard

Cannot release streams created by one http2 connection

Open sonlm opened this issue 3 years ago • 0 comments

Details

I use one http2 connection and create streams with this connection. Each stream is closed by server after finishing to send response to client (process time is very fast because I response as soon as receive request). I set an interval to create stream every 1 second. After 100 streams, I receive message New streams cannot be created. I see that 100 is the maximum keepalive_requests in Nginx config. But why these streams do not close and are keepalive by Nginx although I have closed them ?

let client = http2.connect('https://myserver:443')
setInterval(()=>{
  const base64data = (new Buffer.from(JSON.stringify(eventMessage))).toString('base64')
  var stream = client.request({
    ":method": "POST",
    ":path": "/stream",
    'meta': base64data,
    "Authorization": process.env.ACCESS_TOKEN
  });
  stream.on('data', (message)=>{console.log(message.toString())})
  stream.on('close', ()=>{
    console.log('Stream is closed')
  })
  stream.on('error', (err)=>{console.log(err)})
}, 1000)

Output

Count: 98
Stream is closed
{"status":true}
Count: 99
Stream is closed
{"status":true}
Stream is closed
Count: 100
Thrown:
Error [ERR_HTTP2_GOAWAY_SESSION]: New streams cannot be created after receiving a GOAWAY
    at ClientHttp2Session.request (internal/http2/core.js:1434:13)

Node.js version

14.17.0

Example code

let client = http2.connect('https://myserver:443')
setInterval(()=>{
  const base64data = (new Buffer.from(JSON.stringify(eventMessage))).toString('base64')
  var stream = client.request({
    ":method": "POST",
    ":path": "/stream",
    'meta': base64data,
    "Authorization": process.env.ACCESS_TOKEN
  });
  stream.on('data', (message)=>{console.log(message.toString())})
  stream.on('close', ()=>{
    console.log('Stream is closed')
  })
  stream.on('error', (err)=>{console.log(err)})
}, 1000)

Output

Count: 98
Stream is closed
{"status":true}
Count: 99
Stream is closed
{"status":true}
Stream is closed
Count: 100
Thrown:
Error [ERR_HTTP2_GOAWAY_SESSION]: New streams cannot be created after receiving a GOAWAY
    at ClientHttp2Session.request (internal/http2/core.js:1434:13)

Operating system

alpine

Scope

code,runtime

Module and version

http2

sonlm avatar Sep 28 '22 02:09 sonlm