Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTEGRATION [PR#1283 > development/8.1] bugfix: S3C-3388 network.http.Server.setKeepAliveTimeout() #1289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,16 @@ module.exports = {
's3:ObjectRemoved:DeleteMarkerCreated',
]),
notificationArnPrefix: 'arn:scality:bucketnotif',
// HTTP server keep-alive timeout is set to a higher value than
// client's free sockets timeout to avoid the risk of triggering
// ECONNRESET errors if the server closes the connection at the
// exact moment clients attempt to reuse an established connection
// for a new request.
//
// Note: the ability to close inactive connections on the client
// after httpClientFreeSocketsTimeout milliseconds requires the
// use of "agentkeepalive" module instead of the regular node.js
// http.Agent.
httpServerKeepAliveTimeout: 60000,
httpClientFreeSocketTimeout: 55000,
};
17 changes: 17 additions & 0 deletions lib/network/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Server {
this._address = checkSupportIPv6() ? '::' : '0.0.0.0';
this._server = null;
this._logger = logger;
this._keepAliveTimeout = null; // null: use default node.js value
}

/**
Expand All @@ -57,6 +58,19 @@ class Server {
return this;
}

/**
* Set the keep-alive timeout after which inactive client
* connections are automatically closed (default should be
* 5 seconds in node.js)
*
* @param {number} keepAliveTimeout - keep-alive timeout in milliseconds
* @return {Server} - returns this
*/
setKeepAliveTimeout(keepAliveTimeout) {
this._keepAliveTimeout = keepAliveTimeout;
return this;
}

/**
* Getter to access to the http/https server
*
Expand Down Expand Up @@ -403,6 +417,9 @@ class Server {
this._server = http.createServer(
(req, res) => this._onRequest(req, res));
}
if (this._keepAliveTimeout) {
this._server.keepAliveTimeout = this._keepAliveTimeout;
}

this._server.on('error', err => this._onError(err));
this._server.on('secureConnection',
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/network/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,39 @@ describe('network.Server: ', () => {
res.end('done');
}).start();
});

it('should automatically close idle connections with setKeepAliveTimeout()', done => {
const ws = new Server(3000, log);
ws.setKeepAliveTimeout(1000);
ws.onError(done).onListening(() => {
const options = {
hostname: '127.0.0.1',
port: 3000,
path: '/',
agent: new http.Agent({ keepAlive: true }),
};
const req = http.request(options, res => {
res.on('data', () => {});
res.on('end', () => {});
});
req.on('error', err => {
assert.ifError(err);
});
req.end();
}).onRequest((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end();
assert.strictEqual(ws._server._connections, 1);
setTimeout(() => {
// client connection should remain open after less than 1000ms
assert.strictEqual(ws._server._connections, 1);
setTimeout(() => {
// client connection should have been closed after more than 1000ms
assert.strictEqual(ws._server.connections, 0);
ws.stop();
ws.onStop(done);
}, 200);
}, 900);
}).start();
});
});