Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/refactor polling #8976

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,53 +177,42 @@ export class ClientFeatureToggleCache {

private async onUpdateRevisionEvent() {
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 =
await this.configurationRevisionService.getMaxRevisionId();

if (this.currentRevisionId === 0) {
await this.populateBaseCache(latestRevision);
} else {
const changeEvents = await this.eventStore.getRevisionRange(
this.currentRevisionId,
latestRevision,
);

const changedToggles = [
...new Set(
changeEvents
.filter((event) => event.featureName)
.map((event) => event.featureName!),
),
];
const newToggles = await this.getChangedToggles(
changedToggles,
latestRevision, // TODO: this should come back from the same query to not be out of sync
);

if (this.cache.development) {
this.cache.development.addRevision(newToggles);
}
}
this.currentRevisionId = latestRevision;
}
const changeEvents = await this.eventStore.getRevisionRange(
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,
});
const changedToggles = [
...new Set(
changeEvents
.filter((event) => event.featureName)
.map((event) => event.featureName!),
),
];
const newToggles = await this.getChangedToggles(
changedToggles,
latestRevision, // TODO: this should come back from the same query to not be out of sync
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ok, but I was thinking about if we might not want to return the revisionId, from query .

Because the flow is getMaxRevisionId(), based on which we get the events, and if now with those events, go pull data and revision ID has changed, it will get out sync. I think best is

If we getMaxRevisionId, we use that across all the calls we make.

);

// TODO: Discussion point. Should we filter events by environment and only add revisions in the cache
// 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);
}
console.log(`Populated base cache with ${features.length} features`);

this.currentRevisionId = latestRevision;
}

async getChangedToggles(
Expand Down
Loading