Shawn Neal
Shawn Neal

Reputation: 115

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?

Upvotes: 5

Views: 15654

Answers (1)

damphat
damphat

Reputation: 18956

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.

Upvotes: 4

Related Questions