Paul Draper
Paul Draper

Reputation: 83393

How to not lose data with sendHandle?

Node.js child_process docs say

The optional sendHandle argument that may be passed to subprocess.send() is for passing a TCP server or socket object to the child process...Any data that is received and buffered in the socket will not be sent to the child.

So if pass a socket to a child process, how do I not lose the buffered data?

I called socket.read() on the socket before sending it to check for buffered data. It returned null, yet data was still lost.

How do I pass a socket from one process to another without losing data?

Upvotes: 3

Views: 335

Answers (1)

Paul Draper
Paul Draper

Reputation: 83393

The only solution is to never start reading from the socket. For example, in the internal/cluster/round_robin_handle module

this.handle = this.server._handle;
this.handle.onconnection = (err, handle) => this.distribute(err, handle);

This overwrites the normal processessing of new TCP handles, so a child process can do it instead.

(I would have used the cluster module, except I needed some customization, like least connection load balancing.)

Upvotes: 3

Related Questions