@@ -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 ) {
@@ -1835,12 +1842,12 @@ function createServerHandle(address, port, addressType, fd, flags) {
18351842 if ( err ) {
18361843 handle . close ( ) ;
18371844 // Fallback to ipv4
1838- return createServerHandle ( DEFAULT_IPV4_ADDR , port ) ;
1845+ return createServerHandle ( DEFAULT_IPV4_ADDR , port , undefined , undefined , flags ) ;
18391846 }
18401847 } else if ( addressType === 6 ) {
18411848 err = handle . bind6 ( address , port , flags ) ;
18421849 } else {
1843- err = handle . bind ( address , port ) ;
1850+ err = handle . bind ( address , port , flags ) ;
18441851 }
18451852 }
18461853
@@ -2024,7 +2031,7 @@ Server.prototype.listen = function(...args) {
20242031 toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
20252032
20262033 options = options . _handle || options . handle || options ;
2027- const flags = getFlags ( options . ipv6Only ) ;
2034+ const flags = getFlags ( options ) ;
20282035 // Refresh the id to make the previous call invalid
20292036 this . _listeningId ++ ;
20302037 // (handle[, backlog][, cb]) where handle is an object with a handle
@@ -2057,14 +2064,17 @@ Server.prototype.listen = function(...args) {
20572064 if ( typeof options . port === 'number' || typeof options . port === 'string' ) {
20582065 validatePort ( options . port , 'options.port' ) ;
20592066 backlog = options . backlog || backlogFromArgs ;
2067+ if ( options . reusePort === true ) {
2068+ options . exclusive = true ;
2069+ }
20602070 // start TCP server listening on host:port
20612071 if ( options . host ) {
20622072 lookupAndListen ( this , options . port | 0 , options . host , backlog ,
20632073 options . exclusive , flags ) ;
20642074 } else { // Undefined host, listens on unspecified address
20652075 // Default addressType 4 will be used to search for primary server
20662076 listenInCluster ( this , null , options . port | 0 , 4 ,
2067- backlog , undefined , options . exclusive ) ;
2077+ backlog , undefined , options . exclusive , flags ) ;
20682078 }
20692079 return this ;
20702080 }
0 commit comments