Skip to content

Commit

Permalink
feat: add locking to state generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Verisana committed Sep 14, 2023
1 parent 4704f8c commit 6714ef8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/stateful-event-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export abstract class StatefulEventSubscriber<State>

private _lastPublishedTimeMs: number = 0;

private isForcedGenerateStateIsRunning = false;

constructor(
public readonly parentName: string,
_name: string,
Expand Down Expand Up @@ -95,8 +97,16 @@ export abstract class StatefulEventSubscriber<State>
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(
Expand Down

0 comments on commit 6714ef8

Please sign in to comment.