Sunday, 9 April 2017

Difference between inbound and outbound sockets in node

I am writing a function that will make sure a node instance is gracefully shut down. To do that, I make sure I unref() all sockets. This is what I am doing:

       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 );
        };

I am concerned because the server also sends email, and I want to make absolute sure that any that is being sent is indeed sent. Basically:

"Give na Socket object, how do you tell if it's an INBOUND socket (one receiving connections, as node's HTTP server would open) or an OUTBOUND socket (one open by nodemailer to send an email)."

I will want to unref all inbound sockets, and leave the outbound ones in peace, till all email has been sent.

Hints?



via Merc

No comments:

Post a Comment