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

Take the reason and user into account when caching events #11

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ export function initialize(env, user, specifiedOptions, platform, extraOptionDef
function sendFlagEvent(key, detail, defaultValue, includeReason) {
const user = ident.getUser();
const now = new Date();
const value = detail ? detail.value : null;
if (!options.allowFrequentDuplicateEvents) {
const cacheKey = JSON.stringify(value) + (user && user.key ? user.key : '') + key; // see below
const cacheKey = JSON.stringify(detail) + JSON.stringify(user) + key; // see below
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps only apply these changes if includeReason or inlineUsersInEvents is true.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have a different concern: the behavior of JSON.stringify is not deterministic such that serializing an object with the same properties would always serialize them in the same order. That was already a flaw in the existing logic since value could be a JSON object, but that's a rare enough usage that it hasn't been an issue before, especially since the value of value is always (for any valid flag) an exact copy of the same internal flag value. It's more problematic with regard to the user properties, and the detail object which could be constructed in a different order in different places in the code. In general it's not ideal to use a serialized complex object as a cache key if it doesn't have a canonical encoding.

Serializing the entire user object for every evaluation also adds potentially non-trivial overhead to applications that do frequent evaluations (with even more overhead if we add logic to canonicalize objects as mentioned above), so at a minimum this changed behavior would need to be opt-in.

const cached = seenRequests[cacheKey];
// cache TTL is five minutes
if (cached && now - cached < 300000) {
Expand All @@ -160,7 +159,7 @@ export function initialize(env, user, specifiedOptions, platform, extraOptionDef
kind: 'feature',
key: key,
user: user,
value: value,
value: detail ? detail.value : null,
variation: detail ? detail.variationIndex : null,
default: defaultValue,
creationDate: now.getTime(),
Expand Down