@@ -164,8 +164,15 @@ const {
164164} = require ( 'internal/perf/observe' ) ;
165165const { getDefaultHighWaterMark } = require ( 'internal/streams/state' ) ;
166166
167- function getFlags ( ipv6Only ) {
168- return ipv6Only === true ? TCPConstants . UV_TCP_IPV6ONLY : 0 ;
167+ function getFlags ( options ) {
168+ let flags = 0 ;
169+ if ( options . ipv6Only === true ) {
170+ flags |= TCPConstants . UV_TCP_IPV6ONLY ;
171+ }
172+ if ( options . reusePort === true ) {
173+ flags |= TCPConstants . UV_TCP_REUSEPORT ;
174+ }
175+ return flags ;
169176}
170177
171178function createHandle ( fd , is_server ) {
@@ -1833,12 +1840,12 @@ function createServerHandle(address, port, addressType, fd, flags) {
18331840 if ( err ) {
18341841 handle . close ( ) ;
18351842 // Fallback to ipv4
1836- return createServerHandle ( DEFAULT_IPV4_ADDR , port ) ;
1843+ return createServerHandle ( DEFAULT_IPV4_ADDR , port , undefined , undefined , flags ) ;
18371844 }
18381845 } else if ( addressType === 6 ) {
18391846 err = handle . bind6 ( address , port , flags ) ;
18401847 } else {
1841- err = handle . bind ( address , port ) ;
1848+ err = handle . bind ( address , port , flags ) ;
18421849 }
18431850 }
18441851
@@ -2022,7 +2029,7 @@ Server.prototype.listen = function(...args) {
20222029 toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
20232030
20242031 options = options . _handle || options . handle || options ;
2025- const flags = getFlags ( options . ipv6Only ) ;
2032+ const flags = getFlags ( options ) ;
20262033 // Refresh the id to make the previous call invalid
20272034 this . _listeningId ++ ;
20282035 // (handle[, backlog][, cb]) where handle is an object with a handle
@@ -2055,14 +2062,17 @@ Server.prototype.listen = function(...args) {
20552062 if ( typeof options . port === 'number' || typeof options . port === 'string' ) {
20562063 validatePort ( options . port , 'options.port' ) ;
20572064 backlog = options . backlog || backlogFromArgs ;
2065+ if ( options . reusePort === true ) {
2066+ options . exclusive = true ;
2067+ }
20582068 // start TCP server listening on host:port
20592069 if ( options . host ) {
20602070 lookupAndListen ( this , options . port | 0 , options . host , backlog ,
20612071 options . exclusive , flags ) ;
20622072 } else { // Undefined host, listens on unspecified address
20632073 // Default addressType 4 will be used to search for primary server
20642074 listenInCluster ( this , null , options . port | 0 , 4 ,
2065- backlog , undefined , options . exclusive ) ;
2075+ backlog , undefined , options . exclusive , flags ) ;
20662076 }
20672077 return this ;
20682078 }
0 commit comments