Skip to content

Commit 539f472

Browse files
Allow configuration of keepAliveTimeout (#47)
- Support setting keepAliveTimeout on servers
1 parent e84d869 commit 539f472

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 3.3.0
4+
5+
- Support `keepAliveTimeout`
6+
37
## 3.2.1
48

59
- [#50] Fix bug where cert file reading errors do not surface

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ a node-style callback. The config object must have at minimum an `http` or
1212
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1313
| `handler` | Request handler to be used for any server, unless overridden specifically with `http.handler` or `https.handler`. |
1414
| `timeout` | Socket timeout in milliseconds for any server, unless overridden with `http.timeout` or `https.timeout`. Defaults to the node default of 2 minutes. |
15+
| `keepAliveTimeout` | Milliseconds of activity before sockets are destroyed. Defaults to the node default value (currently 5 seconds). |
1516
| `http` | Optional. If present, an HTTP server is started. This can be an object or a number. If it's a number, it's used as the TCP port for an HTTP server. You may also use an Array to start multiple servers. |
1617
| `http.port` | TCP port for the HTTP server. Defaults to `80`. |
1718
| `http.host` | The address the HTTP server is bound to. Defaults to `::` or `0.0.0.0`. |
1819
| `http.timeout` | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used. |
20+
| `http.keepAliveTimeout` | Overrides the top-level keepAliveTimeout setting if specified. |
1921
| `http.handler` | Handler for HTTP requests. If you want to share a handler with all servers, use a top-level `handler` config property instead. |
2022
| `https` | Optional object. If present, an HTTPS server is started. You may start multiple HTTPS servers by passing an array of objects |
2123
| `https.port` | TCP port for the HTTPS server. Defaults to `443`. |
2224
| `https.host` | The address the HTTPS server is bound to. Defaults to `::` or `0.0.0.0`. |
2325
| `https.timeout` | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used. |
26+
| `https.keepAliveTimeout` | Overrides the top-level keepAliveTimeout setting if specified. |
2427
| `https.ciphers` | Defaults to a [default cipher suite](#note-on-security). To customize, either supply a colon-separated string or array of strings for the ciphers you want the server to support. |
2528
| `https.honorCipherOrder` | If true, prefer the server's specified cipher order instead of the client's. Defaults to `false`. |
2629
| `https.root` | Root directory for certificate/key files. See [Certificate normalization](#certificate-normalization) for more details. |

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ function normalizeHttpOptions(httpConfig, baseConfig) {
116116
host: httpConfig.host || baseConfig.host,
117117
port: +port,
118118
handler: httpConfig.handler || baseConfig.handler,
119-
timeout: httpConfig.timeout || baseConfig.timeout
119+
timeout: httpConfig.timeout || baseConfig.timeout,
120+
keepAliveTimeout: httpConfig.keepAliveTimeout || baseConfig.keepAliveTimeout
120121
};
121122

122123
if (!http.handler) {
@@ -138,7 +139,8 @@ function normalizeHttpsOptions(httpsConfig, baseConfig) {
138139
host: httpsConfig.host || baseConfig.host,
139140
port: +('port' in httpsConfig ? httpsConfig.port : 443),
140141
handler: httpsConfig.handler || baseConfig.handler,
141-
timeout: httpsConfig.timeout || baseConfig.timeout
142+
timeout: httpsConfig.timeout || baseConfig.timeout,
143+
keepAliveTimeout: httpsConfig.keepAliveTimeout || baseConfig.keepAliveTimeout
142144
};
143145

144146
if (!https.handler) {
@@ -295,10 +297,9 @@ async function createHttp(httpConfig, log) {
295297

296298
const
297299
server = require('http').createServer(httpConfig.handler),
298-
timeout = httpConfig.timeout,
299300
port = httpConfig.port;
300301

301-
if (typeof timeout === 'number') server.setTimeout(timeout);
302+
commonPostCreateSetup(httpConfig, server);
302303

303304
const args = [server, port];
304305
if (httpConfig.host) {
@@ -363,8 +364,7 @@ async function createHttps(ssl, log, h2) {
363364
? require('http2').createSecureServer(finalHttpsOptions, ssl.handler)
364365
: require('https').createServer(finalHttpsOptions, ssl.handler);
365366

366-
const timeout = ssl.timeout;
367-
if (typeof timeout === 'number') server.setTimeout(timeout);
367+
commonPostCreateSetup(ssl, server);
368368
const args = [server, port];
369369
if (ssl.host) {
370370
args.push(ssl.host);
@@ -395,3 +395,12 @@ async function createMultiple(createFn, configArray, log) {
395395
return servers;
396396
}
397397
}
398+
399+
function commonPostCreateSetup({ timeout, keepAliveTimeout }, server) {
400+
if (typeof timeout === 'number') {
401+
server.setTimeout(timeout);
402+
}
403+
if (typeof keepAliveTimeout === 'number') {
404+
server.keepAliveTimeout = keepAliveTimeout;
405+
}
406+
}

test/create-servers-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ test('only http, timeout', function (t) {
173173
});
174174
});
175175

176+
test('keepAliveTimeout', function (t) {
177+
t.plan(1);
178+
const time = 3000000;
179+
createServers({
180+
log: console.log,
181+
keepAliveTimeout: time,
182+
http: 0,
183+
handler: fend
184+
}, function (err, servers) {
185+
t.equals(servers.http.keepAliveTimeout, time);
186+
servers.http.close();
187+
});
188+
});
189+
176190
test('only https', function (t) {
177191
t.plan(5);
178192
createServers({

0 commit comments

Comments
 (0)