@@ -25,6 +25,7 @@ const {
2525 ArrayIsArray,
2626 Error,
2727 MathMin,
28+ NumberIsFinite,
2829 ObjectKeys,
2930 ObjectSetPrototypeOf,
3031 ReflectApply,
@@ -185,8 +186,6 @@ const kConnections = Symbol('http.server.connections');
185186const kConnectionsCheckingInterval = Symbol ( 'http.server.connectionsCheckingInterval' ) ;
186187
187188const HTTP_SERVER_TRACE_EVENT_NAME = 'http.server.request' ;
188- // TODO(jazelly): make this configurable
189- const HTTP_SERVER_KEEP_ALIVE_TIMEOUT_BUFFER = 1000 ;
190189
191190class HTTPServerAsyncResource {
192191 constructor ( type , socket ) {
@@ -486,6 +485,14 @@ function storeHTTPOptions(options) {
486485 this . keepAliveTimeout = 5_000 ; // 5 seconds;
487486 }
488487
488+ const keepAliveTimeoutBuffer = options . keepAliveTimeoutBuffer ;
489+ if ( keepAliveTimeoutBuffer !== undefined ) {
490+ validateInteger ( keepAliveTimeoutBuffer , 'keepAliveTimeoutBuffer' , 0 ) ;
491+ this . keepAliveTimeoutBuffer = keepAliveTimeoutBuffer ;
492+ } else {
493+ this . keepAliveTimeoutBuffer = 1000 ;
494+ }
495+
489496 const connectionsCheckingInterval = options . connectionsCheckingInterval ;
490497 if ( connectionsCheckingInterval !== undefined ) {
491498 validateInteger ( connectionsCheckingInterval , 'connectionsCheckingInterval' , 0 ) ;
@@ -548,6 +555,13 @@ function Server(options, requestListener) {
548555 }
549556
550557 storeHTTPOptions . call ( this , options ) ;
558+
559+ // Optional buffer added to the keep-alive timeout when setting socket timeouts.
560+ // Helps reduce ECONNRESET errors from clients by extending the internal timeout.
561+ // Default is 1000ms if not specified.
562+ const buf = options . keepAliveTimeoutBuffer ;
563+ this . keepAliveTimeoutBuffer =
564+ ( typeof buf === 'number' && NumberIsFinite ( buf ) && buf >= 0 ) ? buf : 1000 ;
551565 net . Server . call (
552566 this ,
553567 { allowHalfOpen : true , noDelay : options . noDelay ?? true ,
@@ -1014,9 +1028,10 @@ function resOnFinish(req, res, socket, state, server) {
10141028 }
10151029 } else if ( state . outgoing . length === 0 ) {
10161030 if ( server . keepAliveTimeout && typeof socket . setTimeout === 'function' ) {
1017- // Increase the internal timeout wrt the advertised value to reduce
1031+ // Extend the internal timeout by the configured buffer to reduce
10181032 // the likelihood of ECONNRESET errors.
1019- socket . setTimeout ( server . keepAliveTimeout + HTTP_SERVER_KEEP_ALIVE_TIMEOUT_BUFFER ) ;
1033+ // This allows fine-tuning beyond the advertised keepAliveTimeout.
1034+ socket . setTimeout ( server . keepAliveTimeout + server . keepAliveTimeoutBuffer ) ;
10201035 state . keepAliveTimeoutSet = true ;
10211036 }
10221037 } else {
0 commit comments