From 1099cbb7244c32a7a113c4cd1abd70a31d98f41c Mon Sep 17 00:00:00 2001 From: David Santos Date: Sun, 12 Apr 2026 11:51:03 +0000 Subject: [PATCH] collector-worker: bound ingest POST with INGEST_PUSH_TIMEOUT_MS (BIT-2600) Add AbortController timeout to pushEventsToIngest (error ingest_push_timeout), env INGEST_PUSH_TIMEOUT_MS (default 60s, minimum 1s), Vitest coverage, and compose/env.example documentation. Made-with: Cursor --- .env.example | 1 + collector-worker/src/config/loadEnv.ts | 3 + .../src/ingest/pushEvents.test.ts | 80 +++++++++++++++++++ collector-worker/src/ingest/pushEvents.ts | 9 +++ .../src/scheduler/runHttpSourceOnce.ts | 1 + deploy/lxc/env.example | 1 + docker-compose.yml | 1 + 7 files changed, 96 insertions(+) create mode 100644 collector-worker/src/ingest/pushEvents.test.ts diff --git a/.env.example b/.env.example index 4383c82..c47f35c 100644 --- a/.env.example +++ b/.env.example @@ -31,6 +31,7 @@ ER_COLLECTOR_PROJECT_ALPHA_TOKEN=er_tst_a_secret_one # TICK_INTERVAL_MS=3000 # SOURCE_REFRESH_MS=15000 # HTTP_FETCH_TIMEOUT_MS=15000 +# INGEST_PUSH_TIMEOUT_MS=60000 # MAX_BACKOFF_EXPONENT=8 # COLLECTOR_DEFAULT_INGEST_TOKEN= diff --git a/collector-worker/src/config/loadEnv.ts b/collector-worker/src/config/loadEnv.ts index 014cd1c..1ff89eb 100644 --- a/collector-worker/src/config/loadEnv.ts +++ b/collector-worker/src/config/loadEnv.ts @@ -4,6 +4,7 @@ export type CollectorEnv = { DATABASE_URL: string; INGEST_BASE_URL: string; HTTP_FETCH_TIMEOUT_MS: number; + INGEST_PUSH_TIMEOUT_MS: number; TICK_INTERVAL_MS: number; SOURCE_REFRESH_MS: number; MAX_BACKOFF_EXPONENT: number; @@ -30,10 +31,12 @@ export function loadEnv(): CollectorEnv { if (typeof _ingestBase !== "string" || _ingestBase.trim() === "") { throw new Error("INGEST_BASE_URL is required (e.g. http://ingest-api:3041)"); } + const _ingestPushMs = _parseIntWithDefault(process.env.INGEST_PUSH_TIMEOUT_MS, 60_000); return { DATABASE_URL: _databaseUrl.trim(), INGEST_BASE_URL: _ingestBase.trim().replace(/\/$/, ""), HTTP_FETCH_TIMEOUT_MS: _parseIntWithDefault(process.env.HTTP_FETCH_TIMEOUT_MS, 30_000), + INGEST_PUSH_TIMEOUT_MS: Math.max(1000, _ingestPushMs), TICK_INTERVAL_MS: _parseIntWithDefault(process.env.TICK_INTERVAL_MS, 2000), SOURCE_REFRESH_MS: _parseIntWithDefault(process.env.SOURCE_REFRESH_MS, 10_000), MAX_BACKOFF_EXPONENT: _parseIntWithDefault(process.env.MAX_BACKOFF_EXPONENT, 8), diff --git a/collector-worker/src/ingest/pushEvents.test.ts b/collector-worker/src/ingest/pushEvents.test.ts new file mode 100644 index 0000000..648d356 --- /dev/null +++ b/collector-worker/src/ingest/pushEvents.test.ts @@ -0,0 +1,80 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { pushEventsToIngest } from "./pushEvents.js"; + +const _sampleEvent = { + schema_version: 1, + occurred_at: "2026-04-12T10:00:00.000Z", + level: "error", + message: "test", +} as const; + +describe("pushEventsToIngest", () => { + const _origFetch = global.fetch; + + afterEach(() => { + global.fetch = _origFetch; + vi.restoreAllMocks(); + }); + + it("short-circuits empty events without calling fetch", async () => { + global.fetch = vi.fn() as typeof fetch; + const _r = await pushEventsToIngest({ + ingestBaseUrl: "http://ingest", + bearerToken: "t", + events: [], + timeoutMs: 5000, + }); + expect(_r).toEqual({ ok: true, status: 200, accepted: 0 }); + expect(global.fetch).not.toHaveBeenCalled(); + }); + + it("returns ingest_push_timeout when the request is aborted", async () => { + global.fetch = vi.fn(async (_url, init) => { + await new Promise((_resolve, reject) => { + const _sig = init?.signal; + if (_sig === undefined) { + reject(new Error("expected AbortSignal")); + return; + } + if (_sig.aborted) { + reject(new DOMException("The operation was aborted.", "AbortError")); + return; + } + _sig.addEventListener("abort", () => { + reject(new DOMException("The operation was aborted.", "AbortError")); + }); + }); + return new Response(); + }) as typeof fetch; + const _r = await pushEventsToIngest({ + ingestBaseUrl: "http://ingest", + bearerToken: "t", + events: [_sampleEvent], + timeoutMs: 30, + }); + expect(_r).toEqual({ ok: false, status: null, error: "ingest_push_timeout" }); + }); + + it("returns ok when ingest accepts the batch", async () => { + global.fetch = vi.fn( + async () => + new Response(null, { + status: 200, + }), + ) as typeof fetch; + const _r = await pushEventsToIngest({ + ingestBaseUrl: "http://ingest", + bearerToken: "t", + events: [_sampleEvent], + timeoutMs: 5000, + }); + expect(_r).toEqual({ ok: true, status: 200, accepted: 1 }); + expect(global.fetch).toHaveBeenCalledWith( + "http://ingest/v1/ingest/events", + expect.objectContaining({ + method: "POST", + signal: expect.any(AbortSignal), + }), + ); + }); +}); diff --git a/collector-worker/src/ingest/pushEvents.ts b/collector-worker/src/ingest/pushEvents.ts index f822cce..049df76 100644 --- a/collector-worker/src/ingest/pushEvents.ts +++ b/collector-worker/src/ingest/pushEvents.ts @@ -8,11 +8,14 @@ export async function pushEventsToIngest(input: { ingestBaseUrl: string; bearerToken: string; events: IngestEventInput[]; + timeoutMs: number; }): Promise { if (input.events.length === 0) { return { ok: true, status: 200, accepted: 0 }; } const _url = `${input.ingestBaseUrl}/v1/ingest/events`; + const _controller = new AbortController(); + const _timer = setTimeout(() => _controller.abort(), input.timeoutMs); try { const _res = await fetch(_url, { method: "POST", @@ -21,6 +24,7 @@ export async function pushEventsToIngest(input: { Authorization: `Bearer ${input.bearerToken}`, }, body: JSON.stringify({ events: input.events }), + signal: _controller.signal, }); if (!_res.ok) { const _t = await _res.text(); @@ -32,7 +36,12 @@ export async function pushEventsToIngest(input: { } return { ok: true, status: _res.status, accepted: input.events.length }; } catch (_err) { + if (_err instanceof Error && _err.name === "AbortError") { + return { ok: false, status: null, error: "ingest_push_timeout" }; + } const _msg = _err instanceof Error ? _err.message : "ingest_post_failed"; return { ok: false, status: null, error: _msg }; + } finally { + clearTimeout(_timer); } } diff --git a/collector-worker/src/scheduler/runHttpSourceOnce.ts b/collector-worker/src/scheduler/runHttpSourceOnce.ts index 7027d6e..35e8da9 100644 --- a/collector-worker/src/scheduler/runHttpSourceOnce.ts +++ b/collector-worker/src/scheduler/runHttpSourceOnce.ts @@ -139,6 +139,7 @@ export async function runHttpSourceOnce(input: { ingestBaseUrl: env.INGEST_BASE_URL, bearerToken: _token, events: _fetched.events, + timeoutMs: env.INGEST_PUSH_TIMEOUT_MS, }); if (!_push.ok) { diff --git a/deploy/lxc/env.example b/deploy/lxc/env.example index 391524d..aad27c8 100644 --- a/deploy/lxc/env.example +++ b/deploy/lxc/env.example @@ -27,6 +27,7 @@ INGEST_BASE_URL=http://127.0.0.1:3041 TICK_INTERVAL_MS=3000 SOURCE_REFRESH_MS=15000 HTTP_FETCH_TIMEOUT_MS=15000 +INGEST_PUSH_TIMEOUT_MS=60000 MAX_BACKOFF_EXPONENT=8 # --- admin-ui (Next.js server-side API calls) — target the Fastify `api/` service (default PORT 3200) --- diff --git a/docker-compose.yml b/docker-compose.yml index 2f00ee4..f4c0d82 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -99,6 +99,7 @@ services: TICK_INTERVAL_MS: ${TICK_INTERVAL_MS:-3000} SOURCE_REFRESH_MS: ${SOURCE_REFRESH_MS:-15000} HTTP_FETCH_TIMEOUT_MS: ${HTTP_FETCH_TIMEOUT_MS:-15000} + INGEST_PUSH_TIMEOUT_MS: ${INGEST_PUSH_TIMEOUT_MS:-60000} MAX_BACKOFF_EXPONENT: ${MAX_BACKOFF_EXPONENT:-8} NODE_ENV: production ER_COLLECTOR_PROJECT_ALPHA_TOKEN: ${ER_COLLECTOR_PROJECT_ALPHA_TOKEN:-er_tst_a_secret_one}