@@ -164,8 +164,15 @@ const {
164
164
} = require ( 'internal/perf/observe' ) ;
165
165
const { getDefaultHighWaterMark } = require ( 'internal/streams/state' ) ;
166
166
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 ;
169
176
}
170
177
171
178
function createHandle ( fd , is_server ) {
@@ -1835,12 +1842,12 @@ function createServerHandle(address, port, addressType, fd, flags) {
1835
1842
if ( err ) {
1836
1843
handle . close ( ) ;
1837
1844
// Fallback to ipv4
1838
- return createServerHandle ( DEFAULT_IPV4_ADDR , port ) ;
1845
+ return createServerHandle ( DEFAULT_IPV4_ADDR , port , undefined , undefined , flags ) ;
1839
1846
}
1840
1847
} else if ( addressType === 6 ) {
1841
1848
err = handle . bind6 ( address , port , flags ) ;
1842
1849
} else {
1843
- err = handle . bind ( address , port ) ;
1850
+ err = handle . bind ( address , port , flags ) ;
1844
1851
}
1845
1852
}
1846
1853
@@ -2024,7 +2031,7 @@ Server.prototype.listen = function(...args) {
2024
2031
toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
2025
2032
2026
2033
options = options . _handle || options . handle || options ;
2027
- const flags = getFlags ( options . ipv6Only ) ;
2034
+ const flags = getFlags ( options ) ;
2028
2035
// Refresh the id to make the previous call invalid
2029
2036
this . _listeningId ++ ;
2030
2037
// (handle[, backlog][, cb]) where handle is an object with a handle
@@ -2057,14 +2064,17 @@ Server.prototype.listen = function(...args) {
2057
2064
if ( typeof options . port === 'number' || typeof options . port === 'string' ) {
2058
2065
validatePort ( options . port , 'options.port' ) ;
2059
2066
backlog = options . backlog || backlogFromArgs ;
2067
+ if ( options . reusePort === true ) {
2068
+ options . exclusive = true ;
2069
+ }
2060
2070
// start TCP server listening on host:port
2061
2071
if ( options . host ) {
2062
2072
lookupAndListen ( this , options . port | 0 , options . host , backlog ,
2063
2073
options . exclusive , flags ) ;
2064
2074
} else { // Undefined host, listens on unspecified address
2065
2075
// Default addressType 4 will be used to search for primary server
2066
2076
listenInCluster ( this , null , options . port | 0 , 4 ,
2067
- backlog , undefined , options . exclusive ) ;
2077
+ backlog , undefined , options . exclusive , flags ) ;
2068
2078
}
2069
2079
return this ;
2070
2080
}
0 commit comments