Skip to content

Reduce GC pressure and memory allocations during webhook ingestion - #2767

Open
sodiqscript111 wants to merge 2 commits into
frain-dev:mainfrom
sodiqscript111:main
Open

Reduce GC pressure and memory allocations during webhook ingestion#2767
sodiqscript111 wants to merge 2 commits into
frain-dev:mainfrom
sodiqscript111:main

Conversation

@sodiqscript111

@sodiqscript111 sodiqscript111 commented Aug 2, 2026

Copy link
Copy Markdown

What does this PR do?

This PR optimizes the webhook ingestion pipeline (POST /ingest) by reducing memory allocations during request body processing using sync.Pool. It also fixes an issue where oversized payloads could be silently truncated before validation.

Background

Previously, webhook payloads were read using io.ReadAll(), which allocated a new []byte for every request. Under high throughput, these short-lived allocations increased garbage collection pressure, resulting in unnecessary CPU usage and latency spikes.

Additionally, the use of io.LimitReader could silently truncate payloads that exceeded the configured size limit. These truncated payloads would later fail signature verification instead of returning the appropriate HTTP error.

Changes
Introduced a sync.Pool of reusable bytes.Buffer instances (16 KB default capacity) for reading webhook payloads.
Replaced io.ReadAll() with io.Copy() into pooled buffers to significantly reduce per-request allocations.
Added protection against oversized pooled buffers by only returning buffers with a capacity of 64 KB or less to the pool.
Fixed oversized payload handling by reading maxIngestSize + 1 bytes, allowing the handler to explicitly detect requests that exceed the configured limit and return HTTP 413 (Payload Too Large) instead of silently truncating the request.


Note

Medium Risk
Touches the hot ingest path (verification, payload extraction, and enqueue) with pooled-buffer lifetime constraints; behavior change for oversized bodies (413 vs truncated verify failure) is intentional but client-visible.

Overview
Webhook ingest (IngestEvent) now reads request bodies through a sync.Pool of reusable bytes.Buffer instances (16 KB default cap, only buffers ≤64 KB returned to the pool), using io.Copy instead of per-request io.ReadAll allocations to cut GC pressure under load.

Oversized bodies are detected by reading up to maxIngestSize + 1 bytes; when the limit is exceeded the handler responds with HTTP 413 instead of silently truncating and failing signature verification later.

extractPayloadFromIngestEventReq takes the already-read rawPayload and, for JSON/unknown content types, returns that slice instead of reading r.Body again. Tests were updated for the new signature; benchmarks compare the old read path vs pooled io.Copy.

Reviewed by Cursor Bugbot for commit 896d074. Bugbot is set up for automated code reviews on this repo. Configure here.

@CLAassistant

CLAassistant commented Aug 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants