Forking a new process when Node.JS receives a connection
NickName:sffc Ask DateTime:2014-05-19T22:19:59

Forking a new process when Node.JS receives a connection

I'm running a Node.JS application involving heavy child process I/O. Due to the way Node.JS handles file descriptors (among other reasons), I want to fork a new V8 instance for every connection to the server. (Yes, I'm aware that this is a potentially expensive operation, but that's not the point of this question.)

I am using nssocket for my server, but this question should apply to other types of Node.JS servers (express, Socket.IO, etc) as well.

Right now I have:

var server = require("nssocket").createServer(function(socket){
    // Do stuff with the new connection
}).listen(8000);

The intuitive thing to do is this:

// master.js
var server = require("nssocket").createServer(function(socket){
    // Fork a new process to handle the connection
    child_process.fork("worker.js");
}).listen(8000);

// worker.js
// Do stuff with the new connection

However, then the child process won't have access to the socket variable.

I've read about the new cluster API in Node, but it doesn't look like it's designed for the case when you want every connection to spawn a new worker.

Any ideas?

Copyright Notice:Content Author:「sffc」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23740185/forking-a-new-process-when-node-js-receives-a-connection

Answers
Hendrik Demmer 2014-05-19T19:53:57

The cluster API is probably closest to what you want. In theory you can call cluster.fork() at any time within the master process. Note that once the socket connection is established, there is afaik no way to hand it over to another process.\n\nTo forward the communication to the worker, you could use message passing (i.e. worker.send) or you could open another port in the worker process and direct the client there.\n\nI should stress that running significantly more worker processes than CPU cores is probably not a good idea. Have you considered pooling the workers or using a work queue like Beanstalkd?",


More about “Forking a new process when Node.JS receives a connection” related questions

Forking a new process when Node.JS receives a connection

I'm running a Node.JS application involving heavy child process I/O. Due to the way Node.JS handles file descriptors (among other reasons), I want to fork a new V8 instance for every connection to...

Show Detail

Does a new node.js process created by fork (new process) or spawn (child process) get it's own separate call stack?

When creating a new node.js process programmatically by forking a process or spawning a new child process; does the new process or child process get it's own separate call stack?

Show Detail

Best method for Node.JS forking?

I'm writing a trajectory predictor in Node.JS. You may think it's a funny language to write one in, but it's actually working great. Now, I want to be able to start the predictor from a web interfa...

Show Detail

Forking Django DB connections

I have an application which receives data over a TCP connection and writes it to a postgres database. I then use a django web front end to provide a gui to this data. Since django provides useful

Show Detail

What causes ActiveRecord breaking Postgres connection after forking?

I have a Rake task in a Rails 4.2 project that uses fork. My problem is that after the forked process has finished (i.e. after Process.wait) I get the following Postgres error when I try to access ...

Show Detail

Giving Each Child Process a Unique ID with Forking

To learn socket programming with TCP, I'm making a simple server and client. The client will send chunks of a file and the server will read them and write to a file. Client and server work properly

Show Detail

PHP forking and mysql database connection problem

I am now trying to do forking in php. I would like to do some query and update in child process.. the problem is that whenever a child process finish, it close the connection which makes the other

Show Detail

forking a process within a django view

I have a webservice that initiates a process that can take up to a minute. I want to return a 204 that effectively says, "I have successfully gotten your request," but run the slow process in the

Show Detail

Linux - Is there a way to call to system call (bash scripts) without forking a new process?

In my c++ apps, I need to run several bash scripts. (sometimes regular system calls, i.e "shutdown" , "rm"). When using the "system" call, it forks a new process. Is there a way to call "system&quo

Show Detail

Is there a way to run JMH without forking a new java process?

I'm working on a spring project. It has to perform several DB operations (in several methods) while running. I want to measure the exact execution time of some methods in my project(while running)....

Show Detail