Skip to content

Commit eff0e46

Browse files
committed
fix: still high cpu usage
1 parent a2932df commit eff0e46

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

constants/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export const MEMPOOL_PROCESS_DELAY = 100
276276
// When fetching some address transactions, number of transactions to fetch at a time.
277277
// On chronik, the max allowed is 200
278278
export const CHRONIK_FETCH_N_TXS_PER_PAGE = 200
279-
export const INITIAL_ADDRESS_SYNC_FETCH_CONCURRENTLY = 10
279+
export const INITIAL_ADDRESS_SYNC_FETCH_CONCURRENTLY = 5
280280
export const TX_EMIT_BATCH_SIZE = 500
281281
export const TRANSFORM_TX_FROM_CHRONIK_BATCH_SIZE = 10
282282
export const DB_COMMIT_BATCH_SIZE = 500

services/chronikService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { PHASE_PRODUCTION_BUILD } from 'next/dist/shared/lib/constants'
3333
import { AddressType } from 'ecashaddrjs/dist/types'
3434
import { DecimalJsLike } from '@prisma/client/runtime/library'
3535

36+
const yieldEventLoop = async (): Promise<void> => await new Promise(resolve => setImmediate(resolve))
3637
const decoder = new TextDecoder()
3738

3839
export function getNullDataScriptData (outputScript: string): OpReturnData | null {
@@ -434,6 +435,9 @@ export class ChronikBlockchainClient {
434435

435436
syncedAlready += addressBatchSlice.length
436437

438+
// Yield event loop to prevent CPU starvation between address batches
439+
await yieldEventLoop()
440+
437441
// Yield full TX batches when buffer reaches TX_EMIT_BATCH_SIZE — use splice to drain in-place
438442
while (chronikTxs.length >= TX_EMIT_BATCH_SIZE) {
439443
const drained = chronikTxs.splice(0, TX_EMIT_BATCH_SIZE)
@@ -865,6 +869,8 @@ export class ChronikBlockchainClient {
865869
})
866870
)
867871
pairsFromBatch.push(...chunkResults)
872+
// Yield event loop between transform chunks to prevent CPU starvation
873+
await yieldEventLoop()
868874
}
869875

870876
for (const { row } of pairsFromBatch) {

0 commit comments

Comments
 (0)