Make less decide requests when enriching events#72
Merged
Conversation
When `sendFeatureFlags` is `true`, the client makes a `Decide` requests to get the latest feature flags in order to enrich the captured event with the current feature flag values. However, when processing a batch, it doesn't make sense to make a decide request multiple times for the same `distinctId`. This commit adds an in-memory cache that lasts for the duration of the batch.
Contributor
There was a problem hiding this comment.
Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
src/PostHog/Library/AsyncBatchHandler.cs:92
- [nitpick] Consider using a more descriptive parameter name instead of '_' in the lambda to improve code clarity, for example 'context'.
public bool Enqueue(Task<TItem> item) => Enqueue(new BatchItem<TItem, TBatchContext>(_ => item));
src/PostHog/PostHogClient.cs:157
- [nitpick] Consider extracting the inline lambda 'BatchTask' into a well-named private method to improve readability, particularly for the ternary logic.
Task<CapturedEvent> BatchTask(CapturedEventBatchContext context) => sendFeatureFlags ? AddFreshFeatureFlagDataAsync(context.FeatureFlagCache, distinctId, groups, capturedEvent) : _featureFlagsLoader.IsLoaded && eventName != "$feature_flag_called" ? AddLocalFeatureFlagDataAsync(distinctId, groups, capturedEvent) : Task.FromResult(capturedEvent);
Contributor
There was a problem hiding this comment.
PR Summary
This PR optimizes feature flag enrichment by adding in-memory caching to avoid duplicate decide calls during batch processing.
- Updated src/PostHog/PostHogClient.cs to integrate a batch context using a FallbackFeatureFlagCache.
- Added src/PostHog/Capture/CapturedEventBatchContext.cs to encapsulate cache context for batch processing.
- Introduced src/PostHog/Features/MemoryFeatureFlagCache.cs and FallbackFeatureFlagCache.cs for caching.
- Refactored src/PostHog/Library/AsyncBatchHandler.cs to accept a batch context, reducing redundant decide requests.
- Minor syntax improvement in src/PostHog/Config/PostHogOptions.cs using target-typed new.
13 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
sendFeatureFlagsistrue, the client makes aDeciderequests to get the latest feature flags in order to enrich the captured event with the current feature flag values.However, when processing a batch, it doesn't make sense to make a decide request multiple times for the same
distinctId. This PR adds an in-memory cache that lasts for the duration of the batch.So far, I don't think a lot of the other client libraries actually use the
/batchendpoint to batch requests. And if they do, I don't think they're caching/deciderequests. This is a change I'd like to consider doing for all of the clients.