@@ -279,6 +279,8 @@ function initSocketHandle(self) {
279
279
const kBytesRead = Symbol ( 'kBytesRead' ) ;
280
280
const kBytesWritten = Symbol ( 'kBytesWritten' ) ;
281
281
const kSetNoDelay = Symbol ( 'kSetNoDelay' ) ;
282
+ const kSetKeepAlive = Symbol ( 'kSetKeepAlive' ) ;
283
+ const kSetKeepAliveInitialDelay = Symbol ( 'kSetKeepAliveInitialDelay' ) ;
282
284
283
285
function Socket ( options ) {
284
286
if ( ! ( this instanceof Socket ) ) return new Socket ( options ) ;
@@ -297,6 +299,15 @@ function Socket(options) {
297
299
'is not supported'
298
300
) ;
299
301
}
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
+ }
300
311
301
312
this . connecting = false ;
302
313
// Problem with this is that users can supply their own handle, that may not
@@ -307,7 +318,6 @@ function Socket(options) {
307
318
this [ kHandle ] = null ;
308
319
this . _parent = null ;
309
320
this . _host = null ;
310
- this [ kSetNoDelay ] = false ;
311
321
this [ kLastWriteQueueSize ] = 0 ;
312
322
this [ kTimeout ] = null ;
313
323
this [ kBuffer ] = null ;
@@ -381,6 +391,10 @@ function Socket(options) {
381
391
this [ kBufferCb ] = onread . callback ;
382
392
}
383
393
394
+ this [ kSetNoDelay ] = Boolean ( options . noDelay ) ;
395
+ this [ kSetKeepAlive ] = Boolean ( options . keepAlive ) ;
396
+ this [ kSetKeepAliveInitialDelay ] = ~ ~ ( options . keepAliveInitialDelay / 1000 ) ;
397
+
384
398
// Shut down the socket when we're finished with it.
385
399
this . on ( 'end' , onReadableStreamEnd ) ;
386
400
@@ -520,14 +534,18 @@ Socket.prototype.setNoDelay = function(enable) {
520
534
} ;
521
535
522
536
523
- Socket . prototype . setKeepAlive = function ( setting , msecs ) {
537
+ Socket . prototype . setKeepAlive = function ( enable , initialDelayMsecs ) {
524
538
if ( ! this . _handle ) {
525
- this . once ( 'connect' , ( ) => this . setKeepAlive ( setting , msecs ) ) ;
539
+ this . once ( 'connect' , ( ) => this . setKeepAlive ( enable , initialDelayMsecs ) ) ;
526
540
return this ;
527
541
}
528
542
529
- if ( this . _handle . setKeepAlive )
530
- this . _handle . setKeepAlive ( setting , ~ ~ ( msecs / 1000 ) ) ;
543
+ if ( this . _handle . setKeepAlive && enable !== this [ kSetKeepAlive ] ) {
544
+ const initialDelay = ~ ~ ( initialDelayMsecs / 1000 ) ;
545
+ this [ kSetKeepAlive ] = enable ;
546
+ this [ kSetKeepAliveInitialDelay ] = initialDelay ;
547
+ this . _handle . setKeepAlive ( enable , initialDelay ) ;
548
+ }
531
549
532
550
return this ;
533
551
} ;
@@ -1140,6 +1158,14 @@ function afterConnect(status, handle, req, readable, writable) {
1140
1158
}
1141
1159
self . _unrefTimer ( ) ;
1142
1160
1161
+ if ( self [ kSetNoDelay ] && self . _handle . setNoDelay ) {
1162
+ self . _handle . setNoDelay ( true ) ;
1163
+ }
1164
+
1165
+ if ( self [ kSetKeepAlive ] && self . _handle . setKeepAlive ) {
1166
+ self . _handle . setKeepAlive ( true , self [ kSetKeepAliveInitialDelay ] ) ;
1167
+ }
1168
+
1143
1169
self . emit ( 'connect' ) ;
1144
1170
self . emit ( 'ready' ) ;
1145
1171
@@ -1203,6 +1229,15 @@ function Server(options, connectionListener) {
1203
1229
} else {
1204
1230
throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
1205
1231
}
1232
+ if ( typeof options . keepAliveInitialDelay !== 'undefined' ) {
1233
+ validateNumber (
1234
+ options . keepAliveInitialDelay , 'options.keepAliveInitialDelay'
1235
+ ) ;
1236
+
1237
+ if ( options . keepAliveInitialDelay < 0 ) {
1238
+ options . keepAliveInitialDelay = 0 ;
1239
+ }
1240
+ }
1206
1241
1207
1242
this . _connections = 0 ;
1208
1243
@@ -1214,6 +1249,9 @@ function Server(options, connectionListener) {
1214
1249
1215
1250
this . allowHalfOpen = options . allowHalfOpen || false ;
1216
1251
this . pauseOnConnect = ! ! options . pauseOnConnect ;
1252
+ this . noDelay = Boolean ( options . noDelay ) ;
1253
+ this . keepAlive = Boolean ( options . keepAlive ) ;
1254
+ this . keepAliveInitialDelay = ~ ~ ( options . keepAliveInitialDelay / 1000 ) ;
1217
1255
}
1218
1256
ObjectSetPrototypeOf ( Server . prototype , EventEmitter . prototype ) ;
1219
1257
ObjectSetPrototypeOf ( Server , EventEmitter ) ;
@@ -1565,6 +1603,14 @@ function onconnection(err, clientHandle) {
1565
1603
writable : true
1566
1604
} ) ;
1567
1605
1606
+ if ( self . noDelay && handle . setNoDelay ) {
1607
+ handle . setNoDelay ( true ) ;
1608
+ }
1609
+
1610
+ if ( self . keepAlive && self . setKeepAlive ) {
1611
+ handle . setKeepAlive ( true , handle . keepAliveInitialDelay ) ;
1612
+ }
1613
+
1568
1614
self . _connections ++ ;
1569
1615
socket . server = self ;
1570
1616
socket . _server = self ;
0 commit comments