In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to let the Nodejs server exit gracefully, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Suppose we start a server and receive some requests from the client. What if we want to modify a code release and need to restart the server? Suppose we have the following code.
Server.js
Const net = require ('net'); const server = net.createServer () .listen (80)
Client.js
Const net = require ('net'); net.connect ({port:80})
If we kill the process directly, then the stock request will not be processed properly. This will affect the quality of our service. This article describes how to make nodejs exit gracefully when it is restarted, that is, letting the nodejs process exit after processing the inventory request. The key point is the api server.close () provided by nodejs. Let's take a look at the introduction of 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 when it was closed.
When we use close to close a server, server waits for all connections to close before triggering the close event. Let's look at the source code.
Server.prototype.close = function (cb) {/ / trigger callback if (typeof cb = 'function') {if (! this._handle) {this.once (' close', function close () {cb (new errors.Error ('ERR_SERVER_NOT_RUNNING'));});} else {this.once (' close', cb) }} / / close the underlying resource if (this._handle) {this._handle.close (); this._handle = null;} / / determine whether the close event this._emitCloseIfDrained (); return this;} needs to be triggered immediately / / the close event that triggers server after all connections under server are close Server.prototype._emitCloseIfDrained = function () {/ / if there is a connection, do not deal with if (this._handle | | this._connections) {return;} const asyncId = this._handle? This [async _ id_symbol]: null; nextTick (asyncId, emitCloseNT, this);}; Socket.prototype._destroy = function (exception, cb) {. / / the number of connections under server if (this._server) {/ / server to which socket belongs minus one this._server._connections-- / * whether the close event of server needs to be triggered. It is the close event that triggers server when all connections (socket) are closed * / if (this._server._emitCloseIfDrained) {this._server._emitCloseIfDrained ();}
We can see from the source code that nodejs will first close the handle corresponding to server, so server will not receive new requests. However, server does not trigger the close event, but waits until all connections are disconnected before triggering the close event. This notification mechanism gives us some ideas. We can listen to the close event of server and wait until the close event is triggered before exiting the process.
Const net = require ('net'); const server = net.createServer (). Listen (80); server.on (' close', () = > {process.exit ();}); / / prevent the process from hanging process.on early ('uncaughtException', () = > {}); process.on (' SIGINT', function () {server.close ();})
We first listen to the SIGINT signal, and when we use the SIGINT signal to kill the process, we first call server.close until all connections are disconnected and close is triggered before exiting the process. We first turn on the server, and then open two clients. Then press ctrl+c, we find that the server will not exit at this time, and then we close the two clients, and then server will exit gracefully.
The above is how to make the Nodejs server exit gracefully. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.