Reduce GC pressure and memory allocations during webhook ingestion - #2767
Open
sodiqscript111 wants to merge 2 commits into
Open
Reduce GC pressure and memory allocations during webhook ingestion#2767sodiqscript111 wants to merge 2 commits into
sodiqscript111 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 async.Poolof reusablebytes.Bufferinstances (16 KB default cap, only buffers ≤64 KB returned to the pool), usingio.Copyinstead of per-requestio.ReadAllallocations to cut GC pressure under load.Oversized bodies are detected by reading up to
maxIngestSize + 1bytes; when the limit is exceeded the handler responds with HTTP 413 instead of silently truncating and failing signature verification later.extractPayloadFromIngestEventReqtakes the already-readrawPayloadand, for JSON/unknown content types, returns that slice instead of readingr.Bodyagain. Tests were updated for the new signature; benchmarks compare the old read path vs pooledio.Copy.Reviewed by Cursor Bugbot for commit 896d074. Bugbot is set up for automated code reviews on this repo. Configure here.