@@ -8,6 +8,7 @@ import {Type} from "../addons/eosjs-native/eosjs-serialize";
88import { debugLog , hLog } from "../helpers/common_functions" ;
99import { createHash } from "crypto" ;
1010import flatstr from 'flatstr' ;
11+ import { Options } from "amqplib" ;
1112
1213const FJS = require ( 'fast-json-stringify' ) ;
1314
@@ -124,6 +125,9 @@ export default class MainDSWorker extends HyperionWorker {
124125
125126 allowedDynamicContracts : Set < string > = new Set < string > ( ) ;
126127
128+ backpressureQueue : any [ ] = [ ] ;
129+ waitToSend = false ;
130+
127131 constructor ( ) {
128132
129133 super ( ) ;
@@ -290,6 +294,16 @@ export default class MainDSWorker extends HyperionWorker {
290294 this . ch . consume ( process . env [ 'worker_queue' ] , ( data ) => {
291295 this . consumerQueue . push ( data ) . catch ( console . log ) ;
292296 } ) ;
297+ this . ch . on ( 'drain' , args => {
298+ this . waitToSend = false ;
299+ while ( this . backpressureQueue . length > 0 ) {
300+ const msg = this . backpressureQueue . shift ( ) ;
301+ const status = this . controlledSendToQueue ( msg . queue , msg . payload , msg . options ) ;
302+ if ( ! status ) {
303+ break ;
304+ }
305+ }
306+ } ) ;
293307 }
294308 }
295309
@@ -478,15 +492,15 @@ export default class MainDSWorker extends HyperionWorker {
478492 hLog ( `${ block_num } was filtered with ${ inline_count } actions!` ) ;
479493 }
480494 try {
495+ trace [ 1 ] . signatures = signatures ;
481496 this . routeToPool ( trace [ 1 ] , {
482497 block_num,
483498 block_id,
484499 producer,
485500 ts,
486501 inline_count,
487502 filtered,
488- live : process . env [ 'live_mode' ] ,
489- signatures
503+ live : process . env [ 'live_mode' ]
490504 } ) ;
491505 } catch ( e ) {
492506 hLog ( e ) ;
@@ -622,15 +636,33 @@ export default class MainDSWorker extends HyperionWorker {
622636 }
623637
624638 const pool_queue = `${ this . chain } :ds_pool:${ selected_q } ` ;
625- if ( this . ch_ready ) {
626- // console.log('selected_q', pool_queue);
627- this . ch . sendToQueue ( pool_queue , bufferFromJson ( trace , true ) , { headers} ) ;
628- return true ;
639+ const payload = bufferFromJson ( trace , true ) ;
640+
641+ if ( ! this . waitToSend ) {
642+ if ( this . ch_ready ) {
643+ this . controlledSendToQueue ( pool_queue , payload , { headers} ) ;
644+ return true ;
645+ } else {
646+ return false ;
647+ }
629648 } else {
649+ this . backpressureQueue . push ( {
650+ queue : pool_queue ,
651+ payload : payload ,
652+ options : { headers}
653+ } ) ;
630654 return false ;
631655 }
632656 }
633657
658+ controlledSendToQueue ( pool_queue : string , payload : Buffer , options : Options . Publish ) : boolean {
659+ const enqueueResult = this . ch . sendToQueue ( pool_queue , payload , options ) ;
660+ if ( ! enqueueResult ) {
661+ this . waitToSend = true ;
662+ }
663+ return enqueueResult ;
664+ }
665+
634666 createSerialBuffer ( inputArray ) {
635667 return new Serialize . SerialBuffer ( { textEncoder : this . txEnc , textDecoder : this . txDec , array : inputArray } ) ;
636668 }
@@ -1160,7 +1192,7 @@ export default class MainDSWorker extends HyperionWorker {
11601192 let jsonRow = await this . processContractRowNative ( payload , block_num ) ;
11611193
11621194 if ( jsonRow ?. value && ! jsonRow [ '_blacklisted' ] ) {
1163- console . log ( jsonRow ) ;
1195+ debugLog ( jsonRow ) ;
11641196 debugLog ( 'Delta DS failed ->>' , jsonRow ) ;
11651197 jsonRow = await this . processContractRowNative ( payload , block_num - 1 ) ;
11661198 debugLog ( 'Retry with previous ABI ->>' , jsonRow ) ;
@@ -1495,7 +1527,7 @@ export default class MainDSWorker extends HyperionWorker {
14951527 } catch ( e ) {
14961528 hLog ( `Delta struct [${ key } ] processing error: ${ e . message } ` ) ;
14971529 hLog ( e ) ;
1498- console . log ( data [ 1 ] ) ;
1530+ hLog ( data [ 1 ] ) ;
14991531 }
15001532 }
15011533 }
0 commit comments