fix(perf): paginate GET /streams/pending to prevent unbounded memory fetch - #475
Merged
Xhristin3 merged 1 commit intoJul 29, 2026
Merged
Conversation
…fetch (XStreamRollz#337) - Add GET /streams/pending endpoint to StreamsController with limit/cursor query params (limit default 100, max 1000). Returns { data, nextCursor } where nextCursor is null on the final page. - Implement getPendingEvents() in StreamsService, StreamsRepository (stub), and StreamsDbRepository (queries stream_data ordered by timestamp ASC). - Add POLL_BATCH_SIZE env var (default 100) to config.ts zod schema. - Rewrite worker pollOnce() to paginate: fetches batches in a loop, advancing cursor until nextCursor is null or the high-watermark is reached (back-pressure guard). Legacy plain-array responses are still accepted for backward compatibility. - Document POLL_BATCH_SIZE in xstreamroll-processing/.env.example. Closes XStreamRollz#337
Closed
4 tasks
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.
Summary
Closes #337
The processing worker called
GET /streams/pendingwith no pagination. During a backlog (e.g. after downtime) this returned all pending events in a single HTTP response, causing an unbounded memory spike and unpredictable worker footprint.Changes
api/src/streams/streams.controller.tsNew
GET /streams/pendingendpoint withlimitandcursorquery params:limit: batch size (default 100, max 1000)cursor: offset for the next page (default 0){ data: PendingStreamEvent[], nextCursor: number | null }nextCursorisnullwhen the returned batch is smaller thanlimit(final page)api/src/streams/streams.service.tsAdded
getPendingEvents(limit, offset)delegating to the repository layer.api/src/streams/repository/streams-db.repository.tsSQL implementation: queries
stream_dataordered bytimestamp ASC(FIFO) withLIMIT / OFFSET.api/src/streams/repository/streams.repository.ts(in-memory)Stub returning
{ data: [], nextCursor: null }— the in-memory repository has no stream_data store and is used only in unit tests.xstreamroll-processing/src/config.tsAdded
POLL_BATCH_SIZEto the zod schema (default'100', transformed to a positive integer).xstreamroll-processing/src/worker.tsRewrote
pollOnce()to paginate:POLL_BATCH_SIZEevents per requestnextCursoruntil it isnullor until the high-watermark is reached (back-pressure)xstreamroll-processing/.env.exampleDocuments
POLL_BATCH_SIZEwith description and trade-offs.Behaviour
Testing