Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set maximum buffer size in Node.js?

I was creating a server application, which uses socket.write() to send data back to the clients.

Here, I got the problem of managing buffer size. Suppose the connection between server and client broke down, and server was not aware of the problem. So, it keeps writing to the buffer. In such situation, I want to limit the maximum buffer size, so that it can throw errors before it consumes lots of resources.

I know kMaxLength in node_buffer.h controls the size of buffer, but I do not think it's a good idea to change its value via some self-defined methods. Is there any other method to manage the size?

like image 838
Shawn Neal Avatar asked Oct 03 '22 01:10

Shawn Neal


1 Answers

kMaxLength is the limit value for memory allocation, it is much bigger then socket.buffSize

http://nodejs.org/docs/v0.11.5/api/smalloc.html

You can read about socket.buffSize here:

http://nodejs.org/api/net.html#net_socket_buffersize

You can also see socket.bytesRead, socket.bytesWritten to control your data transmission.

like image 161
damphat Avatar answered Oct 13 '22 12:10

damphat