节点中入站和出站套接字之间的差异



我正在编写一个函数,以确保节点实例被优雅地关闭。为此,我确保unref()所有套接字。这就是我正在做的:

       function tidyUp(){
          console.log("Closing the server...");
          server.close();
          console.log("Ordering a hotplate shutdown...");
          process.emit( 'hotplateShutdown');
          // This will give time to server.close() to actually work.. 
          setTimeout( function() {
            console.log("Calling unref() for all Sockets and for the Server:");
            wtf.dump();
            var handles = Array.prototype.slice.call( process._getActiveHandles() );
            handles.forEach( ( h, i ) => {
              var name = h.constructor.name;
              if( h.unref && typeof h.unref == 'function' & ( name == 'Socket' || name == 'Server' ) ){
                console.log("Unreffing:", i );
                h.unref();
              }
            });
            console.log("After unreffing:");
            wtf.dump();
            setTimeout( function(){
              console.log("This process should soon close");
              console.log("Here is the event queue keeping it alive:");
              wtf.dump( true );
            }, 1000);
          }, 1000 );
        };

我很担心,因为服务器也发送电子邮件,我想绝对确保发送的任何内容确实被发送。基本上:

"给一个Socket对象,你如何判断它是一个入站套接字(一个接收连接,因为节点的HTTP服务器会打开(还是一个出站套接字(一个由nodemailer打开以发送电子邮件(。">

我将要unref()所有入站套接字,并让出站套接字保持平静,直到发送完所有电子邮件。

提示?

我最终跟踪了打开的入站套接字:

      var server = http.createServer(app);
        // Keep track of all inbound sockets. They will be
        // unref()ed at cleanup time
        var inboundSockets = {};
        var socketId = 0;
        server.on('connection',function( socket ){
          socket.__id = socketId ++;
          inboundSockets[ socket.__id ] = socket;
          socket.on('close',function(){
            delete inboundSockets[ socket.__id ];
          });
        });
        process.on('uncaughtException', function (err) {
          console.error( "Error caught: ");
          console.error(err);
          tidyUp();
        })

        process.on('SIGTERM', function(){
          console.log("TERMINATING THIS INSTANCE!");
          tidyUp();
        });

        function tidyUp(){
          console.log("Closing the server...");
          server.close();
         // The next line is important AS IS as it will tell naps that the
          // server is no longer functional
          console.log("THE SERVER HAS STOPPED");
          console.log("Ordering a hotplate shutdown...");
          process.emit( 'hotplateShutdown');
          // This will give time to server.close() to actually work.. 
          setTimeout( function() {
            console.log("Calling unref() for inbound sockets:");
            Object.keys( inboundSockets ).forEach( ( __id ) => {
              var s = inboundSockets[ __id ];
              console.log("Unreffing inbound socket:", s.__id );
              s.unref();
            });
            db.unref();
            console.log("After unreffing:");
            wtf.dump();
            setTimeout( function(){
              console.log("This process should soon close");
              console.log("Here is the event queue keeping it alive:");
              wtf.dump( true );
            }, 5000);
          }, 1000 );
        };

请注意,我在创建时为套接字分配了一个 ID(似乎没有明确的通用方法来获取唯一 ID(,然后在服务器出现故障时取消引用它们......

最新更新