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

fix: initialize cache when we get the first request #8971

Merged
merged 3 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 @@ -115,26 +115,39 @@ export class ClientFeatureToggleCache {
this.clientFeatureToggleStore = clientFeatureToggleStore;
this.flagResolver = flagResolver;
this.onUpdateRevisionEvent = this.onUpdateRevisionEvent.bind(this);
this.cache = {};

// this.initCache(); TODO: we dont want to initialize cache on startup, but ondemand in future?
this.initRevisionId();
this.configurationRevisionService.on(
UPDATE_REVISION,
this.onUpdateRevisionEvent,
);
}

private async initRevisionId() {
this.currentRevisionId =
await this.configurationRevisionService.getMaxRevisionId();
}

async getDelta(
sdkRevisionId: number | undefined,
environment: string,
projects: string[],
): Promise<ClientFeatureChange | undefined> {
const requiredRevisionId = sdkRevisionId || 0;

const hasCache = this.cache[environment] !== undefined;

if (!hasCache) {
await this.initEnvironmentCache(environment);
}

// Should get the latest state if revision does not exist or if sdkRevision is not present
// We should be able to do this without going to the database by merging revisions from the cache with
// the base case
const firstTimeCalling = !sdkRevisionId;
if (
!sdkRevisionId ||
firstTimeCalling ||
(sdkRevisionId &&
sdkRevisionId !== this.currentRevisionId &&
!this.cache[environment].hasRevision(sdkRevisionId))
Expand Down Expand Up @@ -203,7 +216,6 @@ export class ClientFeatureToggleCache {
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
Expand Down Expand Up @@ -238,67 +250,25 @@ export class ClientFeatureToggleCache {
revisionId,
};
}
// TODO: I think we should remove it as is, because we do not need initialized cache, I think we should populate cache on demand for each env
// also we already have populateBaseCache method
public async initCache() {
//TODO: This only returns stuff for the default environment!!! Need to pass a query to get the relevant environment
// featuresByEnvironment cache

// The base cache is a record of <environment, array>
// Each array holds a collection of objects that contains the revisionId and which
// flags changed in each revision. It also holds a type that informs us whether or not
// the revision is the base case or if is an update or remove operation

// To get the base for each cache we need to get all features for all environments and the max revision id

// hardcoded for now
// const environments = ["default", "development", "production"];
const defaultBaseFeatures = await this.getClientFeatures({
environment: 'default',
});
const developmentBaseFeatures = await this.getClientFeatures({
environment: 'development',
});
const productionBaseFeatures = await this.getClientFeatures({
environment: 'production',
});

const defaultCache = new RevisionCache([
{
revisionId: this.currentRevisionId,
updated: [defaultBaseFeatures],
removed: [],
},
]);
public async initEnvironmentCache(environment: string) {
// Todo: replace with method that gets all features for an environment
const baseFeatures = await this.getClientFeatures({
environment,
});

const developmentCache = new RevisionCache([
{
revisionId: this.currentRevisionId,
updated: [developmentBaseFeatures],
removed: [],
},
]);
this.currentRevisionId =
await this.configurationRevisionService.getMaxRevisionId();

const productionCache = new RevisionCache([
const cache = new RevisionCache([
{
revisionId: this.currentRevisionId,
updated: [productionBaseFeatures],
updated: baseFeatures,
removed: [],
},
]);

// Always assume that the first item of the array is the base
const cache = {
default: defaultCache,
development: developmentCache,
production: productionCache,
};

const latestRevision =
await this.configurationRevisionService.getMaxRevisionId();

this.currentRevisionId = latestRevision;
this.cache = cache;
this.cache[environment] = cache;
}

async getClientFeatures(
Expand Down
Loading