Repository navigation
feat: add per-chain request budgets and streaming inputs to multi-chain scans - #288
Bolajiomo99 wants to merge 4 commits into
Conversation
…er pool Add async-iterable scan inputs, bounded prefetch with backpressure, and per-chain request, in-flight and buffer budgets. Cancellation drains queues, and progress accounting stays under the existing chain-concurrency scheduler.
|
@Bolajiomo99 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
truthixify
left a comment
There was a problem hiding this comment.
Head 5bb2f5c screened clean for the known malware indicators; linked issue #275 is assigned to PR author @Bolajiomo99. Changes requested: createBoundedAsyncIterable launches iterator.next() calls concurrently and enqueues values in promise-settlement order. A later pull can resolve done before an earlier value, causing the consumer to terminate and drop that value; otherwise values can be reordered. Preserve source-call order, defer completion until all earlier slots settle, and add a regression with out-of-order next() settlements.
Closes #275
Summary
MultichainScannerPoollimited how many chain scans run at once, but each chainstill took a fully materialized array and had no request, buffer or memory bound.
A slow or rate-limited provider could therefore pin arbitrary memory, and there
was no way to cancel work that had already been handed to the pool.
This adds async-iterable inputs, per-chain request/in-flight/buffer budgets,
backpressure, cancellation and progress accounting underneath the existing
global chain-concurrency scheduler. The global
concurrencyoption is unchangedand still the outer scheduler; per-chain budgets never replace it.
Changes
Bounded streaming (
src/scanner/boundedStream.ts, new)BoundedAnnouncementStreamwraps anyAsyncIterableand guarantees:maxInFlightRequestsoutstanding pulls;maxBufferedAnnouncementsbuffered values, with in-flight pullscounted against the buffer so prefetch cannot overshoot;
maxRequestspulls, after which the scan fails withScanBudgetExceededErrorinstead of silently truncating;close()drains the buffer, rejects pending reads and calls the source'sreturn()so a cancelled scan releases references immediately.ChainRequestBudgetdefaults are finite: 10 000 requests, 2 in-flight,256 buffered.
0, negative and non-finite limits are rejected.resolveChainRequestBudget()andDEFAULT_CHAIN_REQUEST_BUDGETare exported.Pool (
src/scanner-pool.ts)source?: AsyncIterable<...>in addition toannouncements?;ckbuses the same shape withcells. Providing both, orneither, is rejected.
budget→ poolbudgets[chain]→pool
requestBudget→ finite defaults.scanned in their existing small batches with no full materialization.
AbortControllerperscanAll: caller abort rejects with thesignal's reason, and
failFastfailures cancel sibling chains. WithfailFast: false, other chains still run to completion.ProgressEventnow also reportsrequests,bufferedandstreaming;mid-stream events are throttled, with a final
processed/buffered: 0eventper chain on success, error or cancellation.
Errors (
src/errors.ts)ScanBudgetExceededError(WRAITH/INPUT/SCAN_BUDGET_EXCEEDED) with{ chain, kind, limit, processed }and adescribe()hint for resuming.Public API
MultichainScannerPool, its options/input/resulttypes,
ProgressEvent,ChainRequestBudget,DEFAULT_CHAIN_REQUEST_BUDGETand
resolveChainRequestBudget.Tests (
test/scanner-pool-streaming.test.ts, 19 tests)Stress and resilience coverage:
maxInFlightRequestsacross repeated scansmaxBufferedAnnouncements, and pull-ahead is boundedmaxRequestsfails after exactly that manypulls and closes the source
other completes with a larger one
concurrency: 1still orderschains as before
AbortError, callsreturn()on every source, and nofurther pulls happen afterwards
failFastcancels siblings; an already-aborted signal touches no sourcebuffered: 0and the full countBenchmarks (
test/bench/scanner-pool.bench.ts)Same two-chain scan through simulated providers, run with
pnpm bench:Heap delta per profile is printed with the table (approximate, no forced GC).
The local profile shows the buffer filling to its budget while provider-bound
profiles never buffer, which is the intended backpressure behaviour.
Verification
pnpm buildpnpm test(1618 passed, 5 skipped; plus 17 Svelte)pnpm test:typespnpm format:checkpnpm api:check(etc/sdk.api.mdregenerated)pnpm size(Root ESM 36.80/37.1 KB, Stellar CJS 41.01/41.2 KB)pnpm bench(table above)Notes for reviewers
concurrencyis untouched and remains the outer scheduler; all new limits areper chain, so one slow chain holds only its own slot and its own budget.
maxRequestsraises instead of returning a partial list, because atruncated scan would look complete. Matched results collected so far are
still surfaced through progress accounting.
budgets were raised minimally rather than re-baselined.