2017-04-09 101 views
0

我在寫一個函數來確保節點實例正常關閉。 要做到這一點,我確保我所有的套接字都是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()所有入站套接字,並保持出站的和平,直到所有的電子郵件已發送。

提示?

+0

沒有區別。他們都是插座。你要記住哪個是哪個。 – EJP

+0

我想一個有源IP的主機自己的IP是一個「本地生成」的套接字......對吧? – Merc

+0

編號套接字沒有源IP地址。他們有本地和遠程IP地址。接受的aockets與出站連接的套接字無法區分。 – EJP

回答

0

我結束了保持入插座的軌道開:

 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),然後在服務器關閉時取消它們...

相關問題