⬆️ ⬇️

Node.JS: Example of an HTTP server in prefork mode using Web Workers

As promised earlier, I am publishing source code that demonstrates how to build an HTTP server in prefork mode using Web Workers and the new net.Server.listenFD() API. I hope this code will be a good example of how easy it is to load multiple server cores by combining the transfer of file descriptors and Web Workers.



In master.js, we create a socket attached to port 8080 and generate 4 worker processes for processing requests. We send a message to each handler with the text that will need to be used for all responses to requests, as well as the file descriptor of our socket.



  1. var path = require ( 'path' ) ;
  2. var netBindings = process. binding ( 'net' ) ;
  3. var Worker = require ( 'webworker' ) . Worker ;
  4. var fd = netBindings. socket ( 'tcp4' ) ;
  5. netBindings. bind ( fd , 8080 ) ;
  6. netBindings. listen ( fd , 128 ) ;
  7. for ( var i = 0 ; i < 3 ; i ++ ) {
  8. var w = new Worker ( path. join ( __dirname , 'worker.js' ) ) ;
  9. w. postMessage ( { 'banner' : 'Hello, world!' } , fd ) ;
  10. }


')

In worker.js, we create an instance of the HTTP server, but do not call listen() . Instead, we expect to receive messages from the parent process. When an event is received, we retrieve the file descriptor from the message and use it to bind our http.Server instance to the socket. As soon as http.Server.listenFD() is called, this process will start processing requests.



  1. var assert = require ( 'assert' ) ;
  2. var http = require ( 'http' ) ;
  3. var banner = undefined ;
  4. var srv = http. createServer ( function ( req , resp ) {
  5. resp. writeHead ( 200 , { 'Content-Type' : 'text / plain' } ) ;
  6. resp. write ( banner + '(pid' + process. pid + ') \ n ' ) ;
  7. resp. end ( ) ;
  8. } ) ;
  9. onmessage = function ( msg ) {
  10. assert. ok ( msg. fd && msg. fd > 0 ) ;
  11. banner = msg. data . banner ;
  12. srv. listenFD ( msg. fd ) ;
  13. } ;




When we run master.js, we can use curl to verify that requests have been processed by different processes.



  1. % curl 'http: // localhost: 8080'
  2. Hello, world! (pid 27727)
  3. % curl 'http: // localhost: 8080'
  4. Hello, world! (pid 27728)
  5. % curl 'http: // localhost: 8080'
  6. Hello, world! (pid 27729)
  7. % curl 'http: // localhost: 8080'
  8. Hello, world! (pid 27727)




Very simple.

Source: https://habr.com/ru/post/95972/



All Articles