Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/module49.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ export class Module49 {
*/
public processStreamBatch(items: StreamBatchItem49[]): Module49Result[] {
const startTime = performance.now();
const results: Module49Result[] = new Array(items.length);
const results: Module49Result[] = [];

for (let i = 0; i < items.length; i += this.batchChunkSize) {
const chunkEnd = Math.min(i + this.batchChunkSize, items.length);
for (let j = i; j < chunkEnd; j++) {
const item = items[j];
if (!item) continue;

results[j] = this.processSingleItem(item);
results.push(this.processSingleItem(item));
}
}

Expand All @@ -83,7 +83,7 @@ export class Module49 {
*/
public processSingleItem(item: StreamBatchItem49): Module49Result {
const nowSec = item.timestamp ?? Math.floor(Date.now() / 1000);
const cacheKey = `${item.id}_${item.stream.withdrawn.toString()}_${item.stream.paused ? 1 : 0}_${nowSec}`;
const cacheKey = `${item.id}_${item.stream.withdrawn.toString()}_${item.stream.paused ? 1 : 0}_${item.stream.cancelled ? 1 : 0}_${item.stream.pausedAt}_${item.stream.ratePerSecond.toString()}_${item.stream.startTime}_${item.stream.endTime}_${nowSec}`;

if (this.enableOptimization) {
const cached = this.cache.get(cacheKey);
Expand Down
5 changes: 4 additions & 1 deletion src/nonce/NonceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ export class NonceManager {

const { promise, cancel } = this.enqueue();

let timer: ReturnType<typeof setTimeout> | undefined;
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => {
timer = setTimeout(() => {
cancel();
reject(new Error(`NonceManager: acquire timed out after ${timeoutMs}ms`));
}, timeoutMs);
Expand All @@ -198,6 +199,8 @@ export class NonceManager {
throw err;
}
throw new Error(String(err));
} finally {
if (timer) clearTimeout(timer);
}
}

Expand Down
33 changes: 31 additions & 2 deletions src/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ import { ConduitError, RateLimitError, InsufficientBalanceError, StreamErrorCode
* Tracks which v1-deprecated methods have already warned this session, so
* repeated calls (e.g. in a hot loop) do not spam the console.
*/
/** Default concurrency limit for bounded page-fetching (Issue #549). */
const DEFAULT_LIST_CONCURRENCY = 8;

/**
* Runs `fn` over `items` with at most `concurrency` in-flight calls.
* Preserves result ordering to match a naive `Promise.all` fan-out.
*/
async function mapWithConcurrency<T, R>(
items: T[],
concurrency: number,
fn: (item: T) => Promise<R>,
): Promise<R[]> {
const results = new Array<R>(items.length);
let index = 0;

async function worker() {
while (index < items.length) {
const i = index++;
results[i] = await fn(items[i]);
}
}

const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
await Promise.all(workers);
return results;
}

const _warnedDeprecations = new Set<string>();

/**
Expand Down Expand Up @@ -613,8 +640,10 @@ export class StreamsModule {
// call would serially resolve the address and then simulate — 2 serial
// RPCs per stream. Pre-warming collapses the address lookups into a
// single parallel fan-out before the info simulations begin.
await Promise.all(ids.map(id => this._resolveAddr(id)));
const streams = await Promise.all(ids.map(id => this.get(id)));
// Bounded concurrency (#549) avoids hammering the RPC endpoint with
// up to 100 simultaneous simulateTransaction requests.
await mapWithConcurrency(ids, DEFAULT_LIST_CONCURRENCY, (id) => this._resolveAddr(id));
const streams = await mapWithConcurrency(ids, DEFAULT_LIST_CONCURRENCY, (id) => this.get(id));
const hasNextPage = hasNextPageOverride ?? ids.length === limit;
const totalCount = BigInt(offset + ids.length);
return {
Expand Down