File tree Expand file tree Collapse file tree 2 files changed +263
-229
lines changed Expand file tree Collapse file tree 2 files changed +263
-229
lines changed Original file line number Diff line number Diff line change 1
- import { parseEnum , unwrapOptionalProp } from '../helpers' ;
1
+ import { getUintEnvOrDefault , parseEnum , unwrapOptionalProp } from '../helpers' ;
2
2
import {
3
3
BlockQueryResult ,
4
4
ContractTxQueryResult ,
@@ -66,6 +66,7 @@ import { PgStoreEventEmitter } from './pg-store-event-emitter';
66
66
import { SyntheticPoxEventName } from '../pox-helpers' ;
67
67
import { logger } from '../logger' ;
68
68
import { PgSqlClient } from '@hirosystems/api-toolkit' ;
69
+ import PQueue from 'p-queue' ;
69
70
70
71
export const TX_COLUMNS = [
71
72
'tx_id' ,
@@ -1335,3 +1336,21 @@ export function newReOrgUpdatedEntities(): ReOrgUpdatedEntities {
1335
1336
restoredMempoolTxs : 0 ,
1336
1337
} ;
1337
1338
}
1339
+
1340
+ /**
1341
+ * Priority queue for parallel Postgres write query execution. This helps performance because it
1342
+ * parallelizes the work postgres.js has to do when serializing JS types to PG types.
1343
+ */
1344
+ export class PgWriteQueue {
1345
+ readonly queue : PQueue ;
1346
+ constructor ( ) {
1347
+ const concurrency = Math . max ( 1 , getUintEnvOrDefault ( 'STACKS_BLOCK_DATA_INSERT_CONCURRENCY' , 4 ) ) ;
1348
+ this . queue = new PQueue ( { concurrency, autoStart : true } ) ;
1349
+ }
1350
+ enqueue ( task : Parameters < PQueue [ 'add' ] > [ 0 ] ) : void {
1351
+ void this . queue . add ( task ) ;
1352
+ }
1353
+ done ( ) : Promise < void > {
1354
+ return this . queue . onIdle ( ) ;
1355
+ }
1356
+ }
You can’t perform that action at this time.
0 commit comments