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
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:
1 parent
82e1678dea
commit
ff94e55496
7 files changed
+430
-34
No files matched your search
+150
-1
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { asReadableStream, toBodyInit } from '../src/http/web';
|
||||
import { Bus } from '../src/bus';
|
||||
import { asReadableStream, buildFetch, toBodyInit } from '../src/http/web';
|
||||
import { sseEncoder } from '../src/encoders';
|
||||
|
||||
async function* nums() { yield { n: 1 }; yield { n: 2 }; }
|
||||
@@ -46,4 +47,152 @@ describe('asReadableStream', () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
expect(returned).toBe(true);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('buildFetch request gate', () => {
|
||||
it('does not permanently exhaust the gate when CORS setup throws', async () => {
|
||||
let attempts = 0;
|
||||
|
||||
const fetchHandler = buildFetch(
|
||||
[
|
||||
{
|
||||
method: 'GET',
|
||||
pattern: '/ok',
|
||||
transport: 'buffer',
|
||||
handler: async () => ({ status: 200, headers: {}, body: 'ok' }),
|
||||
},
|
||||
],
|
||||
{
|
||||
limits: { maxConcurrentRequests: 2 },
|
||||
cors: {
|
||||
origins: () => {
|
||||
attempts++;
|
||||
if (attempts <= 2) throw new Error('transient CORS failure');
|
||||
return true;
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const request = () =>
|
||||
new Request('http://localhost/ok', {
|
||||
headers: { origin: 'https://example.com' },
|
||||
});
|
||||
|
||||
await expect(fetchHandler(request())).rejects.toThrow('transient CORS failure');
|
||||
await expect(fetchHandler(request())).rejects.toThrow('transient CORS failure');
|
||||
|
||||
const recovered = await fetchHandler(request());
|
||||
expect(recovered.status).toBe(200);
|
||||
});
|
||||
|
||||
it('rejects requests above maxConcurrentRequests and releases the slot afterwards', async () => {
|
||||
let release!: () => void;
|
||||
let started!: () => void;
|
||||
|
||||
const blocked = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const firstStarted = new Promise<void>((resolve) => {
|
||||
started = resolve;
|
||||
});
|
||||
|
||||
const bus = new Bus();
|
||||
const ended: Array<{ method?: string; status?: number; requestId?: string }> = [];
|
||||
bus.on('request:end', (event) => {
|
||||
ended.push(event);
|
||||
});
|
||||
|
||||
const fetchHandler = buildFetch(
|
||||
[
|
||||
{
|
||||
method: 'GET',
|
||||
pattern: '/slow',
|
||||
transport: 'buffer',
|
||||
handler: async () => {
|
||||
started();
|
||||
await blocked;
|
||||
return { status: 200, headers: {}, body: 'ok' };
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
bus,
|
||||
limits: { maxConcurrentRequests: 1 },
|
||||
cors: { origins: 'https://example.com' },
|
||||
},
|
||||
);
|
||||
|
||||
const first = fetchHandler(new Request('http://localhost/slow'));
|
||||
await firstStarted;
|
||||
|
||||
const rejected = await fetchHandler(
|
||||
new Request('http://localhost/slow', {
|
||||
headers: { origin: 'https://example.com' },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(rejected.status).toBe(503);
|
||||
expect(rejected.headers.get('content-type')).toBe('application/json');
|
||||
expect(rejected.headers.get('retry-after')).toBe('1');
|
||||
expect(rejected.headers.get('access-control-allow-origin')).toBe('https://example.com');
|
||||
expect(rejected.headers.get('x-content-type-options')).toBe('nosniff');
|
||||
expect(rejected.headers.get('x-frame-options')).toBe('SAMEORIGIN');
|
||||
await expect(rejected.json()).resolves.toEqual({ error: 'Service Unavailable' });
|
||||
|
||||
expect(ended).toHaveLength(1);
|
||||
expect(ended[0]).toMatchObject({
|
||||
method: 'GET',
|
||||
status: 503,
|
||||
});
|
||||
expect(ended[0].requestId).toBeTruthy();
|
||||
|
||||
release();
|
||||
|
||||
expect((await first).status).toBe(200);
|
||||
|
||||
const afterRelease = await fetchHandler(new Request('http://localhost/slow'));
|
||||
expect(afterRelease.status).toBe(200);
|
||||
});
|
||||
it('releases the slot when a streaming handler returns', async () => {
|
||||
async function* stream() {
|
||||
yield { n: 1 };
|
||||
await new Promise(() => {});
|
||||
}
|
||||
|
||||
const fetchHandler = buildFetch(
|
||||
[
|
||||
{
|
||||
method: 'GET',
|
||||
pattern: '/stream',
|
||||
transport: 'sse',
|
||||
handler: async () => ({ stream: stream() }),
|
||||
},
|
||||
],
|
||||
{ limits: { maxConcurrentRequests: 1 } },
|
||||
);
|
||||
|
||||
const first = await fetchHandler(
|
||||
new Request('http://localhost/stream', {
|
||||
headers: { accept: 'text/event-stream' },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(first.status).toBe(200);
|
||||
|
||||
// The first response stream is still open, but its request slot should
|
||||
// already be released once handle() has returned.
|
||||
const second = await fetchHandler(
|
||||
new Request('http://localhost/stream', {
|
||||
headers: { accept: 'text/event-stream' },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(second.status).toBe(200);
|
||||
|
||||
await first.body?.cancel();
|
||||
await second.body?.cancel();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user