When I recently updated one of my GitHub projects, I wanted to change the pattern of the original single process into a multi-process pattern.
The thing is, I made an HTTP server that supports dynamic scripting.
After a lot of various features have been completed, a script time-out problem has been encountered.
In my HTTP server, the page is divided into template and activity two parts, one for the view, and one for the module, which is similar to ASP.
However, because node. JS is a single process, I can't control the code in the activity (for user scripting).
So, if there is a dead loop in the activity, then the entire HTTP server will die, so I want to do a detection script timeout feature.
=================================== cut ==========================.
All right, get to the chase.
My idea is this, from the main process to accept the socket, then send to the child process, and then the sub-process to do related operations (including domain name analysis, post reading and other behaviors) after the user activity to execute the request. The word process sends a alive message to the main process at intervals, and if the child process does not send a alive message to the main process over a period of time, the main process will kill the child process, prevent the time-out script from continuing, and then city a process to replace the child process that was killed just now.
The idea is this, but there are some problems.
I thought this thing could be done easily, but after the main process sends the socket to the child process, the child process binds a data event on the socket. The problem is that the data event cannot be started in a subprocess, as in the case of a sub-process.
After the experiment, the situation is this, the socket is sent to the child process by the main process, the client sends the data has been sent to the main process, so there is no data in the child process is readable, natural like a law to start the data event.
After modification, I read a request data in the main process, and then even the socket with the first read data, sent to the child process.
After the main process sends the socket to the child process, the socket in the main process appears to be automatically closed. Well, it has no effect on me.
A short summary:
The client socket sends all of the requested data to the main process at one time, and all the content before the child process reads the socket has been sent to the main process.
After a process sends a socket to another process, the socket within the process is marked as closed.
I hope my little practice can help the comrades who read this article.
This article is from the "Malpower" blog, make sure to keep this source http://malpower.blog.51cto.com/9385371/1553156
Some rules for passing client sockets between node. JS processes under Windows