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