Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions src/js/node/_http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,24 @@ function onServerConnection(this: Server, socketHandle) {
// Already wrapped (shouldn't happen for a brand-new connection).
return;
}
// Like Node.js's net.Server onconnection: once maxConnections is reached,
// each further accepted connection is reported via 'drop' and destroyed
// before http's connectionListener sees it.
const maxConnections = this.maxConnections;
if (maxConnections != null && this[kTrackedConnections].size >= maxConnections) {
const remote = socketHandle.remoteAddress;
const local = socketHandle.localAddress;
this.emit("drop", {
localAddress: local?.address,
localPort: local?.port,
localFamily: local?.family,
remoteAddress: remote?.address,
remotePort: remote?.port,
remoteFamily: remote?.family,
});
socketHandle.close();
return;
Comment thread
robobun marked this conversation as resolved.
Resolved
}
const isTLS = !!this[tlsSymbol];
const socket = new NodeHTTPServerSocket(this, socketHandle, isTLS);
// Node's connectionListener attaches the HTTPParser (socket.parser) before
Expand Down
79 changes: 79 additions & 0 deletions test/js/node/http/node-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4054,3 +4054,82 @@ it("OutgoingMessage outputData is per-instance and _flushOutput is defined", ()
c.outputData.push({ data: "y", encoding: "utf8", callback: null });
expect(d.outputData.length).toBe(0);
});

it("http.Server maxConnections destroys the excess and emits 'drop' like net.Server", async () => {
let served = 0;
const drops: any[] = [];
const server = createServer((_req, res) => {
served++;
res.end("ok");
});
server.maxConnections = 2;
Comment thread
robobun marked this conversation as resolved.
Resolved
server.on("drop", data => drops.push(data));
await once(server.listen(0, "127.0.0.1"), "listening");
const port = (server.address() as AddressInfo).port;
const clients: import("node:net").Socket[] = [];

const openAndRequest = () =>
new Promise<import("node:net").Socket>((resolve, reject) => {
const sock = connect(port, "127.0.0.1");
clients.push(sock);
sock.on("error", () => {});
let body = "";
sock.setEncoding("utf8");
sock.on("data", c => body.endsWith("ok") || ((body += c).endsWith("ok") && resolve(sock)));
sock.once("close", () => reject(new Error(`closed before response, got: ${JSON.stringify(body)}`)));
sock.once("connect", () => sock.write("GET / HTTP/1.1\r\nHost: x\r\n\r\n"));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

try {
const s1 = await openAndRequest();
await openAndRequest();
expect(served).toBe(2);
expect(drops).toEqual([]);

// Connections 3 and 4 are over the limit: the server emits 'drop' and
// destroys them on accept; the client side observes the close, and no
// 'request' is dispatched.
const outcomes: string[] = [];
for (let i = 0; i < 2; i++) {
const sock = connect(port, "127.0.0.1");
clients.push(sock);
sock.on("error", () => {});
const outcome = new Promise<string>(r => {
sock.once("data", () => r("served"));
sock.once("close", () => r("dropped"));
});
await once(sock, "connect");
sock.write("GET / HTTP/1.1\r\nHost: x\r\n\r\n");
outcomes.push(await outcome);
sock.destroy();
}

expect({ served, drops: drops.length, outcomes }).toEqual({
served: 2,
drops: 2,
outcomes: ["dropped", "dropped"],
});
expect(drops[0]).toEqual({
localAddress: "127.0.0.1",
localPort: port,
localFamily: "IPv4",
remoteAddress: "127.0.0.1",
remotePort: expect.any(Number),
remoteFamily: "IPv4",
});
const connectionCount = await new Promise<number>(r => server.getConnections((_e, n) => r(n)));
expect(connectionCount).toBe(2);

// Freeing a slot lets the next connection through again.
s1.destroy();
while ((await new Promise<number>(r => server.getConnections((_e, n) => r(n)))) !== 1) {
await new Promise(r => setImmediate(r));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
await openAndRequest();
expect(served).toBe(3);
expect(drops.length).toBe(2);
} finally {
for (const s of clients) s.destroy();
await new Promise<void>(r => server.close(() => r()));
}
});
Loading