@@ -279,6 +279,8 @@ function initSocketHandle(self) {
279279const kBytesRead = Symbol ( 'kBytesRead' ) ;
280280const kBytesWritten = Symbol ( 'kBytesWritten' ) ;
281281const kSetNoDelay = Symbol ( 'kSetNoDelay' ) ;
282+ const kSetKeepAlive = Symbol ( 'kSetKeepAlive' ) ;
283+ const kSetKeepAliveInitialDelay = Symbol ( 'kSetKeepAliveInitialDelay' ) ;
282284
283285function Socket ( options ) {
284286if ( ! ( this instanceof Socket ) ) return new Socket ( options ) ;
@@ -297,6 +299,15 @@ function Socket(options) {
297299'is not supported'
298300) ;
299301}
302+ if ( typeof options ?. keepAliveInitialDelay !== 'undefined' ) {
303+ validateNumber (
304+ options ?. keepAliveInitialDelay , 'options.keepAliveInitialDelay'
305+ ) ;
306+
307+ if ( options . keepAliveInitialDelay < 0 ) {
308+ options . keepAliveInitialDelay = 0 ;
309+ }
310+ }
300311
301312this . connecting = false ;
302313// Problem with this is that users can supply their own handle, that may not
@@ -307,7 +318,6 @@ function Socket(options) {
307318this [ kHandle ] = null ;
308319this . _parent = null ;
309320this . _host = null ;
310- this [ kSetNoDelay ] = false ;
311321this [ kLastWriteQueueSize ] = 0 ;
312322this [ kTimeout ] = null ;
313323this [ kBuffer ] = null ;
@@ -381,6 +391,10 @@ function Socket(options) {
381391this [ kBufferCb ] = onread . callback ;
382392}
383393
394+ this [ kSetNoDelay ] = Boolean ( options . noDelay ) ;
395+ this [ kSetKeepAlive ] = Boolean ( options . keepAlive ) ;
396+ this [ kSetKeepAliveInitialDelay ] = ~ ~ ( options . keepAliveInitialDelay / 1000 ) ;
397+
384398// Shut down the socket when we're finished with it.
385399this . on ( 'end' , onReadableStreamEnd ) ;
386400
@@ -504,31 +518,38 @@ Socket.prototype._onTimeout = function() {
504518
505519
506520Socket . prototype . setNoDelay = function ( enable ) {
521+ // Backwards compatibility: assume true when `enable` is omitted
522+ enable = Boolean ( enable === undefined ? true : enable ) ;
523+
507524if ( ! this . _handle ) {
508- this . once ( 'connect' ,
509- enable ? this . setNoDelay : ( ) => this . setNoDelay ( enable ) ) ;
525+ this [ kSetNoDelay ] = enable ;
510526return this ;
511527}
512528
513- // Backwards compatibility: assume true when `enable` is omitted
514- const newValue = enable === undefined ? true : ! ! enable ;
515- if ( this . _handle . setNoDelay && newValue !== this [ kSetNoDelay ] ) {
516- this [ kSetNoDelay ] = newValue ;
517- this . _handle . setNoDelay ( newValue ) ;
529+ if ( this . _handle . setNoDelay && enable !== this [ kSetNoDelay ] ) {
530+ this [ kSetNoDelay ] = enable ;
531+ this . _handle . setNoDelay ( enable ) ;
518532}
519533
520534return this ;
521535} ;
522536
523537
524- Socket . prototype . setKeepAlive = function ( setting , msecs ) {
538+ Socket . prototype . setKeepAlive = function ( enable , initialDelayMsecs ) {
539+ enable = Boolean ( enable ) ;
540+ const initialDelay = ~ ~ ( initialDelayMsecs / 1000 ) ;
541+
525542if ( ! this . _handle ) {
526- this . once ( 'connect' , ( ) => this . setKeepAlive ( setting , msecs ) ) ;
543+ this [ kSetKeepAlive ] = enable ;
544+ this [ kSetKeepAliveInitialDelay ] = initialDelay ;
527545return this ;
528546}
529547
530- if ( this . _handle . setKeepAlive )
531- this . _handle . setKeepAlive ( setting , ~ ~ ( msecs / 1000 ) ) ;
548+ if ( this . _handle . setKeepAlive && enable !== this [ kSetKeepAlive ] ) {
549+ this [ kSetKeepAlive ] = enable ;
550+ this [ kSetKeepAliveInitialDelay ] = initialDelay ;
551+ this . _handle . setKeepAlive ( enable , initialDelay ) ;
552+ }
532553
533554return this ;
534555} ;
@@ -1141,6 +1162,14 @@ function afterConnect(status, handle, req, readable, writable) {
11411162}
11421163self . _unrefTimer ( ) ;
11431164
1165+ if ( self [ kSetNoDelay ] && self . _handle . setNoDelay ) {
1166+ self . _handle . setNoDelay ( true ) ;
1167+ }
1168+
1169+ if ( self [ kSetKeepAlive ] && self . _handle . setKeepAlive ) {
1170+ self . _handle . setKeepAlive ( true , self [ kSetKeepAliveInitialDelay ] ) ;
1171+ }
1172+
11441173self . emit ( 'connect' ) ;
11451174self . emit ( 'ready' ) ;
11461175
@@ -1204,6 +1233,15 @@ function Server(options, connectionListener) {
12041233} else {
12051234throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
12061235}
1236+ if ( typeof options . keepAliveInitialDelay !== 'undefined' ) {
1237+ validateNumber (
1238+ options . keepAliveInitialDelay , 'options.keepAliveInitialDelay'
1239+ ) ;
1240+
1241+ if ( options . keepAliveInitialDelay < 0 ) {
1242+ options . keepAliveInitialDelay = 0 ;
1243+ }
1244+ }
12071245
12081246this . _connections = 0 ;
12091247
@@ -1215,6 +1253,9 @@ function Server(options, connectionListener) {
12151253
12161254this . allowHalfOpen = options . allowHalfOpen || false ;
12171255this . pauseOnConnect = ! ! options . pauseOnConnect ;
1256+ this . noDelay = Boolean ( options . noDelay ) ;
1257+ this . keepAlive = Boolean ( options . keepAlive ) ;
1258+ this . keepAliveInitialDelay = ~ ~ ( options . keepAliveInitialDelay / 1000 ) ;
12181259}
12191260ObjectSetPrototypeOf ( Server . prototype , EventEmitter . prototype ) ;
12201261ObjectSetPrototypeOf ( Server , EventEmitter ) ;
@@ -1567,6 +1608,14 @@ function onconnection(err, clientHandle) {
15671608writable : true
15681609} ) ;
15691610
1611+ if ( self . noDelay && handle . setNoDelay ) {
1612+ handle . setNoDelay ( true ) ;
1613+ }
1614+
1615+ if ( self . keepAlive && self . setKeepAlive ) {
1616+ handle . setKeepAlive ( true , handle . keepAliveInitialDelay ) ;
1617+ }
1618+
15701619self . _connections ++ ;
15711620socket . server = self ;
15721621socket . _server = self ;
0 commit comments