Skip to content

Commit

Permalink
Limit requests per connection
Browse files Browse the repository at this point in the history
  • Loading branch information
fatal10110 committed Sep 11, 2021
1 parent 3e8eda2 commit 2b5d67f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function OutgoingMessage() {
this._header = null;
this[kOutHeaders] = null;

this._maxRequestsPerSocket = null;
this._keepAliveTimeout = 0;

this._onPendingData = nop;
Expand Down Expand Up @@ -448,9 +449,23 @@ function _storeHeader(firstLine, headers) {
(state.contLen || this.useChunkedEncodingByDefault || this.agent);
if (shouldSendKeepAlive) {
header += 'Connection: keep-alive' + CRLF;
if (this._keepAliveTimeout && this._defaultKeepAlive) {
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
header += `Keep-Alive: timeout=${timeoutSeconds}${CRLF}`;

if (this._defaultKeepAlive) {
let keepAliveParameters = '';

if (this._keepAliveTimeout) {
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
keepAliveParameters += `timeout=${timeoutSeconds}`;
}

if (this._maxRequestsPerSocket) {
if (keepAliveParameters.length > 0) keepAliveParameters += ', ';
keepAliveParameters += `max=${this._maxRequestsPerSocket}`;
}

if (keepAliveParameters.length > 0) {
header += `Keep-Alive: ${keepAliveParameters}${CRLF}`;
}
}
} else {
this._last = true;
Expand Down
13 changes: 13 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ function Server(options, requestListener) {
this.timeout = 0;
this.keepAliveTimeout = 5000;
this.maxHeadersCount = null;
this.maxRequestsPerSocket = null;
this.headersTimeout = 60 * 1000; // 60 seconds
this.requestTimeout = 0;
}
Expand Down Expand Up @@ -486,6 +487,7 @@ function connectionListenerInternal(server, socket) {
// need to pause TCP socket/HTTP parser, and wait until the data will be
// sent to the client.
outgoingData: 0,
requestsCount: 0,
keepAliveTimeoutSet: false
};
state.onData = socketOnData.bind(undefined,
Expand Down Expand Up @@ -876,6 +878,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {

const res = new server[kServerResponse](req);
res._keepAliveTimeout = server.keepAliveTimeout;
res._maxRequestsPerSocket = server.maxRequestsPerSocket;
res._onPendingData = updateOutgoingData.bind(undefined,
socket, state);

Expand Down Expand Up @@ -904,6 +907,16 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
resOnFinish.bind(undefined,
req, res, socket, state, server));

if (req.httpVersionMajor === 1 && req.httpVersionMinor === 1
&& typeof server.maxRequestsPerSocket === 'number'
&& server.maxRequestsPerSocket > ++state.requestsCount) {
res.shouldKeepAlive = false;
res.writeHead(503, {
'Connection': 'close'
});
res.end();
}

if (req.headers.expect !== undefined &&
(req.httpVersionMajor === 1 && req.httpVersionMinor === 1)) {
if (RegExpPrototypeTest(continueExpression, req.headers.expect)) {
Expand Down

0 comments on commit 2b5d67f

Please sign in to comment.