-
Notifications
You must be signed in to change notification settings - Fork 28
fix: Dedupe feature flag called events by response #150
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
Merged
+154
−4
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c27cde9
fix: dedupe feature flag called events by response
marandaneto 9764fbd
address called cache review feedback
marandaneto 98602d8
fix called cache credo nesting
marandaneto be45ea0
use ets for called cache
marandaneto b2c04a7
format called cache test
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| hex/posthog: patch | ||
| --- | ||
|
|
||
| Dedupe feature flag called events by response |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| defmodule PostHog.FeatureFlags.CalledCache do | ||
| @moduledoc false | ||
|
|
||
| use GenServer | ||
|
|
||
| @max_size 50_000 | ||
|
|
||
| @spec start_link(keyword()) :: GenServer.on_start() | ||
| def start_link(opts) do | ||
| supervisor_name = Keyword.fetch!(opts, :supervisor_name) | ||
|
|
||
| GenServer.start_link(__MODULE__, supervisor_name, | ||
| name: PostHog.Registry.via(supervisor_name, __MODULE__) | ||
| ) | ||
| end | ||
|
|
||
| @impl GenServer | ||
| def init(supervisor_name) do | ||
| table = | ||
| supervisor_name | ||
| |> table_name() | ||
| |> :ets.new([ | ||
| :set, | ||
| :public, | ||
| :named_table, | ||
| read_concurrency: true, | ||
| write_concurrency: true | ||
| ]) | ||
|
|
||
| {:ok, %{table: table}} | ||
| end | ||
|
|
||
| @spec first_seen?(PostHog.supervisor_name(), PostHog.distinct_id(), String.t(), any()) :: | ||
| boolean() | ||
| def first_seen?(supervisor_name, distinct_id, flag_key, value) do | ||
| key = {to_string(distinct_id), flag_key, value} | ||
| table = table_name(supervisor_name) | ||
|
|
||
| case :ets.insert_new(table, {key}) do | ||
| true -> | ||
| rollover_if_full(supervisor_name, table, key) | ||
| true | ||
|
|
||
| false -> | ||
| false | ||
| end | ||
| rescue | ||
| ArgumentError -> true | ||
| catch | ||
| :exit, _ -> true | ||
| end | ||
|
|
||
| @impl GenServer | ||
| def handle_call({:rollover, key}, _from, %{table: table} = state) do | ||
| if over_max_size?(table) do | ||
| # Intentionally flush instead of evicting individual entries to keep | ||
| # the hot path simple. Previously seen values may emit again after | ||
| # the cache rolls over. | ||
| :ets.delete_all_objects(table) | ||
| :ets.insert(table, {key}) | ||
| end | ||
|
|
||
| {:reply, :ok, state} | ||
| end | ||
|
|
||
| defp rollover_if_full(supervisor_name, table, key) do | ||
| if over_max_size?(table) do | ||
| GenServer.call(PostHog.Registry.via(supervisor_name, __MODULE__), {:rollover, key}) | ||
| end | ||
| end | ||
|
|
||
| defp over_max_size?(table) do | ||
| case :ets.info(table, :size) do | ||
| size when is_integer(size) -> size > @max_size | ||
| _ -> false | ||
| end | ||
| end | ||
|
|
||
| defp table_name(supervisor_name), do: Module.concat(supervisor_name, CalledCacheTable) | ||
| end | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| defmodule PostHog.FeatureFlags.CalledCacheTest do | ||
| use PostHog.Case, async: true | ||
|
|
||
| alias PostHog.FeatureFlags.CalledCache | ||
|
|
||
| @max_size 50_000 | ||
|
|
||
| setup :setup_supervisor | ||
|
|
||
| test "returns true when the supervisor registry is not running" do | ||
| supervisor_name = __MODULE__.MissingSupervisor | ||
|
|
||
| refute Process.whereis(PostHog.Registry.registry_name(supervisor_name)) | ||
|
|
||
| assert CalledCache.first_seen?(supervisor_name, "user", "flag", true) | ||
| end | ||
|
|
||
| test "flushes the cache when it reaches the maximum size", %{config: config} do | ||
| supervisor_name = config.supervisor_name | ||
| seed_key = {"seed-user", "flag", true} | ||
| table = table(supervisor_name) | ||
|
|
||
| full_cache = | ||
| 1..(@max_size - 1) | ||
| |> Enum.map(&{{"user-#{&1}", "flag", true}}) | ||
| |> then(&[{seed_key} | &1]) | ||
|
|
||
| :ets.insert(table, full_cache) | ||
|
|
||
| refute CalledCache.first_seen?(supervisor_name, "seed-user", "flag", true) | ||
|
|
||
| assert CalledCache.first_seen?(supervisor_name, "overflow-user", "flag", true) | ||
| assert CalledCache.first_seen?(supervisor_name, "seed-user", "flag", true) | ||
| refute CalledCache.first_seen?(supervisor_name, "overflow-user", "flag", true) | ||
| end | ||
|
|
||
| defp table(supervisor_name) do | ||
| [{pid, _value}] = | ||
| Registry.lookup(PostHog.Registry.registry_name(supervisor_name), CalledCache) | ||
|
|
||
| pid | ||
| |> :sys.get_state() | ||
| |> Map.fetch!(:table) | ||
| end | ||
| end |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't account for
groupsin the cache keyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log_feature_flag_usagedoesn't account for it either, but it should be able to be provided toFeatureFlags.check,get_feature_flag_result,evaluate_flagsvia the bodyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah groups aren't accounted for in a few SDKs, i think i'd do this as a follow up since i can check/update the spec and do all at once later