当前位置:首页 > 服务器资讯

如何让Nodejs服务器优雅地退出

2020-11-10 22:41:12 作者: 来源: 阅读:251 评论:0

简介 即将开播:4月29日,民生银行郭庆谈商业银行金融科技赋能的探索与实践--> 本文转载自微信公众号「编程杂技」,作者theanarkh。转载本文请联系编程杂技公众号。假设我们启动了一个服务器,接收到了一些客户端的请求,这时候,如果我们想修改一个代码发布,需要重启服务器,怎么......

即将开播:4月29日,民生银行郭庆谈商业银行金融科技赋能的探索与实践

-->

本文转载自微信公众号「编程杂技」,作者theanarkh。转载本文请联系编程杂技公众号。 

假设我们启动了一个服务器,接收到了一些客户端的请求,这时候,如果我们想修改一个代码发布,需要重启服务器,怎么办?假设我们有以下代码。

server.js

  1. const net = require('net'); 
  2. const server = net.createServer().listen(80); 

client.js

  1. const net = require('net'); 
  2. net.connect({port:80}) 

如果我们直接杀死进程,那么存量的请求就会无法正常被处理。这会影响我们的服务质量。本文介绍如何使nodejs在重启时优雅地退出,所谓优雅,即让nodejs进程处理完存量请求后再退出。这关键的地方在于nodejs提供的api server.close()。我们看一下这api的介绍。

  1. 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. 

当我们使用close关闭一个server时,server会等所有的连接关闭后才会触发close事件。我们看一下源码。

  1. Server.prototype.close = function(cb) { 
  2.   // 触发回调 
  3.   if (typeof cb === 'function') { 
  4.     if (!this._handle) { 
  5.       this.once('close'function close() { 
  6.         cb(new errors.Error('ERR_SERVER_NOT_RUNNING')); 
  7.       }); 
  8.     } else { 
  9.       this.once('close', cb); 
  10.     } 
  11.   } 
  12.   // 关闭底层资源 
  13.   if (this._handle) { 
  14.     this._handle.close(); 
  15.     this._handle = null
  16.   } 
  17.   // 判断是否需要立刻触发close事件 
  18.   this._emitCloseIfDrained(); 
  19.   return this; 
  20. }; 
  21.  
  22. // server下的连接都close后触发server的close事件 
  23. Server.prototype._emitCloseIfDrained = function() { 
  24.   // 还有连接则先不处理 
  25.   if (this._handle || this._connections) { 
  26.      return
  27.   } 
  28.  
  29.   const asyncId = this._handle ? this[async_id_symbol] : null
  30.   nextTick(asyncId, emitCloseNT, this); 
  31. }; 
  32.  
  33. Socket.prototype._destroy = function(exception, cb) { 
  34.   ... 
  35.   // socket所属的server 
  36.   if (this._server) { 
  37.     // server下的连接数减一 
  38.     this._server._connections--; 
  39.     /* 
  40.       是否需要触发server的close事件, 
  41.       当所有的连接(socket)都关闭时才触发server的是close事件 
  42.     */ 
  43.     if (this._server._emitCloseIfDrained) { 
  44.       this._server._emitCloseIfDrained(); 
  45.     } 
  46.   } 
  47. }; 

从源码中我们看到,nodejs会先关闭server对应的handle,所以server不会再接收新的请求了。但是server并没有触发close事件,而是等到所有连接断开后才触发close事件,这个通知机制给了我们一些思路。我们可以监听server的close事件,等到触发close事件后才退出进程。

  1. const net = require('net'); 
  2. const server = net.createServer().listen(80); 
  3. server.on('close', () => { 
  4.   process.exit(); 
  5. }); 
  6. // 防止进程提前挂掉 
  7. process.on('uncaughtException', () => { 
  8.  
  9. }); 
  10. process.on('SIGINT'function() { 
  11.   server.close(); 
  12. }) 

我们首先监听SIGINT信号,当我们使用SIGINT信号杀死进程时,首先调用server.close,等到所有的连接断开,触发close时候时,再退出进程。我们首先开启服务器,然后开启两个客户端。接着按下ctrl+c,我们发现这时候服务器不会退出,然后我们关闭两个客户端,这时候server就会优雅地退出。


标签:server  close  我们  触发  事件  

相关评论

本栏推荐