nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Socket Freeze Issue During High Data Volume in Node.js Application #4361

Closed Gabriel-id closed 3 months ago

Gabriel-id commented 3 months ago

Details

I'm having a problem with a Node.js application. This application is a client that reads data from a TCP connection using net.socket. When there's a sudden surge in data volume, the socket appears to freeze, preventing further data arrival. After a period of being stuck, the socket unlocks, and the previously withheld data starts arriving, as if it had been temporarily held back and then released.

Node.js version

18.7.1

Example code

const connect = require('node:net').connect
const PassThrough = require('node:stream').PassThrough

const TELNET_HOST = process.env.TELNET_HOST
const TELNET_PORT = process.env.TELNET_PORT

const broadcaster = new PassThrough()
const socket = connect(
  {
    host: TELNET_HOST,
    port: TELNET_PORT,
    onread: {
      buffer: Buffer.alloc(32 * 1024),
      callback: (nread, buffer) => socket.emit('data', buffer.subarray(0, nread)),
    },
  },
  () => console.info('Connected')
)

socket.on('data', (data) => broadcaster.write(data))

Operating system

Linux

Scope

code and runtime

Module and version

Not applicable.

gireeshpunathil commented 3 months ago

@Gabriel-id -

Gabriel-id commented 3 months ago

@Gabriel-id -

  • when you say sudden surge in data volume, what is it like? MBs? GBs? TBs?
  • why are you re-emitting? can't you do broadcaster.write in the callback itself?
gireeshpunathil commented 3 months ago

Yes, it would be possible. It was just one of the tests I was conducting.

ok - try that. i.e, omit the unread callback and just do things in socket.on(data) callback and see if it removes the freeze?

Trott commented 3 months ago

Closing as it's been 2 weeks without a reply to the suggestion in https://github.com/nodejs/help/issues/4361#issuecomment-2012508524.