mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
Feat/refactor polling (#8976)
This PR refactors the method that listens on revision changes: - Now supports all environments - Removed unnecessary populate cache method # Discussion point In the listen method, should we implement logic to look into which environments the events touched? By doing this we would: - Reduce cache size - Save some memory/CPU if the environment is not initialized in the cache, because we could skip the DB calls.
This commit is contained in:
parent
428b0b370b
commit
b2cf0e4e6b
@ -178,55 +178,44 @@ export class ClientFeatureToggleCache {
|
|||||||
|
|
||||||
private async onUpdateRevisionEvent() {
|
private async onUpdateRevisionEvent() {
|
||||||
if (this.flagResolver.isEnabled('deltaApi')) {
|
if (this.flagResolver.isEnabled('deltaApi')) {
|
||||||
await this.pollEvents();
|
await this.listenToRevisionChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async pollEvents() {
|
public async listenToRevisionChange() {
|
||||||
|
const keys = Object.keys(this.cache);
|
||||||
|
|
||||||
|
if (keys.length === 0) return;
|
||||||
const latestRevision =
|
const latestRevision =
|
||||||
await this.configurationRevisionService.getMaxRevisionId();
|
await this.configurationRevisionService.getMaxRevisionId();
|
||||||
|
|
||||||
if (this.currentRevisionId === 0) {
|
const changeEvents = await this.eventStore.getRevisionRange(
|
||||||
await this.populateBaseCache(latestRevision);
|
this.currentRevisionId,
|
||||||
} else {
|
latestRevision,
|
||||||
const changeEvents = await this.eventStore.getRevisionRange(
|
);
|
||||||
this.currentRevisionId,
|
|
||||||
latestRevision,
|
|
||||||
);
|
|
||||||
|
|
||||||
const changedToggles = [
|
const changedToggles = [
|
||||||
...new Set(
|
...new Set(
|
||||||
changeEvents
|
changeEvents
|
||||||
.filter((event) => event.featureName)
|
.filter((event) => event.featureName)
|
||||||
.map((event) => event.featureName!),
|
.map((event) => event.featureName!),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
const newToggles = await this.getChangedToggles(
|
const newToggles = await this.getChangedToggles(
|
||||||
changedToggles,
|
changedToggles,
|
||||||
latestRevision, // TODO: this should come back from the same query to not be out of sync
|
latestRevision, // TODO: this should come back from the same query to not be out of sync
|
||||||
);
|
);
|
||||||
|
|
||||||
if (this.cache.development) {
|
// TODO: Discussion point. Should we filter events by environment and only add revisions in the cache
|
||||||
this.cache.development.addRevision(newToggles);
|
// for the environment that changed? If we do that we can also save CPU and memory, because we don't need
|
||||||
}
|
// to talk to the database if the cache is not initialized for that environment
|
||||||
|
for (const key of keys) {
|
||||||
|
this.cache[key].addRevision(newToggles);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.currentRevisionId = latestRevision;
|
this.currentRevisionId = latestRevision;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async populateBaseCache(latestRevisionId: number) {
|
|
||||||
const features = await this.getClientFeatures({
|
|
||||||
environment: 'development',
|
|
||||||
});
|
|
||||||
if (this.cache.development) {
|
|
||||||
this.cache.development.addRevision({
|
|
||||||
updated: features as any, //impressionData is not on the type but should be
|
|
||||||
removed: [],
|
|
||||||
revisionId: latestRevisionId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
console.log(`Populated base cache with ${features.length} features`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getChangedToggles(
|
async getChangedToggles(
|
||||||
toggles: string[],
|
toggles: string[],
|
||||||
revisionId: number,
|
revisionId: number,
|
||||||
|
Loading…
Reference in New Issue
Block a user