feat: cap concurrent requests across runtimes
CI / dco (push) Has been skipped
CI / build (push) Successful in 28s
CI / runtimes (deno) (push) Successful in 19s
CI / runtimes (bun) (push) Successful in 59s
CI / runtimes (edge) (push) Successful in 11s
CI / dco (pull_request) Successful in 6s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 12s
CI / runtimes (deno) (pull_request) Successful in 21s

Signed-off-by: hgshreyas <hgshreyas.cs24@bmsce.ac.in>
This commit is contained in:
hgshreyas authored and Diego Resendez committed 2026-08-31 08:53:14 -05:00
1 parent 82e1678dea
commit ff94e55496
7 files changed
+430 -34

No files matched your search

+55 -11
View File
@@ -1,5 +1,6 @@
import type http from 'http';
import type https from 'https';
import { createRequestGate } from './request-gate';
import { renderError, type ErrorRequest } from '../transformers';
import type { Bus } from '../bus';
import { buildSecurityHeaders, resolveCors } from '../security';
@@ -7,7 +8,7 @@ import { readBody as readBodyBytes, deriveSecure, deriveIp } from './request';
import { pipeStream } from './stream';
import { attachWs } from './ws';
import { mergeInjectedHeaders } from './headers';
import { handle, correlateRequest } from './core';
import { handle, correlateRequest, type HandleResult, type Preflight } from './core';
import type { BodyFailure, BodyReader } from './body';
import type { RouteDef, WsRouteDef, MeshControl, HttpOptions } from './types';
@@ -87,6 +88,7 @@ function createRequestHandler(cfg: HandlerConfig) {
const { routes, bus, opts, trustProxy } = cfg;
// Built once per server, not once per request — see `BodyReader`.
const readBody: BodyReader = (source, limit) => readRequestBytes(source, limit, opts);
const gate = createRequestGate(opts?.limits?.maxConcurrentRequests);
return async (req: http.IncomingMessage, res: http.ServerResponse): Promise<void> => {
// Install the security-header patch BEFORE any routing/response so every path
@@ -101,16 +103,58 @@ function createRequestHandler(cfg: HandlerConfig) {
// by reference at writeHead time, so keys added here still land on every response.
if (opts?.cors) Object.assign(injected, resolveCors(opts.cors, req));
const result = await handle(routes, opts, {
...correlation,
method: req.method ?? 'GET',
url: req.url ?? '/',
headers: req.headers,
readBody,
source: req,
secure,
ip: deriveIp(req, trustProxy),
});
let result: HandleResult | Preflight;
let acquired = false;
const watchingEnd = bus?.hasListeners('request:end') ?? false;
const startedAt = watchingEnd ? performance.now() : 0;
try {
if (!gate.acquire()) {
if (watchingEnd) {
const method = req.method ?? 'GET';
const url = req.url ?? '/';
bus!.emit('request:end', {
name: `${method} ${url}`,
method,
status: 503,
durationMs: performance.now() - startedAt,
requestId: correlation.requestId,
traceId: correlation.traceId,
});
}
res.writeHead(503, {
'content-type': 'application/json',
'retry-after': '1',
connection: 'close',
});
res.end(JSON.stringify({ error: 'Service Unavailable' }));
return;
}
acquired = true;
res.once('close', () => {
if (!acquired) return;
acquired = false;
gate.release();
});
result = await handle(routes, opts, {
...correlation,
method: req.method ?? 'GET',
url: req.url ?? '/',
headers: req.headers,
readBody,
source: req,
secure,
ip: deriveIp(req, trustProxy),
});
} finally {
if (acquired) {
acquired = false;
gate.release();
}
}
if ('preflight' in result) {
res.writeHead(204, result.preflight);