Node.js process smoothing profound analysis

Use node.js to build HTTP Server is a matter of Siki. In the production environment, the Node process smoothes directly related to the reliability of the service, and its importance cannot be ignored. Since it is smoothed, it involves the replacement transition of the new-term process:

  • First, ensure the new process smooth into the field
  • Second, ensure the old process smooth off the field

This article mainly talks about how to ensure the old process smoothly from the new and old processes. What kind of arrival is only smooth?

How to define the smooth separation

As the time division point, we can divide the request into two categories: incremental request and stock request .

  • Before the process leaves, stop receiving new ( increment ) request
  • Before the process leaves, it is guaranteed unfinished ( stock ) normal response request

Therefore, to achieve both objectives, we considered a substantially smooth departure process. We need a mechanism before talking about how to do progress, we need a mechanism. This mechanism allows us to pay for the process, which involves the knowledge of inter-process communication (IPC), let’s briefly understand .


For UNIX or class UNIX systems, there are many ways to communicate between processes – signals (Signal). A sort of.

There are many types of signals, such as

Sigint, Sigterm , and Sigkill , etc. These signals depend specifically for different scenes, such as Sigkill is generally used to kill the process.

We can perform

kill -l on the command line, as shown below(The number represents Signal Number ):

$ KILL -L 1) SIGHUP 2) Sigint 3) Sigvent 4) Sigill 5) SigTrap 6 ) SIGABRT 7) SIGEMT 8) SIGFPE 9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU25) SIGXFSZ 27) SigvTalRM 27) SigProf 28) SigProf 28) SigInfo 30) SIGUSR1 31) SIGUSR1 31) SIGUSR2
   We can use the kill command to send a specified signal to the process: 
# Send SIGTERM signal (default, no specified signal type) gives the process $ KILL

# Send the sigint signal to the process, where

is specific process ID $ KILL -Int
# Send Sigkill signals to process $ kill -kill # or $ kill -9
    process can be made to the received signal Respond. For Node applications, the signal is sent to the Node process as an event, and the process receives Sigterm and SIGI.NT events have default callbacks, the official documentation is described:   'Sigterm' AND 'Sigint' Have Default Handlers on Non-Windows Platforms That Reset The Terminal Mode Before Exiting with Code 128 + Signal Number ITS DEFAULT Behavior Will Be Removed (Node.js Will No Longer EXIT).   This sentence is written very abstract, what is it? What do you mean? We take a simple Node application as an example. 
New file, type the following code, save it as server.js:

const http = request (‘http’); const server = http .createserver ((REQ, RES) => {setTimeout (() => {res.writehead (200, {‘content-type’: ‘text / plain’}); res. is (‘it works’);} , 5000);}); Server.Listen (9420);

Here, for the convenience of testing, each HTTP request received, waiting for 5 seconds, then response.

Perform
Node Server.js

start application. In order to send a signal to the application, we need to obtain the application ID, we can use the lsof command to view:

$ LSOF -I TCP: 9420command Pid User FD Type Device Size / Off Node NameNode 70826 Myunlessor 13u IPv6 0xD250033EEF8912EB 0T0 TCP *: 9420 (Listen)

 In fact, we can also get the process ID through console.log (Process.PID) in your code. Here is only one way to get a process in the case where you know the situation of the TCP port.   Subsequently, we initiate a request, before receiving the response (with 5 second waiting time), we send a SIGINT signal to the app. 
$ CURL HTTP: / / LOCALHOST: 9420 & $ KILL -INT 70826CURL: (52) EMPTY Reply from Server [1] + EXIT 52 CURL HTTP: // Localhost : 9420


As can be seen, the request cannot be received normally. That is to say, by default, when the Node app is received, the process will immediately kill the process, and the process has not been processed. Fortunately, we can manually monitor the Sigint event of the process, like this:

Process.on (‘sigint “, () => {// do sometying here}
   
If we don’t do anything in the incident callback, it means ignoring the signal, what should I do, like?No matter what happened.
So, what if I manually listen to SigkILL? Sorry, Sigkill cannot be listened, and the official documentation is as follows:


‘Sigkill’ Cannot Have a listener Installed, It will unconditionally Terminate Node.js on All Platforms.

This is reasonable, you know that Sigkill is used to kill the process, you can’t intervene it.
  Back to the above question, we can approximate the default callback of the Node application response to the SIGINT event is like this:  

Process.on ‘Sigint’, () => {process.exit (128 + 2 / * signal number * /);});

We can print exit code to verify:
  $ Node Server.js $ Echo $? 130  
With the signal, we can actively notify the process When I left, I would like to talk about how the process is smooth.
How to make progress smooth leaves

Based on the example, in the file server.js, supplementation as follows:


Process.on (‘sigint’, () => {server.close (err => {process.exit (Err? 1: 0);});});

This code is very simple, we rewrite the application to receive the default behavior of

sigint
event, no longer simply rude directly kill the process, but in SERVER.CLOSE
 Method callback callback  process.exit  method, then continue to test. 

$ LSOF -I TCP: 9420command Pid User FD Type Device Size / Off Node NameNode 75842 Myunlessor 13u IPv6 0xD250033EC7C9362B 0T0 TCP *: 9420 (Listen) $ CURL HTTP: / / localhost: 9420 & [1] 75878 $ KILL -2 75842 $ IT Works [1] + DONE CURL http: // localhost: 9420
Can be seen, application The
 stock  request is successfully responded before exiting (before the process leaves).  
We can also verify that the process is no longer receiving increment before leaving

$ CURL HTTP: / /127.0.0.1:9420CURL: (7) failed to connection should 127.0.0.1 port 9420: connection refuse
This is exactly what Server.Close does, the process is smooth It’s so simple to leave the field, the official document describes this API:

Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a ‘close’ event. The optional callback will be called once the ‘close’ event Occurs. Unlike That Event, IT Will Be Called with an Error As ITS ONLY Argument If The Server Was Not Open IT WAS Closed.
 Conclusion   
Process Flat Peak is just part of the Node process smoothes. In the production environment, the replacement of the new and old processes involves the consideration of all aspects of process load balancing, process life cycle management. Professional tools do professional things, PM2 is a good choice for Node processes.

The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment