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

http: add drop request event for http server #43806

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
14 changes: 14 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,20 @@ This event is guaranteed to be passed an instance of the {net.Socket} class,
a subclass of {stream.Duplex}, unless the user specifies a socket
type other than {net.Socket}.

### Event: `'dropRequest'`

<!-- YAML
added: REPLACEME
-->

* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
the [`'request'`][] event
* `socket` {stream.Duplex} Network socket between the server and client

When the number of requests on a socket reaches the threshold of
`server.maxRequestsPerSocket`, the server will drop new requests
and emit `'dropRequest'` event instead, then send `503` to client.

### Event: `'request'`

<!-- YAML
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
if (isRequestsLimitSet &&
(server.maxRequestsPerSocket < state.requestsCount)) {
handled = true;

server.emit('dropRequest', req, socket);
res.writeHead(503);
res.end();
} else if (req.headers.expect !== undefined) {
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-http-keep-alive-drop-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');
const http = require('http');
const net = require('net');
const assert = require('assert');

function request(socket) {
socket.write('GET / HTTP/1.1\r\n');
socket.write('Connection: keep-alive\r\n');
socket.write('\r\n\r\n');
}

const server = http.createServer(common.mustCall((req, res) => {
theanarkh marked this conversation as resolved.
Show resolved Hide resolved
res.end('ok');
}));

server.on('dropRequest', common.mustCall((request, socket) => {
assert.strictEqual(request instanceof http.IncomingMessage, true);
assert.strictEqual(socket instanceof net.Socket, true);
server.close();
}));

server.listen(0, common.mustCall(() => {
const socket = net.connect(server.address().port);
socket.on('connect', common.mustCall(() => {
request(socket);
request(socket);
}));
socket.on('data', common.mustCallAtLeast());
socket.on('close', common.mustCall());
}));

server.maxRequestsPerSocket = 1;
51 changes: 51 additions & 0 deletions test/parallel/test-https-keep-alive-drop-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const common = require('../common');

if (!common.hasCrypto)
common.skip('missing crypto');

const https = require('https');
const http = require('http');
const net = require('net');
const assert = require('assert');
const tls = require('tls');
const { readKey } = require('../common/fixtures');

function request(socket) {
socket.write('GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n\r\n');
}

// https options
const httpsOptions = {
key: readKey('agent1-key.pem'),
cert: readKey('agent1-cert.pem')
};

const server = https.createServer(httpsOptions, common.mustCall((req, res) => {
res.end('ok');
}));

server.on('dropRequest', common.mustCall((request, socket) => {
assert.strictEqual(request instanceof http.IncomingMessage, true);
assert.strictEqual(socket instanceof net.Socket, true);
server.close();
}));

server.listen(0, common.mustCall(() => {
const socket = tls.connect(
server.address().port,
{
rejectUnauthorized: false
},
common.mustCall(() => {
request(socket);
request(socket);
socket.on('error', common.mustNotCall());
socket.on('data', common.mustCallAtLeast());
socket.on('close', common.mustCall());
})
);
}));

server.maxRequestsPerSocket = 1;