Skip to content

Commit

Permalink
Add Full Deltas Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sroyal-statsig committed Dec 5, 2023
1 parent 95617e7 commit 6e76ef4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/StatsigNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export default class StatsigNetwork {
previousDerivedFields?: Record<string, string>;
hadBadDeltaChecksum?: boolean;
badChecksum?: string;
badMergedConfigs?: Record<string, unknown>;
badFullResponse?: Record<string, unknown>;
}): PromiseWithTimeout<Record<string, unknown>> {
const {
user,
Expand All @@ -79,6 +81,8 @@ export default class StatsigNetwork {
previousDerivedFields,
hadBadDeltaChecksum,
badChecksum,
badMergedConfigs,
badFullResponse,
} = args;
const input = {
user,
Expand All @@ -90,6 +94,8 @@ export default class StatsigNetwork {
previousDerivedFields: previousDerivedFields,
hadBadDeltaChecksum: hadBadDeltaChecksum,
badChecksum: badChecksum,
badMergedConfigs: badMergedConfigs,
badFullResponse: badFullResponse,
};

return this.postWithTimeout(StatsigEndpoint.Initialize, input, {
Expand Down
31 changes: 29 additions & 2 deletions src/StatsigStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ type APIInitializeDataWithDeltas = APIInitializeData & {
deleted_layers?: string[];
is_delta?: boolean;
checksum?: string;
deltas_full_response?: {
dynamic_configs: Record<string, APIDynamicConfig | undefined>;
feature_gates: Record<string, APIFeatureGate | undefined>;
layer_configs: Record<string, APIDynamicConfig | undefined>;
};
};

type APIInitializeDataWithPrefetchedUsers = APIInitializeData & {
Expand Down Expand Up @@ -477,11 +482,12 @@ export default class StatsigStore {
mergedValues[requestedUserCacheKey.v1];
removeDeletedKeysFromUserValues(initResponse, userValues);
const expectedFullHash = initResponse.checksum;
const currentFullHash = djb2HashForObject({
const mergedConfigs = {
feature_gates: userValues.feature_gates,
dynamic_configs: userValues.dynamic_configs,
layer_configs: userValues.layer_configs,
});
};
const currentFullHash = djb2HashForObject(mergedConfigs);
if (expectedFullHash && expectedFullHash !== currentFullHash) {
hasBadHash = true;
badChecksum = currentFullHash;
Expand All @@ -492,6 +498,23 @@ export default class StatsigStore {
}

if (hasBadHash || hashChanged) {
if (initResponse.deltas_full_response != null) {
// retry
this.refetchAndSaveValues(
user,
prefetchUsers,
undefined,
badChecksum,
hasBadHash,
mergedConfigs,
initResponse.deltas_full_response,
).catch((reason) =>
this.sdkInternal
.getErrorBoundary()
.logError('refetchAndSaveValues', reason),
);
return;
}
// retry
this.refetchAndSaveValues(
user,
Expand Down Expand Up @@ -525,6 +548,8 @@ export default class StatsigStore {
timeout: number = this.sdkInternal.getOptions().getInitTimeoutMs(),
badChecksum?: string,
hadBadChecksum?: boolean,
badMergedConfigs?: Record<string, unknown>,
badFullResponse?: Record<string, unknown>,
): Promise<void> {
const sinceTime = this.getLastUpdateTime(user);
const previousDerivedFields = this.getPreviousDerivedFields(user);
Expand All @@ -540,6 +565,8 @@ export default class StatsigStore {
previousDerivedFields,
hadBadDeltaChecksum: hadBadChecksum,
badChecksum,
badMergedConfigs,
badFullResponse,
})
.then((json) => {
if (json?.has_updates) {
Expand Down

0 comments on commit 6e76ef4

Please sign in to comment.