fix: warn when connection limit drops a socket
Signed-off-by: hgshreyas <hgshreyas.cs24@bmsce.ac.in>
This commit is contained in:
1 parent
4b04421ddd
commit
82e1678dea
4 files changed
+72
-2
No files matched your search
@@ -274,6 +274,7 @@ export function createApp(opts: {
|
||||
|
||||
const fetchOpts: HttpOptions = {
|
||||
bus,
|
||||
logger,
|
||||
limits: opts.limits,
|
||||
tls: opts.tls,
|
||||
trustProxy: opts.trustProxy,
|
||||
@@ -305,6 +306,7 @@ export function createApp(opts: {
|
||||
|
||||
server = createHttpServer(httpRoutes, wsRoutes, bus, meshControl, {
|
||||
bus,
|
||||
logger,
|
||||
limits: opts.limits,
|
||||
streams,
|
||||
tls: opts.tls,
|
||||
|
||||
+15
-1
@@ -53,7 +53,21 @@ export function createHttpServer(
|
||||
? nodeHttps.createServer(opts.tls as https.ServerOptions, handler)
|
||||
: nodeHttp.createServer(handler)) as unknown as http.Server;
|
||||
const maxConnections = opts?.limits?.maxConnections ?? 1000;
|
||||
if (maxConnections > 0) server.maxConnections = maxConnections;
|
||||
|
||||
if (maxConnections > 0) {
|
||||
server.maxConnections = maxConnections;
|
||||
let lastDropWarn = 0;
|
||||
server.on('drop', ({ remoteAddress, remotePort }) => {
|
||||
const now = Date.now();
|
||||
if (now - lastDropWarn < 60_000) return;
|
||||
lastDropWarn = now;
|
||||
opts?.logger?.warn(
|
||||
`maxConnections (${maxConnections}) reached \u2014 dropped connection from ${remoteAddress ?? 'unknown'}:${remotePort ?? 'unknown'}`,
|
||||
{ maxConnections, remoteAddress, remotePort },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
server.requestTimeout = opts?.limits?.requestTimeoutMs ?? 30_000;
|
||||
server.headersTimeout = opts?.limits?.headersTimeoutMs ?? 10_000;
|
||||
server.keepAliveTimeout = opts?.limits?.keepAliveTimeoutMs ?? 5_000;
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { TlsOptions, SecurityOptions, CorsOptions } from '../security';
|
||||
import type { WsRequest, WsSocket } from './ws-core';
|
||||
import type { StaticResolver } from '../views';
|
||||
import type { Bus } from '../bus';
|
||||
import type { Logger } from '../logger';
|
||||
|
||||
/** Per-request and per-server resource ceilings applied by the server. */
|
||||
export interface RequestLimits {
|
||||
@@ -24,6 +25,8 @@ export interface HttpOptions {
|
||||
* a separate channel for it.
|
||||
*/
|
||||
bus?: Bus;
|
||||
/** Where this server writes diagnostics. Optional because `createHttpServer` takes no options at all in tests. */
|
||||
logger?: Logger;
|
||||
limits?: RequestLimits;
|
||||
streams?: Set<() => void>;
|
||||
tls?: TlsOptions;
|
||||
|
||||
+52
-1
@@ -1,4 +1,6 @@
|
||||
import { describe, expect, it, test } from 'vitest';
|
||||
import { describe, expect, it, test, vi } from 'vitest';
|
||||
import * as net from 'node:net';
|
||||
import { once } from 'node:events';
|
||||
import { matchRoute, parseQuery, createHttpServer } from '../src/http';
|
||||
|
||||
const handler = async () => ({ status: 200, headers: {}, body: 'ok' });
|
||||
@@ -143,6 +145,55 @@ describe('request hardening', () => {
|
||||
server.close();
|
||||
});
|
||||
|
||||
it('warns when maxConnections drops a real connection and throttles repeated warnings', async () => {
|
||||
const logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
const server = createHttpServer([], [], undefined, undefined, {
|
||||
logger,
|
||||
limits: { maxConnections: 1 },
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === 'string') throw new Error('server did not bind to a TCP port');
|
||||
|
||||
const first = net.createConnection({ host: '127.0.0.1', port: address.port });
|
||||
await once(first, 'connect');
|
||||
|
||||
const waitForClose = (socket: net.Socket): Promise<void> =>
|
||||
new Promise((resolve) => {
|
||||
socket.on('error', () => undefined);
|
||||
socket.once('close', () => resolve());
|
||||
});
|
||||
|
||||
const second = net.createConnection({ host: '127.0.0.1', port: address.port });
|
||||
const third = net.createConnection({ host: '127.0.0.1', port: address.port });
|
||||
const secondClosed = waitForClose(second);
|
||||
const thirdClosed = waitForClose(third);
|
||||
|
||||
try {
|
||||
await Promise.all([secondClosed, thirdClosed]);
|
||||
|
||||
expect(logger.warn).toHaveBeenCalledOnce();
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
expect.stringContaining('maxConnections (1) reached \u2014 dropped connection from'),
|
||||
expect.objectContaining({
|
||||
maxConnections: 1,
|
||||
remoteAddress: '127.0.0.1',
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
first.destroy();
|
||||
second.destroy();
|
||||
third.destroy();
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()));
|
||||
}
|
||||
});
|
||||
|
||||
it('requestTimeout does NOT kill an in-flight SSE stream (streaming regression)', async () => {
|
||||
async function* feed() { for (let n = 1; n <= 3; n++) { yield { n }; await new Promise((r) => setTimeout(r, 80)); } }
|
||||
const server = createHttpServer(
|
||||
|
||||
Reference in new issue
Block a user