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

perf: don't fetch from db in constructor #138

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions shared/rollouts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def __init__(self, name, proportion=None, salt=None):
args["salt"] = salt

# so it is hashable
args = tuple(sorted(args.items()))

self._fetch_and_set_from_db(args)
self.args = tuple(sorted(args.items()))

def check_value(self, identifier, default=False):
"""
Expand All @@ -104,7 +102,12 @@ def check_value(self, identifier, default=False):
feature variants via Django Admin.
"""
# Will only run and refresh values from the database every ~5 minutes due to TTL cache
self._fetch_and_set_from_db()
self._fetch_and_set_from_db(self.args)

if (
self.args
): # to create a default when `check_value()` is run for the first time
self.args = None

return self._check_value(identifier, default)

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_rollouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ def test_buckets(self):
"complex",
0.5,
)

# # To make the math simpler, let's pretend our hash function can only
# # return 200 different values.
with patch.object(Feature, "HASHSPACE", 200):

complex_feature.check_value("garbage") # to force fetch values from db

# Because the top-level feature proportion is 0.5, we are only using the
# first 50% of our 200 hash values as our test population: [0..100]
# Each feature variant has a proportion of 1/3, so our three buckets
Expand Down
Loading