0

I am creating Http/2 server where sending a large number(approx. > 50000) of request from server to client. But after sending this number of request it is giving error.

Note: By appling some delay (0.5ms || 0.05ms) in betweeen in each request sent from client to server the number of request sent is increasing depending upon time. But I don't want any delay in between them and want to do a load test for even more number of request?

Can tell me why I am getting this error? what are the possible solutions? I `CLIENT SIDE: error : {"Error":{"code":"ERR_HTTP2_STREAM_ERROR"},"index":29498} error : {"Error":{"code":"ERR_HTTP2_STREAM_ERROR"},"index":29499} . . .

SERVER SIDE: [ERR_HTTP2_STREAM_ERROR]: Stream closed with error code NGHTTP2_INTERNAL_ERROR","streamId":44703,"time":1.083,"total time":5.414000000000001,"index":"22351"} error : {"msg":"Error occured Error [ERR_HTTP2_STREAM_ERROR]: Stream closed with error code NGHTTP2_INTERNAL_ERROR","streamId":44705,"time":1.083,"total time":6.497000000000001,"index":"22352"} error : {"msg":"Error occured Error [ERR_HTTP2_STREAM_ERROR]: Stream closed with error code NGHTTP2_INTERNAL_ERROR","streamId":44707,"time":1.083,"total time":7.580000000000001,"index":"22353"} error : {"msg":"Error occured Error . . .

Few changes earlier I was getting this kind of error error=Error [ERR_HTTP2_STREAM_ERROR]: Stream closed with error code NGHTTP2_INTERNAL_ERROR

Here is my code

client
const http2 = require('node:http2');
const fs = require('node:fs');
const logger = require('gk-logger')()
const sleep = require('sleep');

const generateRandomWord = (length = 10) => {
  const alphabetList = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  let randomWord = '';

  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * alphabetList.length);
    randomWord += alphabetList[randomIndex];
  }

  return randomWord;
}

const client = http2.connect('https://localhost:5000', {
  ca: fs.readFileSync('localhost-cert.pem'),
});

client.on('error', (err) => console.error("client", err));

const getTime = (startTime) => {
  return (Date.now() - startTime) / 1000;
}

console.log("sleeping before")
console.log("sleeping after")
const sendRequest = (i) => {
  const startTime = Date.now();
  logger.info(`Request is being send ${i}....`)


  const req = client.request({
    ':path': '/',
    ':method': 'POST',
    'rid': i
  });

  const payload = {
    type: 'POST Request',
    rid: i,
    key: generateRandomWord(10),
    value: generateRandomWord(20)
  }
  req.write(JSON.stringify(payload), 'utf-8')
  req.end();

  req.on('error', (err) => {
    logger.error(JSON.stringify({
      "Error": err,
      "index": i
    }))
  })

  req.on('response', (headers) => {
    logger.info(JSON.stringify({
      "Response headers": headers,
      "index": i
    }))
  })

  let data = ''
  req.setEncoding('utf-8');
  req.on('data', (chunk) => {
    data += chunk;
  })

  req.on('end', () => {
    const timeRequired = getTime(startTime)
    logger.info(JSON.stringify({
      "Client Data Received": data,
      "Client Time Required": timeRequired
    }))
  })

  logger.info(`Reach at the end of request`)
  // sleep.sleep(1)
}

client.on('connect', stream => {
  console.log("Someone Connected", stream)
  let index = 0;
  for (index = 0; index < 50000; index++) {

    //Appling delay in each request
    setTimeout((index) => {
        sendRequest(index)
    }, i * 0.05, i);
  }

})


client.on('close', () => {
  console.log("All request send closing tcp connection")
})


Server Side Code

const http2 = require('node:http2');
const fs = require('node:fs');
const logger = require('gk-logger')();
const RedisClient = require('./redis/redisClient')

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem'),
});

let total_time = 0;
server.on('stream', (stream, headers) => {
  const startTime = Date.now()
  const index = headers['rid'] ?? 'null'
  const method = headers[':method']


  if (method === 'GET') {
    stream.end(JSON.stringify({
      "Method": "Get Request",
      index: index
    }))
    return
  }
  logger.info(`request received on server ${index}`)
  let data = ''
  stream.setEncoding('utf-8')
  stream.on('data', (chunk) => {
    logger.info(JSON.stringify({ "chunk": chunk }))
    data += chunk
  })

  stream.on('aborted', () => {
    const timeRequired = getTime(startTime);
    total_time += timeRequired;
    logger.error({
      msg: 'Stream Aborted',
      streamId: stream.id,
      time: timeRequired,
      "total time": total_time,
      "index": index
    })
  })

  stream.on('error', (err) => {
    const timeRequired = getTime(startTime);
    total_time += timeRequired;
    logger.error(JSON.stringify({
      msg: `Error occured ${err}`,
      streamId: stream.id,
      time: timeRequired,
      "total time": total_time,
      "index": index
    }))
  })

  stream.on('end', () => {
    logger.info(JSON.stringify({ "Data received @server": data }))
    try {
      const payload = data === '' ? '{}' : JSON.parse(data);
      RedisClient.setKey(payload.key, payload.value).then(response => {
        logger.info(JSON.stringify(`Redis key set successfully res:${response} key:${payload.key} Value:${payload.value} index:${index}`))
        stream.respond({ ':status': 200, index: index })
        stream.end(JSON.stringify({
          "msg": "Redis key set success",
          "index": index,
          "streamId": stream.id
        }))

      }).catch(error => {
        logger.error(JSON.stringify({ "Error while parsing or setting redis key": error }))
        stream.respond({ ':status': 500, index: index })
        stream.end(JSON.stringify({
          "msg": "Redis key set failure Error",
          "index": index,
          "streamId": stream.id
        }))
      })


    } catch (error) {
      logger.error(JSON.stringify({ "Error while parsing or setting redis key": error }))
      stream.respond({ ':status': 500, index: index })
      stream.end(JSON.stringify({
        "msg": "Redis key set failure",
        "index": index,
        "streamId": stream.id
      }))
    }

  })
})


const port = 5000
server.listen(port, () => {
  console.log(`Server running https://localhost:${port}`)
})

0 Answers0