Guest User

Untitled

a guest
Oct 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. /**
  2. * This demonstrates how server.listen with port set to zero
  3. * does not function properly in a cluster environment.
  4. *
  5. * EXPECTED RESULT: each worker's server starts listening on a
  6. * unique port as per documentation "A port value of zero will assign a random port."
  7. *
  8. * ACTUAL DEMONSTRATED RESULT: each worker's server starts listening
  9. * on the exact same port as every other worker.
  10. */
  11. "use strict";
  12.  
  13. var cluster = require('cluster')
  14. , net = require('net')
  15. , numForks = 5
  16. , i
  17. ;
  18.  
  19. if (cluster.isMaster) {
  20. // fork workers
  21. for (i = 0; i < numForks; ++i) {
  22. cluster.fork();
  23. }
  24. } else {
  25. // worker code - create variable scope with executed function
  26. (function() {
  27. var server = net.createServer();
  28. /**
  29. * server.listen(port, [host], [listeningListener])
  30. * Begin accepting connections on the specified port and host. If the host
  31. * is omitted, the server will accept connections directed to any IPv4 address
  32. * (INADDR_ANY). A port value of zero will assign a random port.
  33. */
  34. server.listen(0);
  35. server.on('listening', function() {
  36. var address = server.address()
  37. , port = address.port
  38. ;
  39.  
  40. console.log(port);
  41. });
  42. })();
  43. }
Add Comment
Please, Sign In to add comment