From 6714ef8a0eb94caceda2a3279f7379de755bdb28 Mon Sep 17 00:00:00 2001 From: Verisana Date: Thu, 14 Sep 2023 14:25:50 +0100 Subject: [PATCH] feat: add locking to state generate --- src/stateful-event-subscriber.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/stateful-event-subscriber.ts b/src/stateful-event-subscriber.ts index e76e47ffc..5a05b70c7 100644 --- a/src/stateful-event-subscriber.ts +++ b/src/stateful-event-subscriber.ts @@ -51,6 +51,8 @@ export abstract class StatefulEventSubscriber private _lastPublishedTimeMs: number = 0; + private isForcedGenerateStateIsRunning = false; + constructor( public readonly parentName: string, _name: string, @@ -95,8 +97,16 @@ export abstract class StatefulEventSubscriber this.logger.debug( `${this.parentName}: ${this.name}: forced to regenerate state`, ); - const state = await this.generateState(blockNumber); - this.setState(state, blockNumber); + if (this.isForcedGenerateStateIsRunning) { + return; + } + this.isForcedGenerateStateIsRunning = true; + try { + const state = await this.generateState(blockNumber); + this.setState(state, blockNumber); + } finally { + this.isForcedGenerateStateIsRunning = false; + } } else { if (this.dexHelper.config.isSlave && this.masterPoolNeeded) { let stateAsString = await this.dexHelper.cache.hget(