Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cardano-services): rework epoch poll
- rework EpochPollService as singleton class, used across services
- Loading branch information
Ivaylo Andonov
committed
Aug 8, 2022
1 parent
9b3e26c
commit 49eb011
Showing
11 changed files
with
95 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/cardano-services/src/NetworkInfo/DbSyncNetworkInfoProvider/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './DbSyncNetworkInfoProvider'; | ||
export * from './utils'; | ||
export * as NetworkInfoCacheKey from './keys'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
packages/cardano-services/src/NetworkInfo/DbSyncNetworkInfoProvider/utils.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './http'; | ||
export * from './provider'; | ||
export * from './hexString'; | ||
export * from './polling'; |
64 changes: 64 additions & 0 deletions
64
packages/cardano-services/src/util/polling/EpochPollService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { AsyncAction, InMemoryCache, UNLIMITED_CACHE_TTL } from '../../InMemoryCache'; | ||
|
||
export const EPOCH_POLL_INTERVAL_DEFAULT = 10_000; | ||
export const CURRENT_EPOCH_CACHE_KEY = 'current_epoch'; | ||
|
||
/** | ||
* Class to handle epoch rollover through db polling | ||
*/ | ||
export class EpochPollService { | ||
static #instance: EpochPollService; | ||
#cache: InMemoryCache; | ||
#timeoutId: number; | ||
#interval: number; | ||
#asyncAction: AsyncAction<number>; | ||
|
||
private constructor(cache: InMemoryCache, asyncAction: AsyncAction<number>, interval: number) { | ||
this.#cache = cache; | ||
this.#interval = interval; | ||
this.#asyncAction = asyncAction; | ||
this.executePoll = this.executePoll.bind(this); | ||
} | ||
|
||
/** | ||
* Poll execution to determine new epoch rollover | ||
*/ | ||
private async executePoll() { | ||
const lastEpoch = await this.#asyncAction(); | ||
const currentEpoch = this.#cache.getVal<number>(CURRENT_EPOCH_CACHE_KEY); | ||
const shouldClearCache = !!(currentEpoch && lastEpoch > currentEpoch); | ||
|
||
if (!currentEpoch || shouldClearCache) { | ||
shouldClearCache ? this.#cache.clear() : void 0; | ||
this.#cache.set<number>(CURRENT_EPOCH_CACHE_KEY, lastEpoch, UNLIMITED_CACHE_TTL); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a single instance of EpochPollService | ||
* | ||
* @returns {EpochPollService} EpochPollService instance | ||
*/ | ||
static create(cache: InMemoryCache, asyncAction: AsyncAction<number>, interval: number): EpochPollService { | ||
if (!this.#instance) { | ||
this.#instance = new EpochPollService(cache, asyncAction, interval); | ||
} | ||
return this.#instance; | ||
} | ||
|
||
/** | ||
* Starts the poll execution, should be executed only once across all HTTP services | ||
*/ | ||
start() { | ||
if (this.#timeoutId) return; | ||
|
||
this.#timeoutId = setInterval(this.executePoll, this.#interval); | ||
} | ||
|
||
/** | ||
* Stops the poll execution | ||
*/ | ||
stop() { | ||
clearInterval(this.#timeoutId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './EpochPollService'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters